import { Moment } from "moment" ; export class DisqusApi { static get instance ( ) { return disqusApi ; } private _apiKey : string ; private _accessToken : string ; private _forum : string ; public get apiKey ( ) : string { return this . _apiKey ; } public get accessToken ( ) : string { return this . _accessToken ; } public get forum ( ) : string { return this . _forum ; } public configure ( apiKey , accessToken , forum ) { this . _apiKey = apiKey ; this . _accessToken = accessToken ; this . _forum = forum ; } public createImport ( xmlContent , filename , onLoad ) { const formData = new FormData ( ) ; formData . append ( 'upload' , new Blob ( [ xmlContent ] , { type : 'text/xml' } ) , filename ) ; formData . append ( 'sourceType' , '0' ) ; formData . append ( 'forum' , this . forum ) ; return this . post ( 'imports/create' , formData , onLoad ) ; } public listPostsForForum ( cursor , startDate , endDate , limit , onLoad ) { const query = [ `start=${ startDate . toISOString ( ) }` , `end=${ endDate . toISOString ( ) }` , `forum=${ this . forum }` , 'related=thread' , `limit=${ Math . min ( Math . max ( limit , 1 ) , 100 ) }` , `cursor=${ cursor || '' }` , ] . join ( '&' ) ; return this . get ( 'posts/list' , query , onLoad ) ; } private get ( path , query , onLoad ) { if ( ! this . apiKey ) return null ; const XHR = new XMLHttpRequest ( ) ; XHR . open ( 'GET' , `https://disqus.com/api/3.0/${ path }.json?api_key=${ this . apiKey }&access_token=${ this . accessToken }&${ query }` ) ; XHR . addEventListener ( 'load' , onLoad ) ; XHR . send ( ) ; return XHR ; } private post ( path , data , onLoad ) { if ( ! this . apiKey ) return null ; data . append ( 'api_key' , this . apiKey ) ; data . append ( 'access_token' , this . accessToken ) ; const XHR = new XMLHttpRequest ( ) ; XHR . open ( 'POST' , `https://disqus.com/api/3.0/${ path }.json` ) ; XHR . addEventListener ( 'load' , onLoad ) ; XHR . send ( data ) ; return XHR ; } } const disqusApi = new DisqusApi ( ) ;