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
+ }
3
9
4
10
export default class Request {
5
- get ( url : string , options : Object ) : Promise < any > {
11
+ get ( url : string , options : FetchOptions ) : Promise < any > {
6
12
options [ 'method' ] = 'GET' ;
7
13
return this . _fetchWithLogging ( url , options ) ;
8
14
}
9
15
10
- post ( url : string , payload : Object , options : Object ) : Promise < any > {
16
+ post ( url : string , payload : Object , options : FetchOptions ) : Promise < any > {
11
17
options [ 'method' ] = 'POST' ;
12
18
options [ 'body' ] = JSON . stringify ( payload ) ;
13
19
14
20
return this . _fetchWithLogging ( url , options ) ;
15
21
}
16
22
17
- put ( url : string , payload : Object , options : Object ) : Promise < any > {
23
+ put ( url : string , payload : Object , options : FetchOptions ) : Promise < any > {
18
24
options [ 'method' ] = 'PUT' ;
19
25
options [ 'body' ] = JSON . stringify ( payload ) ;
20
26
21
27
return this . _fetchWithLogging ( url , options ) ;
22
28
}
23
29
24
- delete ( url : string , options : Object ) : Promise < any > {
30
+ delete ( url : string , options : FetchOptions ) : Promise < any > {
25
31
options [ 'method' ] = 'DELETE' ;
26
32
return this . _fetchWithLogging ( url , options ) ;
27
33
}
@@ -36,7 +42,7 @@ export default class Request {
36
42
Config . logger . debug ( colorize ( 'bold' , JSON . stringify ( responseJSON , null , 4 ) ) ) ;
37
43
}
38
44
39
- private _fetchWithLogging ( url : string , options : Object ) : Promise < any > {
45
+ private _fetchWithLogging ( url : string , options : FetchOptions ) : Promise < any > {
40
46
this . _logRequest ( options [ 'method' ] , url ) ;
41
47
let promise = this . _fetch ( url , options ) ;
42
48
promise . then ( ( response : any ) => {
@@ -45,10 +51,13 @@ export default class Request {
45
51
return promise ;
46
52
}
47
53
48
- private _fetch ( url : string , options : Object ) : Promise < any > {
54
+ private _fetch ( url : string , opts : FetchOptions ) : Promise < any > {
49
55
return new Promise ( ( resolve , reject ) => {
56
+ // Clone options since we are changing the object
57
+ let options : RequestInit = cloneDeep ( opts as RequestInit )
58
+
50
59
let headers = this . buildHeaders ( options ) ;
51
- options [ ' headers' ] = headers ;
60
+ options . headers = headers ;
52
61
53
62
let fetchPromise = fetch ( url , options ) ;
54
63
fetchPromise . then ( ( response ) => {
@@ -57,17 +66,24 @@ export default class Request {
57
66
resolve ( response ) ;
58
67
} ) . catch ( ( e ) => { throw ( e ) ; } ) ;
59
68
} ) ;
69
+
60
70
fetchPromise . catch ( reject ) ;
61
71
} ) ;
62
72
}
63
73
64
- private buildHeaders ( options : Object ) : any {
74
+ private buildHeaders ( options : FetchOptions ) : any {
65
75
let headers = { } ;
76
+
77
+ if ( typeof options . headers == 'object' ) {
78
+ headers = options . headers
79
+ }
80
+
66
81
headers [ 'Accept' ] = 'application/json' ;
67
82
headers [ 'Content-Type' ] = 'application/json' ;
68
83
69
- if ( options [ 'jwt' ] ) {
70
- headers [ 'Authorization' ] = `Token token="${ options [ 'jwt' ] } "` ;
84
+ if ( options . jwt ) {
85
+ headers [ 'Authorization' ] = `Token token="${ options . jwt } "` ;
86
+ delete options . jwt
71
87
}
72
88
73
89
return headers ;
0 commit comments