@@ -2,27 +2,27 @@ import Config from './configuration';
2
2
import colorize from './util/colorize' ;
3
3
4
4
export 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' ;
7
7
return this . _fetchWithLogging ( url , options ) ;
8
8
}
9
9
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 ) ;
13
13
14
14
return this . _fetchWithLogging ( url , options ) ;
15
15
}
16
16
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 ) ;
20
20
21
21
return this . _fetchWithLogging ( url , options ) ;
22
22
}
23
23
24
- delete ( url : string , options : Object ) : Promise < any > {
25
- options [ ' method' ] = 'DELETE' ;
24
+ delete ( url : string , options : RequestInit ) : Promise < any > {
25
+ options . method = 'DELETE' ;
26
26
return this . _fetchWithLogging ( url , options ) ;
27
27
}
28
28
@@ -36,40 +36,26 @@ export default class Request {
36
36
Config . logger . debug ( colorize ( 'bold' , JSON . stringify ( responseJSON , null , 4 ) ) ) ;
37
37
}
38
38
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 ) ;
41
41
let promise = this . _fetch ( url , options ) ;
42
42
promise . then ( ( response : any ) => {
43
43
this . _logResponse ( response [ 'jsonPayload' ] ) ;
44
44
} ) ;
45
45
return promise ;
46
46
}
47
47
48
- private _fetch ( url : string , options : Object ) : Promise < any > {
48
+ private _fetch ( url : string , options : RequestInit ) : Promise < any > {
49
49
return new Promise ( ( resolve , reject ) => {
50
- let headers = this . buildHeaders ( options ) ;
51
- options [ 'headers' ] = headers ;
52
-
53
50
let fetchPromise = fetch ( url , options ) ;
54
51
fetchPromise . then ( ( response ) => {
55
52
response . json ( ) . then ( ( json ) => {
56
53
response [ 'jsonPayload' ] = json ;
57
54
resolve ( response ) ;
58
55
} ) . catch ( ( e ) => { throw ( e ) ; } ) ;
59
56
} ) ;
57
+
60
58
fetchPromise . catch ( reject ) ;
61
59
} ) ;
62
60
}
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
- }
75
61
}
0 commit comments