1
1
import Config from './configuration' ;
2
2
import 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
- }
9
3
10
4
export 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' ;
13
7
return this . _fetchWithLogging ( url , options ) ;
14
8
}
15
9
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 ) ;
19
13
20
14
return this . _fetchWithLogging ( url , options ) ;
21
15
}
22
16
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 ) ;
26
20
27
21
return this . _fetchWithLogging ( url , options ) ;
28
22
}
29
23
30
- delete ( url : string , options : FetchOptions ) : Promise < any > {
31
- options [ ' method' ] = 'DELETE' ;
24
+ delete ( url : string , options : RequestInit ) : Promise < any > {
25
+ options . method = 'DELETE' ;
32
26
return this . _fetchWithLogging ( url , options ) ;
33
27
}
34
28
@@ -42,23 +36,17 @@ export default class Request {
42
36
Config . logger . debug ( colorize ( 'bold' , JSON . stringify ( responseJSON , null , 4 ) ) ) ;
43
37
}
44
38
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 ) ;
47
41
let promise = this . _fetch ( url , options ) ;
48
42
promise . then ( ( response : any ) => {
49
43
this . _logResponse ( response [ 'jsonPayload' ] ) ;
50
44
} ) ;
51
45
return promise ;
52
46
}
53
47
54
- private _fetch ( url : string , opts : FetchOptions ) : Promise < any > {
48
+ private _fetch ( url : string , options : RequestInit ) : Promise < any > {
55
49
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
-
62
50
let fetchPromise = fetch ( url , options ) ;
63
51
fetchPromise . then ( ( response ) => {
64
52
response . json ( ) . then ( ( json ) => {
@@ -70,22 +58,4 @@ export default class Request {
70
58
fetchPromise . catch ( reject ) ;
71
59
} ) ;
72
60
}
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
- }
91
61
}
0 commit comments