@@ -34,23 +34,29 @@ class AbstractClient {
34
34
return `${ pathBase } /${ idValue } ` ;
35
35
}
36
36
37
- find ( id , queryParam = { } , pathParameters = { } ) {
37
+ find ( id , queryParam = { } , pathParameters = { } , requestParams = { } ) {
38
38
const url = this . _generateUrlFromParams ( queryParam , pathParameters , id ) ;
39
39
40
- return this . deserializeResponse ( this . authorizedFetch ( url ) , 'item' ) ;
40
+ return this . deserializeResponse (
41
+ this . authorizedFetch ( url , requestParams ) ,
42
+ 'item'
43
+ ) ;
41
44
}
42
45
43
- findBy ( queryParam , pathParameters = { } ) {
46
+ findBy ( queryParam , pathParameters = { } , requestParams = { } ) {
44
47
const url = this . _generateUrlFromParams ( queryParam , pathParameters ) ;
45
48
46
- return this . deserializeResponse ( this . authorizedFetch ( url ) , 'list' ) ;
49
+ return this . deserializeResponse (
50
+ this . authorizedFetch ( url , requestParams ) ,
51
+ 'list'
52
+ ) ;
47
53
}
48
54
49
- findAll ( queryParam = { } , pathParameters = { } ) {
50
- return this . findBy ( queryParam , pathParameters ) ;
55
+ findAll ( queryParam = { } , pathParameters = { } , requestParams = { } ) {
56
+ return this . findBy ( queryParam , pathParameters , requestParams ) ;
51
57
}
52
58
53
- create ( entity , queryParam = { } , pathParameters = { } ) {
59
+ create ( entity , queryParam = { } , pathParameters = { } , requestParams = { } ) {
54
60
const url = new URI ( this . getPathBase ( pathParameters ) ) ;
55
61
url . addSearch ( queryParam ) ;
56
62
@@ -70,12 +76,13 @@ class AbstractClient {
70
76
this . authorizedFetch ( url , {
71
77
method : 'POST' ,
72
78
body : this . serializer . encodeItem ( diff , this . metadata ) ,
79
+ ...requestParams ,
73
80
} ) ,
74
81
'item'
75
82
) ;
76
83
}
77
84
78
- update ( entity , queryParam = { } ) {
85
+ update ( entity , queryParam = { } , requestParams = { } ) {
79
86
const url = new URI ( this . getEntityURI ( entity ) ) ;
80
87
url . addSearch ( queryParam ) ;
81
88
@@ -99,17 +106,19 @@ class AbstractClient {
99
106
this . authorizedFetch ( url , {
100
107
method : 'PUT' ,
101
108
body : this . serializer . encodeItem ( newSerializedModel , this . metadata ) ,
109
+ ...requestParams ,
102
110
} ) ,
103
111
'item'
104
112
) ;
105
113
}
106
114
107
- delete ( entity ) {
115
+ delete ( entity , requestParams = { } ) {
108
116
const url = this . getEntityURI ( entity ) ;
109
117
const identifier = this . _getEntityIdentifier ( entity ) ;
110
118
111
119
return this . authorizedFetch ( url , {
112
120
method : 'DELETE' ,
121
+ ...requestParams ,
113
122
} ) . then ( response => {
114
123
this . sdk . unitOfWork . clear ( identifier ) ;
115
124
@@ -186,10 +195,10 @@ class AbstractClient {
186
195
return url ;
187
196
}
188
197
189
- authorizedFetch ( input , init ) {
198
+ authorizedFetch ( input , requestParams = { } ) {
190
199
const url = this . makeUri ( input ) ;
191
200
192
- return this . _fetchWithToken ( url . toString ( ) , init ) ;
201
+ return this . _fetchWithToken ( url . toString ( ) , requestParams ) ;
193
202
}
194
203
195
204
_generateUrlFromParams ( queryParam , pathParameters = { } , id = null ) {
@@ -222,7 +231,7 @@ class AbstractClient {
222
231
return url ;
223
232
}
224
233
225
- _fetchWithToken ( input , init ) {
234
+ _fetchWithToken ( input , requestParams = { } ) {
226
235
if ( ! input ) {
227
236
throw new Error ( 'input is empty' ) ;
228
237
}
@@ -244,24 +253,24 @@ class AbstractClient {
244
253
245
254
return accessToken ;
246
255
} )
247
- . then ( token => this . _doFetch ( token , input , init ) ) ;
256
+ . then ( token => this . _doFetch ( token , input , requestParams ) ) ;
248
257
}
249
258
250
- return this . _doFetch ( null , input , init ) ;
259
+ return this . _doFetch ( null , input , requestParams ) ;
251
260
}
252
261
253
- _refreshTokenAndRefetch ( response , input , init ) {
262
+ _refreshTokenAndRefetch ( response , input , requestParams = { } ) {
254
263
return this . _tokenStorage . refreshToken ( ) . then ( ( ) => {
255
- const params = Object . assign ( { } , init , {
256
- headers : Object . assign ( { } , init . headers ) ,
264
+ const updatedRequestParams = Object . assign ( { } , requestParams , {
265
+ headers : Object . assign ( { } , requestParams . headers ) ,
257
266
} ) ;
258
- delete params . headers . Authorization ;
267
+ delete updatedRequestParams . headers . Authorization ;
259
268
260
- return this . _fetchWithToken ( input , params ) ;
269
+ return this . _fetchWithToken ( input , updatedRequestParams ) ;
261
270
} ) ;
262
271
}
263
272
264
- _manageUnauthorized ( response , input , init ) {
273
+ _manageUnauthorized ( response , input , requestParams = { } ) {
265
274
const error = getHttpErrorFromResponse ( response ) ;
266
275
267
276
// https://tools.ietf.org/html/rfc2617#section-1.2
@@ -271,7 +280,7 @@ class AbstractClient {
271
280
'error = "invalid_grant"'
272
281
) ;
273
282
if ( invalidGrant && this . _tokenStorage ) {
274
- return this . _refreshTokenAndRefetch ( response , input , init ) ;
283
+ return this . _refreshTokenAndRefetch ( response , input , requestParams ) ;
275
284
}
276
285
277
286
throw error ;
@@ -281,11 +290,11 @@ class AbstractClient {
281
290
. json ( )
282
291
. then ( body => {
283
292
if ( this . _tokenStorage && body . error === 'invalid_grant' ) {
284
- return this . _refreshTokenAndRefetch ( response , input , init ) ;
293
+ return this . _refreshTokenAndRefetch ( response , input , requestParams ) ;
285
294
}
286
295
throw error ;
287
296
} )
288
- . catch ( ( err ) => {
297
+ . catch ( err => {
289
298
if ( err instanceof OauthError ) {
290
299
throw err ;
291
300
}
@@ -294,8 +303,8 @@ class AbstractClient {
294
303
}
295
304
}
296
305
297
- _doFetch ( accessToken , input , init ) {
298
- let params = init ;
306
+ _doFetch ( accessToken , input , requestParams ) {
307
+ let params = requestParams ;
299
308
300
309
const baseHeaders = {
301
310
'Content-Type' : 'application/json' ,
0 commit comments