import fetch from 'cross-fetch' ; export interface AwsCredentials { accessKeyId : string ; secretAccessKey : string ; sessionToken : string ; } export interface AwsUploadContentAuth { bucketName : string ; credentials : AwsCredentials ; } export class APIClient { constructor ( private baseUrl , public getToken ) { this . baseUrl = baseUrl . replace ( /\/$/ , '' ) ; } aws_getAwsUploadContentAuth ( ) { return this . call ( 'aws.getAwsUploadContentAuth' , { } ) ; } challenge_updateChallenge ( values ) { return this . call ( 'challenge.updateChallenge' , { values } ) ; } module_updateModule ( values ) { return this . call ( 'module.updateModule' , { values } ) ; } private async call ( name , params ) { const token = this . getToken ( ) ; const headers = { 'content-type' : 'application/json' , } ; if ( token ) { headers [ 'authorization' ] = token ; } const res = await fetch ( `${ this . baseUrl }/rpc/${ name }` , { method : 'POST' , headers , body : JSON . stringify ( params ) , } ) ; const body = await res . json ( ) ; if ( res . status !== 200 ) { const err = new Error ( body . error || 'Failed to call API' ) ; err . res = res ; err . body = body ; throw err ; } return body ; } }