@@ -2,27 +2,27 @@ import Config from './configuration';
22import colorize from './util/colorize' ;
33
44export default class Request {
5- get ( url : string , options : Object ) : Promise < any > {
6- options [ ' method' ] = 'GET' ;
5+ get ( url : string , options : RequestInit ) : Promise < any > {
6+ options . method = 'GET' ;
77 return this . _fetchWithLogging ( url , options ) ;
88 }
99
10- post ( url : string , payload : Object , options : Object ) : Promise < any > {
11- options [ ' method' ] = 'POST' ;
12- 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 ) ;
1313
1414 return this . _fetchWithLogging ( url , options ) ;
1515 }
1616
17- put ( url : string , payload : Object , options : Object ) : Promise < any > {
18- options [ ' method' ] = 'PUT' ;
19- 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 ) ;
2020
2121 return this . _fetchWithLogging ( url , options ) ;
2222 }
2323
24- delete ( url : string , options : Object ) : Promise < any > {
25- options [ ' method' ] = 'DELETE' ;
24+ delete ( url : string , options : RequestInit ) : Promise < any > {
25+ options . method = 'DELETE' ;
2626 return this . _fetchWithLogging ( url , options ) ;
2727 }
2828
@@ -36,40 +36,26 @@ export default class Request {
3636 Config . logger . debug ( colorize ( 'bold' , JSON . stringify ( responseJSON , null , 4 ) ) ) ;
3737 }
3838
39- private _fetchWithLogging ( url : string , options : Object ) : Promise < any > {
40- this . _logRequest ( options [ ' method' ] , url ) ;
39+ private _fetchWithLogging ( url : string , options : RequestInit ) : Promise < any > {
40+ this . _logRequest ( options . method , url ) ;
4141 let promise = this . _fetch ( url , options ) ;
4242 promise . then ( ( response : any ) => {
4343 this . _logResponse ( response [ 'jsonPayload' ] ) ;
4444 } ) ;
4545 return promise ;
4646 }
4747
48- private _fetch ( url : string , options : Object ) : Promise < any > {
48+ private _fetch ( url : string , options : RequestInit ) : Promise < any > {
4949 return new Promise ( ( resolve , reject ) => {
50- let headers = this . buildHeaders ( options ) ;
51- options [ 'headers' ] = headers ;
52-
5350 let fetchPromise = fetch ( url , options ) ;
5451 fetchPromise . then ( ( response ) => {
5552 response . json ( ) . then ( ( json ) => {
5653 response [ 'jsonPayload' ] = json ;
5754 resolve ( response ) ;
5855 } ) . catch ( ( e ) => { throw ( e ) ; } ) ;
5956 } ) ;
57+
6058 fetchPromise . catch ( reject ) ;
6159 } ) ;
6260 }
63-
64- private buildHeaders ( options : Object ) : any {
65- let headers = { } ;
66- headers [ 'Accept' ] = 'application/json' ;
67- headers [ 'Content-Type' ] = 'application/json' ;
68-
69- if ( options [ 'jwt' ] ) {
70- headers [ 'Authorization' ] = `Token token="${ options [ 'jwt' ] } "` ;
71- }
72-
73- return headers ;
74- }
7561}
0 commit comments