import AdminOptions , { IAdminOptions } from './reducers/AdminOptions' ; import { IDisqusWordpressWindow } from './reducers/AdminState' ; import REST_OPTIONS from './REST_OPTIONS' ; export interface IRestResponse < T > { code : string ; data : T ; message : string ; } export type RequestData = ( ) => { path : string ; data : any ; onLoad : ( response : IRestResponse < any > ) => void ; } ; export type PendingRequests = ( ) => Promise < void > ; export class WordPressRestApi { private static current : WordPressRestApi ; static get instance ( ) { if ( ! WordPressRestApi . current ) WordPressRestApi . current = new WordPressRestApi ( ) ; return WordPressRestApi . current ; } static set instance ( newInstance ) { WordPressRestApi . current = newInstance ; } public pluginRestGet ( path , onLoad ) { return this . makeApiRequest ( 'GET' , `${ REST_OPTIONS . base }${ REST_OPTIONS . disqusBase }${ path }` , null , ( xhr ) => { this . handleResponse ( ( xhr . target as XMLHttpRequest ) . responseText , onLoad ) ; } ) ; } public pluginRestPost ( path , data , onLoad ) { return this . makeApiRequest ( 'POST' , `${ REST_OPTIONS . base }${ REST_OPTIONS . disqusBase }${ path }` , data && JSON . stringify ( data ) , ( xhr ) => { this . handleResponse ( ( xhr . target as XMLHttpRequest ) . responseText , onLoad ) ; } ) ; } public pluginRestPostAsync ( path , data , onLoad ) { return this . makeApiPromise ( 'POST' , `${ REST_OPTIONS . base }${ REST_OPTIONS . disqusBase }${ path }` , data && JSON . stringify ( data ) , ( xhr ) => { this . handleResponse ( ( xhr . target as XMLHttpRequest ) . responseText , onLoad ) ; } ) ; } public wordpressRestGet ( path , query , onLoad ) { return this . makeApiRequest ( 'GET' , `${ REST_OPTIONS . base }wp/v2/${ path }${ REST_OPTIONS . base . indexOf ( '?' ) > - 1 ? '&' : '?' }${ query || '' }` , null , ( xhr ) => { this . handleResponse ( ( xhr . target as XMLHttpRequest ) . responseText , onLoad ) ; } ) ; } private makeApiRequest ( method , url , data , onLoad ) { const XHR = new XMLHttpRequest ( ) ; XHR . open ( method , url ) ; XHR . setRequestHeader ( 'Content-type' , 'application/json' ) ; XHR . setRequestHeader ( 'X-WP-Nonce' , REST_OPTIONS . nonce ) ; XHR . addEventListener ( 'load' , onLoad ) ; XHR . send ( data ) ; return XHR ; } private makeApiPromise ( method , url , data , onLoad ) { const XHR = new XMLHttpRequest ( ) ; return new Promise ( ( resolve , reject ) => { XHR . open ( method , url ) ; XHR . setRequestHeader ( 'Content-type' , 'application/json' ) ; XHR . setRequestHeader ( 'X-WP-Nonce' , REST_OPTIONS . nonce ) ; XHR . addEventListener ( 'load' , onLoad ) ; XHR . onreadystatechange = ( ) => { if ( XHR . readyState !== 4 ) { return ; } if ( XHR . status >= 200 && XHR . status < 300 ) { resolve ( ) ; } else { reject ( { status : XHR . status , statusText : XHR . statusText } ) ; console . error ( 'Error' , XHR . statusText ) ; } } ; XHR . send ( data ) ; } ) ; } private handleResponse ( text , callback ) { let jsonObject = null ; try { jsonObject = JSON . parse ( text ) ; } catch ( error ) { } callback . call ( null , jsonObject ) ; } }