@geekmidas/client 0.4.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/dist/auth-fetcher.cjs +0 -1
- package/dist/auth-fetcher.cjs.map +1 -1
- package/dist/auth-fetcher.d.cts +1 -1
- package/dist/auth-fetcher.d.cts.map +1 -1
- package/dist/auth-fetcher.d.mts +1 -1
- package/dist/auth-fetcher.d.mts.map +1 -1
- package/dist/auth-fetcher.mjs +0 -1
- package/dist/auth-fetcher.mjs.map +1 -1
- package/dist/endpoint-hooks.cjs +10 -5
- package/dist/endpoint-hooks.cjs.map +1 -1
- package/dist/endpoint-hooks.d.cts +2 -2
- package/dist/endpoint-hooks.d.cts.map +1 -1
- package/dist/endpoint-hooks.d.mts +2 -2
- package/dist/endpoint-hooks.d.mts.map +1 -1
- package/dist/endpoint-hooks.mjs +10 -5
- package/dist/endpoint-hooks.mjs.map +1 -1
- package/dist/fetcher-5fnBE7Dk.mjs.map +1 -1
- package/dist/fetcher-CgLEaIAC.cjs.map +1 -1
- package/dist/fetcher.d.cts +1 -1
- package/dist/fetcher.d.cts.map +1 -1
- package/dist/fetcher.d.mts +1 -1
- package/dist/fetcher.d.mts.map +1 -1
- package/dist/infer.d.cts.map +1 -1
- package/dist/infer.d.mts.map +1 -1
- package/dist/openapi-hooks.cjs.map +1 -1
- package/dist/openapi-hooks.d.cts +2 -2
- package/dist/openapi-hooks.d.cts.map +1 -1
- package/dist/openapi-hooks.d.mts +5 -5
- package/dist/openapi-hooks.d.mts.map +1 -1
- package/dist/openapi-hooks.mjs.map +1 -1
- package/dist/openapi-types.d.cts.map +1 -1
- package/dist/openapi-types.d.mts.map +1 -1
- package/dist/openapi.cjs.map +1 -1
- package/dist/openapi.mjs.map +1 -1
- package/dist/react-query.cjs.map +1 -1
- package/dist/react-query.d.cts +1 -1
- package/dist/react-query.d.cts.map +1 -1
- package/dist/react-query.d.mts +8 -8
- package/dist/react-query.d.mts.map +1 -1
- package/dist/react-query.mjs.map +1 -1
- package/dist/{types-FxummKaL.d.mts → types-4K4N-Fl1.d.cts} +2 -2
- package/dist/types-4K4N-Fl1.d.cts.map +1 -0
- package/dist/{types-vutATyWv.d.cts → types-hhkZWjQn.d.mts} +2 -2
- package/dist/types-hhkZWjQn.d.mts.map +1 -0
- package/dist/types.d.cts +1 -1
- package/dist/types.d.mts +1 -1
- package/package.json +5 -5
- package/src/__tests__/auth-fetcher.spec.ts +476 -476
- package/src/__tests__/endpoint-hooks.spec.tsx +348 -348
- package/src/__tests__/fetcher.spec.ts +403 -403
- package/src/__tests__/infer.integration.spec.ts +171 -171
- package/src/__tests__/infer.spec.ts +182 -182
- package/src/__tests__/method-restrictions.spec.tsx +165 -165
- package/src/__tests__/openapi-hooks.spec.tsx +536 -536
- package/src/__tests__/react-query-infinite.spec.tsx +989 -989
- package/src/__tests__/react-query-invalidation.spec.tsx +223 -222
- package/src/__tests__/react-query.spec.tsx +567 -567
- package/src/__tests__/setup.ts +260 -260
- package/src/__tests__/types.spec.ts +127 -127
- package/src/__tests__/url-parsing.spec.ts +191 -191
- package/src/auth-fetcher.ts +160 -162
- package/src/endpoint-hooks.ts +151 -156
- package/src/fetcher.ts +163 -163
- package/src/infer.ts +64 -63
- package/src/openapi-hooks.ts +146 -146
- package/src/openapi-types.d.ts +428 -428
- package/src/openapi.json +593 -593
- package/src/react-query.ts +281 -281
- package/src/types.ts +121 -124
- package/dist/types-FxummKaL.d.mts.map +0 -1
- package/dist/types-vutATyWv.d.cts.map +0 -1
|
@@ -1,505 +1,505 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HttpResponse, http } from 'msw';
|
|
2
2
|
import { setupServer } from 'msw/node';
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
afterAll,
|
|
5
|
+
afterEach,
|
|
6
|
+
beforeAll,
|
|
7
|
+
describe,
|
|
8
|
+
expect,
|
|
9
|
+
it,
|
|
10
|
+
vi,
|
|
11
11
|
} from 'vitest';
|
|
12
12
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
type ApiKeyProvider,
|
|
14
|
+
type AwsSigner,
|
|
15
|
+
createAuthAwareFetcher,
|
|
16
|
+
type SecuritySchemeObject,
|
|
17
|
+
type TokenProvider,
|
|
18
18
|
} from '../auth-fetcher';
|
|
19
19
|
|
|
20
20
|
// Test types - structured to match OpenAPI spec format
|
|
21
21
|
interface TestPaths {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
22
|
+
'/users': {
|
|
23
|
+
get: {
|
|
24
|
+
responses: {
|
|
25
|
+
200: {
|
|
26
|
+
content: {
|
|
27
|
+
'application/json': { users: Array<{ id: string; name: string }> };
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
'/protected': {
|
|
34
|
+
get: {
|
|
35
|
+
responses: {
|
|
36
|
+
200: {
|
|
37
|
+
content: {
|
|
38
|
+
'application/json': { data: string };
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
'/api-key-protected': {
|
|
45
|
+
get: {
|
|
46
|
+
responses: {
|
|
47
|
+
200: {
|
|
48
|
+
content: {
|
|
49
|
+
'application/json': { data: string };
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
'/iam-protected': {
|
|
56
|
+
get: {
|
|
57
|
+
responses: {
|
|
58
|
+
200: {
|
|
59
|
+
content: {
|
|
60
|
+
'application/json': { data: string };
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
'/data': {
|
|
67
|
+
post: {
|
|
68
|
+
requestBody: {
|
|
69
|
+
content: {
|
|
70
|
+
'application/json': { value: string };
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
responses: {
|
|
74
|
+
201: {
|
|
75
|
+
content: {
|
|
76
|
+
'application/json': { id: string };
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
// Track headers received by handlers
|
|
85
85
|
let lastReceivedHeaders: Record<string, string> = {};
|
|
86
86
|
|
|
87
87
|
const handlers = [
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
88
|
+
http.get('https://api.example.com/users', ({ request }) => {
|
|
89
|
+
lastReceivedHeaders = Object.fromEntries(request.headers.entries());
|
|
90
|
+
return HttpResponse.json({
|
|
91
|
+
users: [{ id: '1', name: 'John' }],
|
|
92
|
+
});
|
|
93
|
+
}),
|
|
94
|
+
|
|
95
|
+
http.get('https://api.example.com/protected', ({ request }) => {
|
|
96
|
+
lastReceivedHeaders = Object.fromEntries(request.headers.entries());
|
|
97
|
+
const auth = request.headers.get('Authorization');
|
|
98
|
+
if (!auth || auth !== 'Bearer valid-token') {
|
|
99
|
+
return HttpResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
|
100
|
+
}
|
|
101
|
+
return HttpResponse.json({ data: 'secret' });
|
|
102
|
+
}),
|
|
103
|
+
|
|
104
|
+
http.get('https://api.example.com/api-key-protected', ({ request }) => {
|
|
105
|
+
lastReceivedHeaders = Object.fromEntries(request.headers.entries());
|
|
106
|
+
const apiKey = request.headers.get('X-API-Key');
|
|
107
|
+
if (!apiKey || apiKey !== 'my-api-key') {
|
|
108
|
+
return HttpResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
|
109
|
+
}
|
|
110
|
+
return HttpResponse.json({ data: 'api-key-data' });
|
|
111
|
+
}),
|
|
112
|
+
|
|
113
|
+
http.get('https://api.example.com/iam-protected', ({ request }) => {
|
|
114
|
+
lastReceivedHeaders = Object.fromEntries(request.headers.entries());
|
|
115
|
+
return HttpResponse.json({ data: 'iam-data' });
|
|
116
|
+
}),
|
|
117
|
+
|
|
118
|
+
http.post('https://api.example.com/data', async ({ request }) => {
|
|
119
|
+
lastReceivedHeaders = Object.fromEntries(request.headers.entries());
|
|
120
|
+
return HttpResponse.json({ id: '123' }, { status: 201 });
|
|
121
|
+
}),
|
|
122
122
|
];
|
|
123
123
|
|
|
124
124
|
const server = setupServer(...handlers);
|
|
125
125
|
|
|
126
126
|
beforeAll(() => {
|
|
127
|
-
|
|
127
|
+
server.listen({ onUnhandledRequest: 'error' });
|
|
128
128
|
});
|
|
129
129
|
|
|
130
130
|
afterEach(() => {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
131
|
+
server.resetHandlers();
|
|
132
|
+
lastReceivedHeaders = {};
|
|
133
|
+
vi.clearAllMocks();
|
|
134
134
|
});
|
|
135
135
|
|
|
136
136
|
afterAll(() => {
|
|
137
|
-
|
|
137
|
+
server.close();
|
|
138
138
|
});
|
|
139
139
|
|
|
140
140
|
describe('createAuthAwareFetcher', () => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
141
|
+
const securitySchemes: Record<string, SecuritySchemeObject> = {
|
|
142
|
+
bearer: {
|
|
143
|
+
type: 'http',
|
|
144
|
+
scheme: 'bearer',
|
|
145
|
+
bearerFormat: 'JWT',
|
|
146
|
+
},
|
|
147
|
+
apiKey: {
|
|
148
|
+
type: 'apiKey',
|
|
149
|
+
in: 'header',
|
|
150
|
+
name: 'X-API-Key',
|
|
151
|
+
},
|
|
152
|
+
iam: {
|
|
153
|
+
type: 'http',
|
|
154
|
+
scheme: 'aws4-hmac-sha256',
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
describe('bearer auth', () => {
|
|
159
|
+
it('should add Authorization header for bearer auth endpoints', async () => {
|
|
160
|
+
const tokenProvider: TokenProvider = {
|
|
161
|
+
getValidAccessToken: vi.fn().mockResolvedValue('valid-token'),
|
|
162
|
+
createValidAuthHeaders: vi.fn().mockResolvedValue({
|
|
163
|
+
Authorization: 'Bearer valid-token',
|
|
164
|
+
}),
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const endpointAuth = {
|
|
168
|
+
'GET /users': null,
|
|
169
|
+
'GET /protected': 'bearer',
|
|
170
|
+
} as const;
|
|
171
|
+
|
|
172
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
173
|
+
baseURL: 'https://api.example.com',
|
|
174
|
+
endpointAuth,
|
|
175
|
+
securitySchemes,
|
|
176
|
+
authStrategies: {
|
|
177
|
+
bearer: { type: 'bearer', tokenProvider },
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const result = await api('GET /protected');
|
|
182
|
+
|
|
183
|
+
expect(tokenProvider.createValidAuthHeaders).toHaveBeenCalled();
|
|
184
|
+
expect(result).toEqual({ data: 'secret' });
|
|
185
|
+
expect(lastReceivedHeaders.authorization).toBe('Bearer valid-token');
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('should not add auth headers for public endpoints', async () => {
|
|
189
|
+
const tokenProvider: TokenProvider = {
|
|
190
|
+
getValidAccessToken: vi.fn().mockResolvedValue('valid-token'),
|
|
191
|
+
createValidAuthHeaders: vi.fn().mockResolvedValue({
|
|
192
|
+
Authorization: 'Bearer valid-token',
|
|
193
|
+
}),
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const endpointAuth = {
|
|
197
|
+
'GET /users': null,
|
|
198
|
+
'GET /protected': 'bearer',
|
|
199
|
+
} as const;
|
|
200
|
+
|
|
201
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
202
|
+
baseURL: 'https://api.example.com',
|
|
203
|
+
endpointAuth,
|
|
204
|
+
securitySchemes,
|
|
205
|
+
authStrategies: {
|
|
206
|
+
bearer: { type: 'bearer', tokenProvider },
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const result = await api('GET /users');
|
|
211
|
+
|
|
212
|
+
expect(tokenProvider.createValidAuthHeaders).not.toHaveBeenCalled();
|
|
213
|
+
expect(result).toEqual({ users: [{ id: '1', name: 'John' }] });
|
|
214
|
+
expect(lastReceivedHeaders.authorization).toBeUndefined();
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
describe('apiKey auth', () => {
|
|
219
|
+
it('should add API key header for apiKey auth endpoints', async () => {
|
|
220
|
+
const apiKeyProvider: ApiKeyProvider = {
|
|
221
|
+
getApiKey: vi.fn().mockResolvedValue('my-api-key'),
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const endpointAuth = {
|
|
225
|
+
'GET /api-key-protected': 'apiKey',
|
|
226
|
+
} as const;
|
|
227
|
+
|
|
228
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
229
|
+
baseURL: 'https://api.example.com',
|
|
230
|
+
endpointAuth,
|
|
231
|
+
securitySchemes,
|
|
232
|
+
authStrategies: {
|
|
233
|
+
apiKey: { type: 'apiKey', apiKeyProvider },
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
const result = await api('GET /api-key-protected');
|
|
238
|
+
|
|
239
|
+
expect(apiKeyProvider.getApiKey).toHaveBeenCalled();
|
|
240
|
+
expect(result).toEqual({ data: 'api-key-data' });
|
|
241
|
+
expect(lastReceivedHeaders['x-api-key']).toBe('my-api-key');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('should use custom header name if provided', async () => {
|
|
245
|
+
const apiKeyProvider: ApiKeyProvider = {
|
|
246
|
+
getApiKey: vi.fn().mockResolvedValue('custom-key'),
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const customSchemes: Record<string, SecuritySchemeObject> = {
|
|
250
|
+
customApiKey: {
|
|
251
|
+
type: 'apiKey',
|
|
252
|
+
in: 'header',
|
|
253
|
+
name: 'X-Custom-Key',
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const endpointAuth = {
|
|
258
|
+
'GET /api-key-protected': 'customApiKey',
|
|
259
|
+
} as const;
|
|
260
|
+
|
|
261
|
+
server.use(
|
|
262
|
+
http.get('https://api.example.com/api-key-protected', ({ request }) => {
|
|
263
|
+
lastReceivedHeaders = Object.fromEntries(request.headers.entries());
|
|
264
|
+
return HttpResponse.json({ data: 'custom-key-data' });
|
|
265
|
+
}),
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
269
|
+
baseURL: 'https://api.example.com',
|
|
270
|
+
endpointAuth,
|
|
271
|
+
securitySchemes: customSchemes,
|
|
272
|
+
authStrategies: {
|
|
273
|
+
customApiKey: {
|
|
274
|
+
type: 'apiKey',
|
|
275
|
+
apiKeyProvider,
|
|
276
|
+
headerName: 'X-Custom-Key',
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
await api('GET /api-key-protected');
|
|
282
|
+
|
|
283
|
+
expect(lastReceivedHeaders['x-custom-key']).toBe('custom-key');
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('should handle synchronous getApiKey', async () => {
|
|
287
|
+
const apiKeyProvider: ApiKeyProvider = {
|
|
288
|
+
getApiKey: vi.fn().mockReturnValue('sync-api-key'),
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
server.use(
|
|
292
|
+
http.get('https://api.example.com/api-key-protected', ({ request }) => {
|
|
293
|
+
lastReceivedHeaders = Object.fromEntries(request.headers.entries());
|
|
294
|
+
return HttpResponse.json({ data: 'sync-data' });
|
|
295
|
+
}),
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
const endpointAuth = {
|
|
299
|
+
'GET /api-key-protected': 'apiKey',
|
|
300
|
+
} as const;
|
|
301
|
+
|
|
302
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
303
|
+
baseURL: 'https://api.example.com',
|
|
304
|
+
endpointAuth,
|
|
305
|
+
securitySchemes,
|
|
306
|
+
authStrategies: {
|
|
307
|
+
apiKey: { type: 'apiKey', apiKeyProvider },
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
await api('GET /api-key-protected');
|
|
312
|
+
|
|
313
|
+
expect(lastReceivedHeaders['x-api-key']).toBe('sync-api-key');
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
describe('IAM auth', () => {
|
|
318
|
+
it('should call signer for IAM auth endpoints', async () => {
|
|
319
|
+
const signer: AwsSigner = {
|
|
320
|
+
sign: vi.fn().mockResolvedValue({}),
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const endpointAuth = {
|
|
324
|
+
'GET /iam-protected': 'iam',
|
|
325
|
+
} as const;
|
|
326
|
+
|
|
327
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
328
|
+
baseURL: 'https://api.example.com',
|
|
329
|
+
endpointAuth,
|
|
330
|
+
securitySchemes,
|
|
331
|
+
authStrategies: {
|
|
332
|
+
iam: { type: 'iam', signer },
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
const result = await api('GET /iam-protected');
|
|
337
|
+
|
|
338
|
+
// IAM returns empty headers in current implementation
|
|
339
|
+
expect(result).toEqual({ data: 'iam-data' });
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
describe('header merging', () => {
|
|
344
|
+
it('should merge auth headers with user-provided headers', async () => {
|
|
345
|
+
const tokenProvider: TokenProvider = {
|
|
346
|
+
getValidAccessToken: vi.fn().mockResolvedValue('valid-token'),
|
|
347
|
+
createValidAuthHeaders: vi.fn().mockResolvedValue({
|
|
348
|
+
Authorization: 'Bearer valid-token',
|
|
349
|
+
}),
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const endpointAuth = {
|
|
353
|
+
'GET /protected': 'bearer',
|
|
354
|
+
} as const;
|
|
355
|
+
|
|
356
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
357
|
+
baseURL: 'https://api.example.com',
|
|
358
|
+
endpointAuth,
|
|
359
|
+
securitySchemes,
|
|
360
|
+
authStrategies: {
|
|
361
|
+
bearer: { type: 'bearer', tokenProvider },
|
|
362
|
+
},
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
await api('GET /protected', {
|
|
366
|
+
headers: { 'X-Custom-Header': 'custom-value' },
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
expect(lastReceivedHeaders.authorization).toBe('Bearer valid-token');
|
|
370
|
+
expect(lastReceivedHeaders['x-custom-header']).toBe('custom-value');
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it('should allow user headers to override auth headers', async () => {
|
|
374
|
+
const tokenProvider: TokenProvider = {
|
|
375
|
+
getValidAccessToken: vi.fn().mockResolvedValue('valid-token'),
|
|
376
|
+
createValidAuthHeaders: vi.fn().mockResolvedValue({
|
|
377
|
+
Authorization: 'Bearer valid-token',
|
|
378
|
+
}),
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
// Override handler to accept any valid Bearer token
|
|
382
|
+
server.use(
|
|
383
|
+
http.get('https://api.example.com/protected', ({ request }) => {
|
|
384
|
+
lastReceivedHeaders = Object.fromEntries(request.headers.entries());
|
|
385
|
+
const auth = request.headers.get('Authorization');
|
|
386
|
+
if (!auth || !auth.startsWith('Bearer ')) {
|
|
387
|
+
return HttpResponse.json(
|
|
388
|
+
{ message: 'Unauthorized' },
|
|
389
|
+
{ status: 401 },
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
return HttpResponse.json({ data: 'secret' });
|
|
393
|
+
}),
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
const endpointAuth = {
|
|
397
|
+
'GET /protected': 'bearer',
|
|
398
|
+
} as const;
|
|
399
|
+
|
|
400
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
401
|
+
baseURL: 'https://api.example.com',
|
|
402
|
+
endpointAuth,
|
|
403
|
+
securitySchemes,
|
|
404
|
+
authStrategies: {
|
|
405
|
+
bearer: { type: 'bearer', tokenProvider },
|
|
406
|
+
},
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
await api('GET /protected', {
|
|
410
|
+
headers: { Authorization: 'Bearer override-token' },
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
expect(lastReceivedHeaders.authorization).toBe('Bearer override-token');
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
describe('onRequest interceptor', () => {
|
|
418
|
+
it('should call user onRequest interceptor after auth headers', async () => {
|
|
419
|
+
const onRequest = vi.fn((config: RequestInit) => ({
|
|
420
|
+
...config,
|
|
421
|
+
headers: {
|
|
422
|
+
...config.headers,
|
|
423
|
+
'X-Intercepted': 'true',
|
|
424
|
+
},
|
|
425
|
+
}));
|
|
426
|
+
|
|
427
|
+
const tokenProvider: TokenProvider = {
|
|
428
|
+
getValidAccessToken: vi.fn().mockResolvedValue('valid-token'),
|
|
429
|
+
createValidAuthHeaders: vi.fn().mockResolvedValue({
|
|
430
|
+
Authorization: 'Bearer valid-token',
|
|
431
|
+
}),
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
const endpointAuth = {
|
|
435
|
+
'GET /protected': 'bearer',
|
|
436
|
+
} as const;
|
|
437
|
+
|
|
438
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
439
|
+
baseURL: 'https://api.example.com',
|
|
440
|
+
endpointAuth,
|
|
441
|
+
securitySchemes,
|
|
442
|
+
authStrategies: {
|
|
443
|
+
bearer: { type: 'bearer', tokenProvider },
|
|
444
|
+
},
|
|
445
|
+
onRequest,
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
await api('GET /protected');
|
|
449
|
+
|
|
450
|
+
expect(onRequest).toHaveBeenCalled();
|
|
451
|
+
expect(lastReceivedHeaders['x-intercepted']).toBe('true');
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
describe('none auth strategy', () => {
|
|
456
|
+
it('should not add any headers for none strategy', async () => {
|
|
457
|
+
const endpointAuth = {
|
|
458
|
+
'GET /users': 'noAuth',
|
|
459
|
+
} as const;
|
|
460
|
+
|
|
461
|
+
const customSchemes: Record<string, SecuritySchemeObject> = {
|
|
462
|
+
noAuth: {
|
|
463
|
+
type: 'http',
|
|
464
|
+
scheme: 'none',
|
|
465
|
+
},
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
469
|
+
baseURL: 'https://api.example.com',
|
|
470
|
+
endpointAuth,
|
|
471
|
+
securitySchemes: customSchemes,
|
|
472
|
+
authStrategies: {
|
|
473
|
+
noAuth: { type: 'none' },
|
|
474
|
+
},
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
const result = await api('GET /users');
|
|
478
|
+
|
|
479
|
+
expect(result).toEqual({ users: [{ id: '1', name: 'John' }] });
|
|
480
|
+
expect(lastReceivedHeaders.authorization).toBeUndefined();
|
|
481
|
+
});
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
describe('missing strategy handling', () => {
|
|
485
|
+
it('should not add headers when scheme exists but strategy is missing', async () => {
|
|
486
|
+
const endpointAuth = {
|
|
487
|
+
'GET /users': 'unknownScheme',
|
|
488
|
+
} as const;
|
|
489
|
+
|
|
490
|
+
const api = createAuthAwareFetcher<TestPaths>({
|
|
491
|
+
baseURL: 'https://api.example.com',
|
|
492
|
+
endpointAuth,
|
|
493
|
+
securitySchemes: {
|
|
494
|
+
unknownScheme: { type: 'http', scheme: 'custom' },
|
|
495
|
+
},
|
|
496
|
+
// No strategy for unknownScheme
|
|
497
|
+
authStrategies: {} as any,
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
const result = await api('GET /users');
|
|
501
|
+
|
|
502
|
+
expect(result).toEqual({ users: [{ id: '1', name: 'John' }] });
|
|
503
|
+
});
|
|
504
|
+
});
|
|
505
505
|
});
|