11import Config from './configuration' ;
22import colorize from './util/colorize' ;
3- import cloneDeep from './util/clonedeep' ;
4-
5- // RequestInit is the type expected by `fetch()` API.
6- export interface FetchOptions extends RequestInit {
7- jwt ? : string | undefined
8- }
93
104export default class Request {
11- get ( url : string , options : FetchOptions ) : Promise < any > {
12- options [ ' method' ] = 'GET' ;
5+ get ( url : string , options : RequestInit ) : Promise < any > {
6+ options . method = 'GET' ;
137 return this . _fetchWithLogging ( url , options ) ;
148 }
159
16- post ( url : string , payload : Object , options : FetchOptions ) : Promise < any > {
17- options [ ' method' ] = 'POST' ;
18- options [ ' body' ] = JSON . stringify ( payload ) ;
10+ post ( url : string , payload : Object , options : RequestInit ) : Promise < any > {
11+ options . method = 'POST' ;
12+ options . body = JSON . stringify ( payload ) ;
1913
2014 return this . _fetchWithLogging ( url , options ) ;
2115 }
2216
23- put ( url : string , payload : Object , options : FetchOptions ) : Promise < any > {
24- options [ ' method' ] = 'PUT' ;
25- options [ ' body' ] = JSON . stringify ( payload ) ;
17+ put ( url : string , payload : Object , options : RequestInit ) : Promise < any > {
18+ options . method = 'PUT' ;
19+ options . body = JSON . stringify ( payload ) ;
2620
2721 return this . _fetchWithLogging ( url , options ) ;
2822 }
2923
30- delete ( url : string , options : FetchOptions ) : Promise < any > {
31- options [ ' method' ] = 'DELETE' ;
24+ delete ( url : string , options : RequestInit ) : Promise < any > {
25+ options . method = 'DELETE' ;
3226 return this . _fetchWithLogging ( url , options ) ;
3327 }
3428
@@ -42,23 +36,17 @@ export default class Request {
4236 Config . logger . debug ( colorize ( 'bold' , JSON . stringify ( responseJSON , null , 4 ) ) ) ;
4337 }
4438
45- private _fetchWithLogging ( url : string , options : FetchOptions ) : Promise < any > {
46- this . _logRequest ( options [ ' method' ] , url ) ;
39+ private _fetchWithLogging ( url : string , options : RequestInit ) : Promise < any > {
40+ this . _logRequest ( options . method , url ) ;
4741 let promise = this . _fetch ( url , options ) ;
4842 promise . then ( ( response : any ) => {
4943 this . _logResponse ( response [ 'jsonPayload' ] ) ;
5044 } ) ;
5145 return promise ;
5246 }
5347
54- private _fetch ( url : string , opts : FetchOptions ) : Promise < any > {
48+ private _fetch ( url : string , options : RequestInit ) : Promise < any > {
5549 return new Promise ( ( resolve , reject ) => {
56- // Clone options since we are changing the object
57- let options : RequestInit = cloneDeep ( opts as RequestInit )
58-
59- let headers = this . buildHeaders ( options ) ;
60- options . headers = headers ;
61-
6250 let fetchPromise = fetch ( url , options ) ;
6351 fetchPromise . then ( ( response ) => {
6452 response . json ( ) . then ( ( json ) => {
@@ -70,22 +58,4 @@ export default class Request {
7058 fetchPromise . catch ( reject ) ;
7159 } ) ;
7260 }
73-
74- private buildHeaders ( options : FetchOptions ) : any {
75- let headers = { } ;
76-
77- if ( typeof options . headers == 'object' ) {
78- headers = options . headers
79- }
80-
81- headers [ 'Accept' ] = 'application/json' ;
82- headers [ 'Content-Type' ] = 'application/json' ;
83-
84- if ( options . jwt ) {
85- headers [ 'Authorization' ] = `Token token="${ options . jwt } "` ;
86- delete options . jwt
87- }
88-
89- return headers ;
90- }
9161}
0 commit comments