import { HttpClient , RequestConfig } from '../..' ; declare const fetch ; export class FetchClient implements HttpClient { private maxConcurrentRequests = 15 ; private requestIntervalMs = 10 ; private pendingRequests = 0 ; get ( url , config ? ) { return this . request ( url , 'GET' , config ) ; } delete ( url , config ? ) { return this . request ( url , 'DELETE' , config ) ; } head ( url , config ? ) { return this . request ( url , 'HEAD' , config ) ; } post ( url , data ? , config ? ) { return this . request ( url , 'POST' , config , data ) ; } put ( url , data ? , config ? ) { return this . request ( url , 'PUT' , config , data ) ; } patch ( url , data ? , config ? ) { return this . request ( url , 'PATCH' , config , data ) ; } private async request ( url , method , config ? , data ? ) { let body = undefined ; if ( ! ! data ) { if ( typeof data === 'object' && ! this . isBuffer ( data ) ) { body = JSON . stringify ( data ) ; config = config || { headers : { } } ; config . headers = config . headers || { } ; config . headers [ 'Content-Type' ] = 'application/json' ; } else { body = data ; } } await this . waitInQueue ( ) ; return fetch ( url , { method : method , headers : ! ! config ? config . headers : { } , mode : ( config && config . mode ) || 'no-cors' , body : body , } ) . then ( async ( resp ) => { if ( resp . status === 204 ) { return { } ; } const text = await resp . text ( ) ; const json = text ? JSON . parse ( text ) : { } ; if ( resp . status >= 200 && resp . status < 300 ) { return json ; } else { throw json ; } } ) . finally ( ( ) => ( this . pendingRequests = Math . max ( 0 , this . pendingRequests - 1 ) ) ) ; } private isBuffer ( data ) { if ( typeof ArrayBuffer === 'function' ) { return ArrayBuffer . isView ( data ) ; } else if ( typeof Buffer === 'function' ) { return Buffer . isBuffer ( data ) ; } else { return false ; } } private waitInQueue ( ) { return new Promise < void > ( resolve => { const interval = setInterval ( ( ) => { if ( this . pendingRequests < this . maxConcurrentRequests ) { this . pendingRequests ++ ; clearInterval ( interval ) ; resolve ( ) ; } } , this . requestIntervalMs ) ; } ) ; } }