import '@ef-carbon/fetch/install' ; import URL from 'url-parse' ; import { buildAbsoluteURL } from 'url-toolkit' ; import { KeyValue , ApiOptions } from '../models' ; export const DEFAULT_API_OPTIONS = { url : 'http://127.0.0.1' , port : 40600 , version : 0 } ; param { string [ ] } argsAr private export const getArgs = ( argsAr ? ) => { if ( ! argsAr || ! argsAr . length ) { return '' ; } return argsAr . map ( ( ar ) => encodeValue ( ar ) ) . join ( ',' ) ; } ; param { Object . < string , string > } opts private export const getOpts = ( opts ? ) => { if ( ! opts ) { return '' ; } return Object . keys ( opts ) . map ( ( key ) => `${ key }=${ encodeValue ( opts [ key ] ) }` ) . join ( ',' ) ; } ; const encodeValue = ( val ) => { return encodeURIComponent ( val . toString ( ) ) ; } ; export const createHeaders = ( args ? , opts ? , headers ? ) : Record < string , string > => { const h = headers || { } ; return { ... h , 'X-Textile-Args' : getArgs ( args ) , 'X-Textile-Opts' : getOpts ( opts ) } ; } ; const handleErrors = ( response ) => { if ( ! response . ok ) { throw Error ( response . statusText ) ; } return response ; } ; params class API { private opts : ApiOptions ; private baseURL : string ; private gatewayURL : string ; constructor ( opts = DEFAULT_API_OPTIONS ) { this . opts = opts ; const url = new URL ( opts . url ) ; if ( opts . port ) { url . set ( 'port' , opts . port ) ; } url . set ( 'pathname' , `/api/v${ opts . version || 0 }/` ) ; this . baseURL = url . toString ( ) ; const gateway = new URL ( this . opts . url ) ; gateway . set ( 'port' , 5050 ) ; gateway . set ( 'pathname' , `/ipfs/` ) ; this . gatewayURL = gateway . toString ( ) ; } param url param args param opts protected async sendGatewayGet ( path , headers ? ) { return fetch ( buildAbsoluteURL ( this . gatewayURL , path ) , { method : 'GET' , headers : createHeaders ( [ ] , { } , headers ) } ) ; } param url param args param opts param data protected async sendPost ( url , args ? , opts ? , data ? , headers ? , raw ? ) { const h = createHeaders ( args , opts , headers ) ; const response = await fetch ( buildAbsoluteURL ( this . baseURL , url ) , { method : 'POST' , headers : new Headers ( h ) , body : raw ? data : JSON . stringify ( data ) } ) ; return handleErrors ( response ) ; } param url param args param opts protected async sendGet ( url , args ? , opts ? , headers ? ) { const response = await fetch ( buildAbsoluteURL ( this . baseURL , url ) , { method : 'GET' , headers : createHeaders ( args , opts , headers ) } ) ; return handleErrors ( response ) ; } param url param args param opts protected async sendDelete ( url , args ? , opts ? , headers ? ) { const response = await fetch ( buildAbsoluteURL ( this . baseURL , url ) , { method : 'DELETE' , headers : createHeaders ( args , opts , headers ) } ) ; return handleErrors ( response ) ; } param url param args param opts param data protected async sendPut ( url , args ? , opts ? , data ? , headers ? ) { const response = await fetch ( buildAbsoluteURL ( this . baseURL , url ) , { method : 'PUT' , headers : createHeaders ( args , opts , headers ) , body : JSON . stringify ( data ) } ) ; return handleErrors ( response ) ; } param url param args param opts param data protected async sendPatch ( url , args ? , opts ? , data ? , headers ? ) { const response = await fetch ( buildAbsoluteURL ( this . baseURL , url ) , { method : 'patch' , headers : createHeaders ( args , opts , headers ) , body : JSON . stringify ( data ) } ) ; return handleErrors ( response ) ; } param url param args param opts protected sendEventSource ( url , args ? , opts ? , headers ? ) { return new EventSource ( buildAbsoluteURL ( this . baseURL , `${ url }${ opts ? `?${ URL . qs . stringify ( opts ) }` : '' }` ) ) ; } } export { API } ;