import { Token } from "client-oauth2" ; import { UnreadCountsResponse } from "./UnreadCountResponse" ; import { SubscriptionsResponse } from "./SubscriptionResponse" ; import { Subscription } from "../../domain/Subscriptions/Subscription" ; import { StreamContentsResponse } from "./StreamContentsResponse" ; import { stringify } from "querystring" ; import { InoreaderAuthority } from "../../domain/App/Authority/InoreaderAuthority" ; import { OAuth } from "./OAuth" ; const debug = require ( "debug" ) ( "irodr:InoreaderAPI" ) ; const baseURL = localStorage . getItem ( "REACT_APP_INOREADER_API_BASE_URL" ) || process . env . REACT_APP_INOREADER_API_BASE_URL ; const userFetch = ( input , init ? ) => { return window . fetch ( input , init ) ; } ; export class InoreaderAPI { baseURL : string ; private auth : OAuth ; constructor ( private inoreaderAuthority ) { this . auth = new OAuth ( { clientId : this . inoreaderAuthority . clientId , clientSecret : this . inoreaderAuthority . clientSecret , accessTokenUri : this . inoreaderAuthority . accessTokenUri , authorizationUri : this . inoreaderAuthority . authorizationUri , scopes : this . inoreaderAuthority . scopes , state : this . inoreaderAuthority . state } ) ; this . baseURL = baseURL || "" ; } getToken ( ) { return this . auth . getToken ( ) ; } saveTokenFromCallbackURL ( url ) { return this . auth . saveTokenFromCallbackURL ( url ) ; } getAuthorizeUrl ( ) { return this . auth . getNewTokenUrl ( ) ; } getRequest ( apiPath , parameters ? ) { return this . getToken ( ) . then ( ( token ) => { debug ( "token" , token ) ; const query = parameters ? `?${ stringify ( parameters ) }` : "" ; const requestObject = token . sign ( { method : "get" , url : this . baseURL + apiPath + query } ) ; const headers = { } ; Object . keys ( ( requestObject as any ) . headers ) . forEach ( ( key ) => { headers [ key ] = ( requestObject as any ) . headers [ key ] ; } ) ; return userFetch ( requestObject . url , { method : requestObject . method , headers : headers } ) ; } ) . catch ( ( error ) => { debug ( "Fetch Error" , error ) ; return Promise . reject ( error ) ; } ) ; } postRequest < T > ( apiPath , body ) { return this . getToken ( ) . then ( ( token ) => { const requestObject = token . sign ( { method : "post" , url : this . baseURL + apiPath } ) ; const headers = { } ; Object . keys ( ( requestObject as any ) . headers ) . forEach ( ( key ) => { headers [ key ] = ( requestObject as any ) . headers [ key ] ; } ) ; headers [ "Accept" ] = "application/json, text/plain, */*" ; headers [ "Content-Type" ] = "application/json" ; return userFetch ( requestObject . url , { method : requestObject . method , headers : headers , body : JSON . stringify ( body ) } ) . then ( ( res ) => res . json ( ) ) . then ( function ( res ) { return res ; } ) ; } ) . catch ( ( error ) => { debug ( "Fetch Error" , error ) ; return Promise . reject ( error ) ; } ) ; } subscriptions ( ) { return this . getRequest ( "/api/0/subscription/list" ) . then ( ( res ) => { debug ( "subscriptions:response" , res ) ; return res . json ( ) ; } ) . then ( function ( json ) { return json ; } ) ; } unreadCounts ( ) { return this . getRequest ( "/api/0/unread-count" ) . then ( ( res ) => { debug ( "unreadCounts:response" , res ) ; return res . json ( ) ; } ) . then ( function ( json ) { debug ( "unreadCounts:response.json" , json ) ; return json ; } ) ; } streamContents ( subscription , fetchCount , isContinuous ? ) { const feedId = encodeURIComponent ( subscription . props . id . toValue ( ) ) ; return this . getRequest ( `/api/0/stream/contents/${ feedId }` , { n : fetchCount , c : isContinuous && subscription . contents . continuationKey ? subscription . contents . continuationKey : undefined } ) . then ( ( res ) => res . json ( ) ) . then ( function ( res ) { return res ; } ) ; } markAsRead ( subscription ) { return this . getRequest ( "/api/0/mark-all-as-read" , { s : subscription . props . id . toValue ( ) , ts : subscription . unread . readTimestamp . second } ) . then ( ( res ) => res . text ( ) ) . then ( ( res ) => { return res === "ok" ; } ) ; } }