@@ -37,7 +37,6 @@ import { FileFull } from '../schemas/fileFull.generated.js';
37
37
import { ResponseFormat } from '../networking/fetchOptions.generated.js' ;
38
38
import { UserFull } from '../schemas/userFull.generated.js' ;
39
39
import { CreateUserRequestBody } from '../managers/users.generated.js' ;
40
- import { Interceptor } from '../networking/interceptors.generated.js' ;
41
40
import { getUuid } from '../internal/utils.js' ;
42
41
import { generateByteStream } from '../internal/utils.js' ;
43
42
import { bufferEquals } from '../internal/utils.js' ;
@@ -53,13 +52,139 @@ import { jsonToSerializedData } from '../serialization/json.js';
53
52
import { getSdValueByKey } from '../serialization/json.js' ;
54
53
import { toString } from '../internal/utils.js' ;
55
54
import { sdToJson } from '../serialization/json.js' ;
55
+ import { Interceptor } from '../networking/interceptors.generated.js' ;
56
56
import { sdIsEmpty } from '../serialization/json.js' ;
57
57
import { sdIsBoolean } from '../serialization/json.js' ;
58
58
import { sdIsNumber } from '../serialization/json.js' ;
59
59
import { sdIsString } from '../serialization/json.js' ;
60
60
import { sdIsList } from '../serialization/json.js' ;
61
61
import { sdIsMap } from '../serialization/json.js' ;
62
62
export const client : BoxClient = getDefaultClient ( ) ;
63
+ export class InterceptorAddingRoleToFields implements Interceptor {
64
+ constructor (
65
+ fields : Omit <
66
+ InterceptorAddingRoleToFields ,
67
+ 'beforeRequest' | 'afterRequest'
68
+ > ,
69
+ ) { }
70
+ /**
71
+ * @param {FetchOptionsInput } optionsInput
72
+ * @returns {FetchOptions }
73
+ */
74
+ beforeRequest ( optionsInput : FetchOptionsInput ) : FetchOptions {
75
+ const options : FetchOptions = new FetchOptions ( {
76
+ url : optionsInput . url ,
77
+ method : optionsInput . method ,
78
+ params : optionsInput . params ,
79
+ headers : optionsInput . headers ,
80
+ data : optionsInput . data ,
81
+ fileStream : optionsInput . fileStream ,
82
+ multipartData : optionsInput . multipartData ,
83
+ contentType : optionsInput . contentType ,
84
+ responseFormat : optionsInput . responseFormat ,
85
+ auth : optionsInput . auth ,
86
+ networkSession : optionsInput . networkSession ,
87
+ cancellationToken : optionsInput . cancellationToken ,
88
+ } ) ;
89
+ return new FetchOptions ( {
90
+ url : options . url ,
91
+ method : options . method ,
92
+ headers : options . headers ,
93
+ params : { ...options . params , ...{ [ 'fields' ] : 'role' } } ,
94
+ data : options . data ,
95
+ fileStream : options . fileStream ,
96
+ multipartData : options . multipartData ,
97
+ contentType : options . contentType ,
98
+ responseFormat : options . responseFormat ,
99
+ auth : options . auth ,
100
+ networkSession : options . networkSession ,
101
+ cancellationToken : options . cancellationToken ,
102
+ } ) ;
103
+ }
104
+ /**
105
+ * @param {FetchResponse } response
106
+ * @returns {FetchResponse }
107
+ */
108
+ afterRequest ( response : FetchResponse ) : FetchResponse {
109
+ return response ;
110
+ }
111
+ }
112
+ export class InterceptorThrowingError implements Interceptor {
113
+ constructor (
114
+ fields : Omit < InterceptorThrowingError , 'beforeRequest' | 'afterRequest' > ,
115
+ ) { }
116
+ /**
117
+ * @param {FetchOptionsInput } optionsInput
118
+ * @returns {FetchOptions }
119
+ */
120
+ beforeRequest ( optionsInput : FetchOptionsInput ) : FetchOptions {
121
+ const options : FetchOptions = new FetchOptions ( {
122
+ url : optionsInput . url ,
123
+ method : optionsInput . method ,
124
+ params : optionsInput . params ,
125
+ headers : optionsInput . headers ,
126
+ data : optionsInput . data ,
127
+ fileStream : optionsInput . fileStream ,
128
+ multipartData : optionsInput . multipartData ,
129
+ contentType : optionsInput . contentType ,
130
+ responseFormat : optionsInput . responseFormat ,
131
+ auth : optionsInput . auth ,
132
+ networkSession : optionsInput . networkSession ,
133
+ cancellationToken : optionsInput . cancellationToken ,
134
+ } ) ;
135
+ return options ;
136
+ }
137
+ /**
138
+ * @param {FetchResponse } response
139
+ * @returns {FetchResponse }
140
+ */
141
+ afterRequest ( response : FetchResponse ) : FetchResponse {
142
+ return {
143
+ status : 400 ,
144
+ data : response . data ,
145
+ content : response . content ,
146
+ headers : response . headers ,
147
+ } satisfies FetchResponse ;
148
+ }
149
+ }
150
+ export class InterceptorChangingResponse implements Interceptor {
151
+ constructor (
152
+ fields : Omit < InterceptorChangingResponse , 'beforeRequest' | 'afterRequest' > ,
153
+ ) { }
154
+ /**
155
+ * @param {FetchOptionsInput } optionsInput
156
+ * @returns {FetchOptions }
157
+ */
158
+ beforeRequest ( optionsInput : FetchOptionsInput ) : FetchOptions {
159
+ const options : FetchOptions = new FetchOptions ( {
160
+ url : optionsInput . url ,
161
+ method : optionsInput . method ,
162
+ params : optionsInput . params ,
163
+ headers : optionsInput . headers ,
164
+ data : optionsInput . data ,
165
+ fileStream : optionsInput . fileStream ,
166
+ multipartData : optionsInput . multipartData ,
167
+ contentType : optionsInput . contentType ,
168
+ responseFormat : optionsInput . responseFormat ,
169
+ auth : optionsInput . auth ,
170
+ networkSession : optionsInput . networkSession ,
171
+ cancellationToken : optionsInput . cancellationToken ,
172
+ } ) ;
173
+ return options ;
174
+ }
175
+ /**
176
+ * @param {FetchResponse } response
177
+ * @returns {FetchResponse }
178
+ */
179
+ afterRequest ( response : FetchResponse ) : FetchResponse {
180
+ return {
181
+ status : response . status ,
182
+ data : jsonToSerializedData ( '{"id": "123", "type": "user"}' ) ,
183
+ content : response . content ,
184
+ headers : response . headers ,
185
+ } satisfies FetchResponse ;
186
+ }
187
+ }
63
188
test ( 'testMakeRequestJsonCRUD' , async function testMakeRequestJsonCRUD ( ) : Promise < any > {
64
189
const newFolderName : string = getUuid ( ) ;
65
190
const requestBodyPost : string = '' . concat (
@@ -273,80 +398,16 @@ test('testWithInterceptors', async function testWithInterceptors(): Promise<any>
273
398
if ( ! ( user . role == void 0 ) ) {
274
399
throw new Error ( 'Assertion failed' ) ;
275
400
}
276
- function beforeRequest ( optionsInput : FetchOptionsInput ) : FetchOptions {
277
- const options : FetchOptions = new FetchOptions ( {
278
- url : optionsInput . url ,
279
- method : optionsInput . method ,
280
- params : optionsInput . params ,
281
- headers : optionsInput . headers ,
282
- data : optionsInput . data ,
283
- fileStream : optionsInput . fileStream ,
284
- multipartData : optionsInput . multipartData ,
285
- contentType : optionsInput . contentType ,
286
- responseFormat : optionsInput . responseFormat ,
287
- auth : optionsInput . auth ,
288
- networkSession : optionsInput . networkSession ,
289
- cancellationToken : optionsInput . cancellationToken ,
290
- } ) ;
291
- return new FetchOptions ( {
292
- url : options . url ,
293
- method : options . method ,
294
- headers : options . headers ,
295
- params : { ...options . params , ...{ [ 'fields' ] : 'role' } } ,
296
- data : options . data ,
297
- fileStream : options . fileStream ,
298
- multipartData : options . multipartData ,
299
- contentType : options . contentType ,
300
- responseFormat : options . responseFormat ,
301
- auth : options . auth ,
302
- networkSession : options . networkSession ,
303
- cancellationToken : options . cancellationToken ,
304
- } ) ;
305
- }
306
- function emptyAfterRequest ( response : FetchResponse ) : FetchResponse {
307
- return response ;
308
- }
309
401
const clientWithInterceptor : BoxClient = client . withInterceptors ( [
310
- {
311
- beforeRequest : beforeRequest ,
312
- afterRequest : emptyAfterRequest ,
313
- } satisfies Interceptor ,
402
+ new InterceptorAddingRoleToFields ( { } ) ,
314
403
] ) ;
315
404
const newUser : UserFull = await clientWithInterceptor . users . getUserMe ( ) ;
316
405
if ( ! ! ( newUser . role == void 0 ) ) {
317
406
throw new Error ( 'Assertion failed' ) ;
318
407
}
319
- function emptyBeforeRequest ( optionsInput : FetchOptionsInput ) : FetchOptions {
320
- const options : FetchOptions = new FetchOptions ( {
321
- url : optionsInput . url ,
322
- method : optionsInput . method ,
323
- params : optionsInput . params ,
324
- headers : optionsInput . headers ,
325
- data : optionsInput . data ,
326
- fileStream : optionsInput . fileStream ,
327
- multipartData : optionsInput . multipartData ,
328
- contentType : optionsInput . contentType ,
329
- responseFormat : optionsInput . responseFormat ,
330
- auth : optionsInput . auth ,
331
- networkSession : optionsInput . networkSession ,
332
- cancellationToken : optionsInput . cancellationToken ,
333
- } ) ;
334
- return options ;
335
- }
336
- function afterRequest ( response : FetchResponse ) : FetchResponse {
337
- return {
338
- status : response . status ,
339
- data : jsonToSerializedData ( '{"id": "123", "type": "user"}' ) ,
340
- content : response . content ,
341
- headers : response . headers ,
342
- } satisfies FetchResponse ;
343
- }
344
408
const clientWithTwoInterceptors : BoxClient =
345
409
clientWithInterceptor . withInterceptors ( [
346
- {
347
- beforeRequest : emptyBeforeRequest ,
348
- afterRequest : afterRequest ,
349
- } satisfies Interceptor ,
410
+ new InterceptorChangingResponse ( { } ) ,
350
411
] ) ;
351
412
const superNewUser : UserFull =
352
413
await clientWithTwoInterceptors . users . getUserMe ( ) ;
@@ -359,36 +420,8 @@ test('testWithFailingInterceptors', async function testWithFailingInterceptors()
359
420
if ( ! ! ( user . id == void 0 ) ) {
360
421
throw new Error ( 'Assertion failed' ) ;
361
422
}
362
- function emptyBeforeRequest ( optionsInput : FetchOptionsInput ) : FetchOptions {
363
- const options : FetchOptions = new FetchOptions ( {
364
- url : optionsInput . url ,
365
- method : optionsInput . method ,
366
- params : optionsInput . params ,
367
- headers : optionsInput . headers ,
368
- data : optionsInput . data ,
369
- fileStream : optionsInput . fileStream ,
370
- multipartData : optionsInput . multipartData ,
371
- contentType : optionsInput . contentType ,
372
- responseFormat : optionsInput . responseFormat ,
373
- auth : optionsInput . auth ,
374
- networkSession : optionsInput . networkSession ,
375
- cancellationToken : optionsInput . cancellationToken ,
376
- } ) ;
377
- return options ;
378
- }
379
- function afterRequest ( response : FetchResponse ) : FetchResponse {
380
- return {
381
- status : 400 ,
382
- data : response . data ,
383
- content : response . content ,
384
- headers : response . headers ,
385
- } satisfies FetchResponse ;
386
- }
387
423
const clientWithInterceptor : BoxClient = client . withInterceptors ( [
388
- {
389
- beforeRequest : emptyBeforeRequest ,
390
- afterRequest : afterRequest ,
391
- } satisfies Interceptor ,
424
+ new InterceptorThrowingError ( { } ) ,
392
425
] ) ;
393
426
await expect ( async ( ) => {
394
427
await clientWithInterceptor . users . getUserMe ( ) ;
0 commit comments