@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.
Files changed (72) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/auth-fetcher.cjs +0 -1
  3. package/dist/auth-fetcher.cjs.map +1 -1
  4. package/dist/auth-fetcher.d.cts +1 -1
  5. package/dist/auth-fetcher.d.cts.map +1 -1
  6. package/dist/auth-fetcher.d.mts +1 -1
  7. package/dist/auth-fetcher.d.mts.map +1 -1
  8. package/dist/auth-fetcher.mjs +0 -1
  9. package/dist/auth-fetcher.mjs.map +1 -1
  10. package/dist/endpoint-hooks.cjs +10 -5
  11. package/dist/endpoint-hooks.cjs.map +1 -1
  12. package/dist/endpoint-hooks.d.cts +2 -2
  13. package/dist/endpoint-hooks.d.cts.map +1 -1
  14. package/dist/endpoint-hooks.d.mts +2 -2
  15. package/dist/endpoint-hooks.d.mts.map +1 -1
  16. package/dist/endpoint-hooks.mjs +10 -5
  17. package/dist/endpoint-hooks.mjs.map +1 -1
  18. package/dist/fetcher-5fnBE7Dk.mjs.map +1 -1
  19. package/dist/fetcher-CgLEaIAC.cjs.map +1 -1
  20. package/dist/fetcher.d.cts +1 -1
  21. package/dist/fetcher.d.cts.map +1 -1
  22. package/dist/fetcher.d.mts +1 -1
  23. package/dist/fetcher.d.mts.map +1 -1
  24. package/dist/infer.d.cts.map +1 -1
  25. package/dist/infer.d.mts.map +1 -1
  26. package/dist/openapi-hooks.cjs.map +1 -1
  27. package/dist/openapi-hooks.d.cts +2 -2
  28. package/dist/openapi-hooks.d.cts.map +1 -1
  29. package/dist/openapi-hooks.d.mts +5 -5
  30. package/dist/openapi-hooks.d.mts.map +1 -1
  31. package/dist/openapi-hooks.mjs.map +1 -1
  32. package/dist/openapi-types.d.cts.map +1 -1
  33. package/dist/openapi-types.d.mts.map +1 -1
  34. package/dist/openapi.cjs.map +1 -1
  35. package/dist/openapi.mjs.map +1 -1
  36. package/dist/react-query.cjs.map +1 -1
  37. package/dist/react-query.d.cts +1 -1
  38. package/dist/react-query.d.cts.map +1 -1
  39. package/dist/react-query.d.mts +8 -8
  40. package/dist/react-query.d.mts.map +1 -1
  41. package/dist/react-query.mjs.map +1 -1
  42. package/dist/{types-FxummKaL.d.mts → types-4K4N-Fl1.d.cts} +2 -2
  43. package/dist/types-4K4N-Fl1.d.cts.map +1 -0
  44. package/dist/{types-vutATyWv.d.cts → types-hhkZWjQn.d.mts} +2 -2
  45. package/dist/types-hhkZWjQn.d.mts.map +1 -0
  46. package/dist/types.d.cts +1 -1
  47. package/dist/types.d.mts +1 -1
  48. package/package.json +5 -5
  49. package/src/__tests__/auth-fetcher.spec.ts +476 -476
  50. package/src/__tests__/endpoint-hooks.spec.tsx +348 -348
  51. package/src/__tests__/fetcher.spec.ts +403 -403
  52. package/src/__tests__/infer.integration.spec.ts +171 -171
  53. package/src/__tests__/infer.spec.ts +182 -182
  54. package/src/__tests__/method-restrictions.spec.tsx +165 -165
  55. package/src/__tests__/openapi-hooks.spec.tsx +536 -536
  56. package/src/__tests__/react-query-infinite.spec.tsx +989 -989
  57. package/src/__tests__/react-query-invalidation.spec.tsx +223 -222
  58. package/src/__tests__/react-query.spec.tsx +567 -567
  59. package/src/__tests__/setup.ts +260 -260
  60. package/src/__tests__/types.spec.ts +127 -127
  61. package/src/__tests__/url-parsing.spec.ts +191 -191
  62. package/src/auth-fetcher.ts +160 -162
  63. package/src/endpoint-hooks.ts +151 -156
  64. package/src/fetcher.ts +163 -163
  65. package/src/infer.ts +64 -63
  66. package/src/openapi-hooks.ts +146 -146
  67. package/src/openapi-types.d.ts +428 -428
  68. package/src/openapi.json +593 -593
  69. package/src/react-query.ts +281 -281
  70. package/src/types.ts +121 -124
  71. package/dist/types-FxummKaL.d.mts.map +0 -1
  72. package/dist/types-vutATyWv.d.cts.map +0 -1
@@ -1,409 +1,409 @@
1
1
  import { describe, expect, it, vi } from 'vitest';
2
- import { type FetchFn, createTypedFetcher } from '../fetcher';
2
+ import { createTypedFetcher, type FetchFn } from '../fetcher';
3
3
  import type { paths } from '../openapi-types';
4
4
  import './setup';
5
5
 
6
6
  describe('TypedFetcher', () => {
7
- it('should make GET request to fetch users', async () => {
8
- const client = createTypedFetcher<paths>({
9
- baseURL: 'https://api.example.com',
10
- });
11
-
12
- const result = await client('GET /users');
13
-
14
- expect(result).toEqual({
15
- users: [
16
- { id: '1', name: 'John Doe', email: 'john@example.com' },
17
- { id: '2', name: 'Jane Smith', email: 'jane@example.com' },
18
- ],
19
- });
20
- });
21
-
22
- it('should make GET request with path params', async () => {
23
- const client = createTypedFetcher<paths>({
24
- baseURL: 'https://api.example.com',
25
- });
26
-
27
- const result = await client('GET /users/{id}', { params: { id: '123' } });
28
-
29
- expect(result).toEqual({
30
- id: '123',
31
- name: 'John Doe',
32
- email: 'john@example.com',
33
- });
34
- });
35
-
36
- it('should make POST request with body', async () => {
37
- const client = createTypedFetcher<paths>({
38
- baseURL: 'https://api.example.com',
39
- });
40
-
41
- const requestBody = { name: 'Jane Doe', email: 'jane@example.com' };
42
- const result = await client('POST /users', { body: requestBody });
43
-
44
- expect(result).toEqual({
45
- id: '123',
46
- name: 'Jane Doe',
47
- email: 'jane@example.com',
48
- });
49
- });
50
-
51
- it('should make PUT request with path params and body', async () => {
52
- const client = createTypedFetcher<paths>({
53
- baseURL: 'https://api.example.com',
54
- });
55
-
56
- const result = await client('PUT /users/{id}', {
57
- params: { id: '456' },
58
- body: { name: 'Updated Name' },
59
- });
60
-
61
- expect(result).toEqual({
62
- id: '456',
63
- name: 'Updated Name',
64
- email: 'john@example.com',
65
- });
66
- });
67
-
68
- it('should handle DELETE requests', async () => {
69
- const client = createTypedFetcher<paths>({
70
- baseURL: 'https://api.example.com',
71
- });
72
-
73
- // DELETE returns no content (204)
74
- const result = await client('DELETE /users/{id}', {
75
- params: { id: '123' },
76
- });
77
-
78
- // For 204 responses, result should be undefined or empty
79
- expect(result).toBeUndefined();
80
- });
81
-
82
- it('should handle query parameters', async () => {
83
- const client = createTypedFetcher<paths>({
84
- baseURL: 'https://api.example.com',
85
- });
86
-
87
- const result = await client('GET /posts', {
88
- query: { page: 2, limit: 5, sort: 'desc' },
89
- });
90
-
91
- expect(result.posts).toHaveLength(5);
92
- });
93
-
94
- it('should handle array query parameters', async () => {
95
- // Mock fetch to capture the request URL
96
- const mockFetch = vi.fn().mockResolvedValue({
97
- ok: true,
98
- status: 200,
99
- headers: new Headers({ 'content-type': 'application/json' }),
100
- json: async () => ({ items: ['a', 'b', 'c'] }),
101
- });
102
-
103
- const client = createTypedFetcher<any>({
104
- baseURL: 'https://api.example.com',
105
- fetch: mockFetch,
106
- });
107
-
108
- await client('GET /search', {
109
- query: { tags: ['nodejs', 'typescript', 'javascript'] as any },
110
- });
111
-
112
- expect(mockFetch).toHaveBeenCalledWith(
113
- 'https://api.example.com/search?tags=nodejs&tags=typescript&tags=javascript',
114
- expect.any(Object),
115
- );
116
- });
117
-
118
- it('should handle object query parameters with dot notation', async () => {
119
- // Mock fetch to capture the request URL
120
- const mockFetch = vi.fn().mockResolvedValue({
121
- ok: true,
122
- status: 200,
123
- headers: new Headers({ 'content-type': 'application/json' }),
124
- json: async () => ({ results: [] }),
125
- });
126
-
127
- const client = createTypedFetcher<any>({
128
- baseURL: 'https://api.example.com',
129
- fetch: mockFetch,
130
- });
131
-
132
- await client('GET /products', {
133
- query: {
134
- filter: {
135
- category: 'electronics',
136
- minPrice: 100,
137
- maxPrice: 500,
138
- },
139
- sort: 'price',
140
- } as any,
141
- });
142
-
143
- expect(mockFetch).toHaveBeenCalledWith(
144
- 'https://api.example.com/products?filter.category=electronics&filter.minPrice=100&filter.maxPrice=500&sort=price',
145
- expect.any(Object),
146
- );
147
- });
148
-
149
- it('should handle arrays within nested objects', async () => {
150
- // Mock fetch to capture the request URL
151
- const mockFetch = vi.fn().mockResolvedValue({
152
- ok: true,
153
- status: 200,
154
- headers: new Headers({ 'content-type': 'application/json' }),
155
- json: async () => ({ results: [] }),
156
- });
157
-
158
- const client = createTypedFetcher<any>({
159
- baseURL: 'https://api.example.com',
160
- fetch: mockFetch,
161
- });
162
-
163
- await client('GET /advanced-search', {
164
- query: {
165
- user: {
166
- roles: ['admin', 'moderator', 'user'],
167
- status: 'active',
168
- },
169
- settings: {
170
- notifications: {
171
- types: ['email', 'sms', 'push'],
172
- enabled: true,
173
- },
174
- },
175
- } as any,
176
- });
177
-
178
- expect(mockFetch).toHaveBeenCalledWith(
179
- 'https://api.example.com/advanced-search?user.roles=admin&user.roles=moderator&user.roles=user&user.status=active&settings.notifications.types=email&settings.notifications.types=sms&settings.notifications.types=push&settings.notifications.enabled=true',
180
- expect.any(Object),
181
- );
182
- });
183
-
184
- it('should handle 404 errors', async () => {
185
- const client = createTypedFetcher<paths>({
186
- baseURL: 'https://api.example.com',
187
- });
188
-
189
- try {
190
- await client('GET /users/{id}', {
191
- params: { id: '404' },
192
- });
193
- expect.fail('Should have thrown an error');
194
- } catch (error) {
195
- expect(error).toBeInstanceOf(Response);
196
- const response = error as Response;
197
- expect(response.status).toBe(404);
198
- const data = await response.json();
199
- expect(data.message).toBe('User not found');
200
- }
201
- });
202
-
203
- it('should handle 500 errors', async () => {
204
- const client = createTypedFetcher<paths>({
205
- baseURL: 'https://api.example.com',
206
- });
207
-
208
- try {
209
- await client('GET /error');
210
- expect.fail('Should have thrown an error');
211
- } catch (error) {
212
- expect(error).toBeInstanceOf(Response);
213
- const response = error as Response;
214
- expect(response.status).toBe(500);
215
- const data = await response.json();
216
- expect(data.message).toBe('Internal server error');
217
- }
218
- });
219
-
220
- it('should apply request interceptor', async () => {
221
- const onRequest = vi.fn((config) => ({
222
- ...config,
223
- headers: { ...config.headers, 'X-Custom-Header': 'test-value' },
224
- }));
225
-
226
- const client = createTypedFetcher<paths>({
227
- baseURL: 'https://api.example.com',
228
- onRequest,
229
- });
230
-
231
- await client('GET /users/{id}', { params: { id: '123' } });
232
-
233
- expect(onRequest).toHaveBeenCalledWith(
234
- expect.objectContaining({
235
- method: 'GET',
236
- headers: expect.any(Object),
237
- }),
238
- );
239
- });
240
-
241
- it('should apply response interceptor', async () => {
242
- const onResponse = vi.fn((response) => response);
243
-
244
- const client = createTypedFetcher<paths>({
245
- baseURL: 'https://api.example.com',
246
- onResponse,
247
- });
248
-
249
- await client('GET /users/{id}', { params: { id: '123' } });
250
-
251
- expect(onResponse).toHaveBeenCalledWith(expect.any(Response));
252
- });
253
-
254
- it('should apply error handler', async () => {
255
- const onError = vi.fn();
256
-
257
- const client = createTypedFetcher<paths>({
258
- baseURL: 'https://api.example.com',
259
- onError,
260
- });
261
-
262
- await expect(
263
- client('GET /users/{id}', { params: { id: '404' } }),
264
- ).rejects.toThrow();
265
-
266
- expect(onError).toHaveBeenCalledWith(expect.any(Response));
267
- });
268
-
269
- it('should merge default headers with request headers', async () => {
270
- const client = createTypedFetcher<paths>({
271
- baseURL: 'https://api.example.com',
272
- headers: { Authorization: 'Bearer valid-token' },
273
- });
274
-
275
- const result = await client('GET /protected', {
276
- headers: { 'X-Custom': 'value' },
277
- });
278
-
279
- expect(result).toEqual({ message: 'Protected data' });
280
- });
281
-
282
- it('should handle unauthorized requests', async () => {
283
- const client = createTypedFetcher<paths>({
284
- baseURL: 'https://api.example.com',
285
- headers: { Authorization: 'Bearer broken-token' },
286
- });
287
-
288
- try {
289
- await client('GET /protected');
290
- expect.fail('Should have thrown an error');
291
- } catch (error) {
292
- expect(error).toBeInstanceOf(Response);
293
- const response = error as Response;
294
- expect(response.status).toBe(401);
295
- const data = await response.json();
296
- expect(data.message).toBe('Unauthorized');
297
- }
298
- });
299
-
300
- it('should handle empty query parameters', async () => {
301
- const client = createTypedFetcher<paths>({
302
- baseURL: 'https://api.example.com',
303
- });
304
-
305
- const result = await client('GET /posts', {
306
- query: { page: 1, sort: undefined as any },
307
- });
308
-
309
- expect(result.pagination.page).toBe(1);
310
- expect(result.sort).toBe('asc'); // Default value when sort is undefined
311
- });
312
-
313
- it('should properly encode path parameters', async () => {
314
- const client = createTypedFetcher<paths>({
315
- baseURL: 'https://api.example.com',
316
- });
317
-
318
- const result = await client('GET /users/{id}', {
319
- params: { id: 'user with spaces' },
320
- });
321
-
322
- // MSW decodes the URL parameters, so we get the original value back
323
- expect(result).toEqual({
324
- id: 'user with spaces',
325
- name: 'John Doe',
326
- email: 'john@example.com',
327
- });
328
- });
329
-
330
- it('should correctly substitute multiple path parameters', async () => {
331
- // Add a mock handler for this test
332
- const mockFetch = vi.fn(async (url: string) => {
333
- if (url === 'https://api.example.com/posts/123/comments/456') {
334
- return new Response(
335
- JSON.stringify({
336
- postId: '123',
337
- commentId: '456',
338
- content: 'Test comment',
339
- }),
340
- { status: 200, headers: { 'Content-Type': 'application/json' } },
341
- );
342
- }
343
- return new Response('Not found', { status: 404 });
344
- }) as FetchFn;
345
-
346
- const client = createTypedFetcher<paths>({
347
- baseURL: 'https://api.example.com',
348
- fetch: mockFetch,
349
- });
350
-
351
- const result = await client(
352
- 'GET /posts/{postId}/comments/{commentId}' as any,
353
- {
354
- params: { postId: '123', commentId: '456' },
355
- },
356
- );
357
-
358
- expect(mockFetch).toHaveBeenCalledWith(
359
- 'https://api.example.com/posts/123/comments/456',
360
- expect.any(Object),
361
- );
362
- expect(result).toEqual({
363
- postId: '123',
364
- commentId: '456',
365
- content: 'Test comment',
366
- });
367
- });
368
-
369
- it('should URL encode special characters in path parameters', async () => {
370
- // Mock fetch to verify the actual URL being called
371
- const mockFetch = vi.fn(async (url: string) => {
372
- if (
373
- url ===
374
- 'https://api.example.com/users/user%20with%20spaces%2Fand%2Fslashes'
375
- ) {
376
- return new Response(
377
- JSON.stringify({
378
- id: 'user with spaces/and/slashes',
379
- name: 'Test User',
380
- email: 'test@example.com',
381
- }),
382
- { status: 200, headers: { 'Content-Type': 'application/json' } },
383
- );
384
- }
385
- return new Response('Not found', { status: 404 });
386
- }) as typeof fetch;
387
-
388
- const client = createTypedFetcher<paths>({
389
- baseURL: 'https://api.example.com',
390
- fetch: mockFetch,
391
- });
392
-
393
- const result = await client('GET /users/{id}', {
394
- params: { id: 'user with spaces/and/slashes' },
395
- });
396
-
397
- // Verify the URL was properly encoded
398
- expect(mockFetch).toHaveBeenCalledWith(
399
- 'https://api.example.com/users/user%20with%20spaces%2Fand%2Fslashes',
400
- expect.any(Object),
401
- );
402
-
403
- expect(result).toEqual({
404
- id: 'user with spaces/and/slashes',
405
- name: 'Test User',
406
- email: 'test@example.com',
407
- });
408
- });
7
+ it('should make GET request to fetch users', async () => {
8
+ const client = createTypedFetcher<paths>({
9
+ baseURL: 'https://api.example.com',
10
+ });
11
+
12
+ const result = await client('GET /users');
13
+
14
+ expect(result).toEqual({
15
+ users: [
16
+ { id: '1', name: 'John Doe', email: 'john@example.com' },
17
+ { id: '2', name: 'Jane Smith', email: 'jane@example.com' },
18
+ ],
19
+ });
20
+ });
21
+
22
+ it('should make GET request with path params', async () => {
23
+ const client = createTypedFetcher<paths>({
24
+ baseURL: 'https://api.example.com',
25
+ });
26
+
27
+ const result = await client('GET /users/{id}', { params: { id: '123' } });
28
+
29
+ expect(result).toEqual({
30
+ id: '123',
31
+ name: 'John Doe',
32
+ email: 'john@example.com',
33
+ });
34
+ });
35
+
36
+ it('should make POST request with body', async () => {
37
+ const client = createTypedFetcher<paths>({
38
+ baseURL: 'https://api.example.com',
39
+ });
40
+
41
+ const requestBody = { name: 'Jane Doe', email: 'jane@example.com' };
42
+ const result = await client('POST /users', { body: requestBody });
43
+
44
+ expect(result).toEqual({
45
+ id: '123',
46
+ name: 'Jane Doe',
47
+ email: 'jane@example.com',
48
+ });
49
+ });
50
+
51
+ it('should make PUT request with path params and body', async () => {
52
+ const client = createTypedFetcher<paths>({
53
+ baseURL: 'https://api.example.com',
54
+ });
55
+
56
+ const result = await client('PUT /users/{id}', {
57
+ params: { id: '456' },
58
+ body: { name: 'Updated Name' },
59
+ });
60
+
61
+ expect(result).toEqual({
62
+ id: '456',
63
+ name: 'Updated Name',
64
+ email: 'john@example.com',
65
+ });
66
+ });
67
+
68
+ it('should handle DELETE requests', async () => {
69
+ const client = createTypedFetcher<paths>({
70
+ baseURL: 'https://api.example.com',
71
+ });
72
+
73
+ // DELETE returns no content (204)
74
+ const result = await client('DELETE /users/{id}', {
75
+ params: { id: '123' },
76
+ });
77
+
78
+ // For 204 responses, result should be undefined or empty
79
+ expect(result).toBeUndefined();
80
+ });
81
+
82
+ it('should handle query parameters', async () => {
83
+ const client = createTypedFetcher<paths>({
84
+ baseURL: 'https://api.example.com',
85
+ });
86
+
87
+ const result = await client('GET /posts', {
88
+ query: { page: 2, limit: 5, sort: 'desc' },
89
+ });
90
+
91
+ expect(result.posts).toHaveLength(5);
92
+ });
93
+
94
+ it('should handle array query parameters', async () => {
95
+ // Mock fetch to capture the request URL
96
+ const mockFetch = vi.fn().mockResolvedValue({
97
+ ok: true,
98
+ status: 200,
99
+ headers: new Headers({ 'content-type': 'application/json' }),
100
+ json: async () => ({ items: ['a', 'b', 'c'] }),
101
+ });
102
+
103
+ const client = createTypedFetcher<any>({
104
+ baseURL: 'https://api.example.com',
105
+ fetch: mockFetch,
106
+ });
107
+
108
+ await client('GET /search', {
109
+ query: { tags: ['nodejs', 'typescript', 'javascript'] as any },
110
+ });
111
+
112
+ expect(mockFetch).toHaveBeenCalledWith(
113
+ 'https://api.example.com/search?tags=nodejs&tags=typescript&tags=javascript',
114
+ expect.any(Object),
115
+ );
116
+ });
117
+
118
+ it('should handle object query parameters with dot notation', async () => {
119
+ // Mock fetch to capture the request URL
120
+ const mockFetch = vi.fn().mockResolvedValue({
121
+ ok: true,
122
+ status: 200,
123
+ headers: new Headers({ 'content-type': 'application/json' }),
124
+ json: async () => ({ results: [] }),
125
+ });
126
+
127
+ const client = createTypedFetcher<any>({
128
+ baseURL: 'https://api.example.com',
129
+ fetch: mockFetch,
130
+ });
131
+
132
+ await client('GET /products', {
133
+ query: {
134
+ filter: {
135
+ category: 'electronics',
136
+ minPrice: 100,
137
+ maxPrice: 500,
138
+ },
139
+ sort: 'price',
140
+ } as any,
141
+ });
142
+
143
+ expect(mockFetch).toHaveBeenCalledWith(
144
+ 'https://api.example.com/products?filter.category=electronics&filter.minPrice=100&filter.maxPrice=500&sort=price',
145
+ expect.any(Object),
146
+ );
147
+ });
148
+
149
+ it('should handle arrays within nested objects', async () => {
150
+ // Mock fetch to capture the request URL
151
+ const mockFetch = vi.fn().mockResolvedValue({
152
+ ok: true,
153
+ status: 200,
154
+ headers: new Headers({ 'content-type': 'application/json' }),
155
+ json: async () => ({ results: [] }),
156
+ });
157
+
158
+ const client = createTypedFetcher<any>({
159
+ baseURL: 'https://api.example.com',
160
+ fetch: mockFetch,
161
+ });
162
+
163
+ await client('GET /advanced-search', {
164
+ query: {
165
+ user: {
166
+ roles: ['admin', 'moderator', 'user'],
167
+ status: 'active',
168
+ },
169
+ settings: {
170
+ notifications: {
171
+ types: ['email', 'sms', 'push'],
172
+ enabled: true,
173
+ },
174
+ },
175
+ } as any,
176
+ });
177
+
178
+ expect(mockFetch).toHaveBeenCalledWith(
179
+ 'https://api.example.com/advanced-search?user.roles=admin&user.roles=moderator&user.roles=user&user.status=active&settings.notifications.types=email&settings.notifications.types=sms&settings.notifications.types=push&settings.notifications.enabled=true',
180
+ expect.any(Object),
181
+ );
182
+ });
183
+
184
+ it('should handle 404 errors', async () => {
185
+ const client = createTypedFetcher<paths>({
186
+ baseURL: 'https://api.example.com',
187
+ });
188
+
189
+ try {
190
+ await client('GET /users/{id}', {
191
+ params: { id: '404' },
192
+ });
193
+ expect.fail('Should have thrown an error');
194
+ } catch (error) {
195
+ expect(error).toBeInstanceOf(Response);
196
+ const response = error as Response;
197
+ expect(response.status).toBe(404);
198
+ const data = await response.json();
199
+ expect(data.message).toBe('User not found');
200
+ }
201
+ });
202
+
203
+ it('should handle 500 errors', async () => {
204
+ const client = createTypedFetcher<paths>({
205
+ baseURL: 'https://api.example.com',
206
+ });
207
+
208
+ try {
209
+ await client('GET /error');
210
+ expect.fail('Should have thrown an error');
211
+ } catch (error) {
212
+ expect(error).toBeInstanceOf(Response);
213
+ const response = error as Response;
214
+ expect(response.status).toBe(500);
215
+ const data = await response.json();
216
+ expect(data.message).toBe('Internal server error');
217
+ }
218
+ });
219
+
220
+ it('should apply request interceptor', async () => {
221
+ const onRequest = vi.fn((config) => ({
222
+ ...config,
223
+ headers: { ...config.headers, 'X-Custom-Header': 'test-value' },
224
+ }));
225
+
226
+ const client = createTypedFetcher<paths>({
227
+ baseURL: 'https://api.example.com',
228
+ onRequest,
229
+ });
230
+
231
+ await client('GET /users/{id}', { params: { id: '123' } });
232
+
233
+ expect(onRequest).toHaveBeenCalledWith(
234
+ expect.objectContaining({
235
+ method: 'GET',
236
+ headers: expect.any(Object),
237
+ }),
238
+ );
239
+ });
240
+
241
+ it('should apply response interceptor', async () => {
242
+ const onResponse = vi.fn((response) => response);
243
+
244
+ const client = createTypedFetcher<paths>({
245
+ baseURL: 'https://api.example.com',
246
+ onResponse,
247
+ });
248
+
249
+ await client('GET /users/{id}', { params: { id: '123' } });
250
+
251
+ expect(onResponse).toHaveBeenCalledWith(expect.any(Response));
252
+ });
253
+
254
+ it('should apply error handler', async () => {
255
+ const onError = vi.fn();
256
+
257
+ const client = createTypedFetcher<paths>({
258
+ baseURL: 'https://api.example.com',
259
+ onError,
260
+ });
261
+
262
+ await expect(
263
+ client('GET /users/{id}', { params: { id: '404' } }),
264
+ ).rejects.toThrow();
265
+
266
+ expect(onError).toHaveBeenCalledWith(expect.any(Response));
267
+ });
268
+
269
+ it('should merge default headers with request headers', async () => {
270
+ const client = createTypedFetcher<paths>({
271
+ baseURL: 'https://api.example.com',
272
+ headers: { Authorization: 'Bearer valid-token' },
273
+ });
274
+
275
+ const result = await client('GET /protected', {
276
+ headers: { 'X-Custom': 'value' },
277
+ });
278
+
279
+ expect(result).toEqual({ message: 'Protected data' });
280
+ });
281
+
282
+ it('should handle unauthorized requests', async () => {
283
+ const client = createTypedFetcher<paths>({
284
+ baseURL: 'https://api.example.com',
285
+ headers: { Authorization: 'Bearer broken-token' },
286
+ });
287
+
288
+ try {
289
+ await client('GET /protected');
290
+ expect.fail('Should have thrown an error');
291
+ } catch (error) {
292
+ expect(error).toBeInstanceOf(Response);
293
+ const response = error as Response;
294
+ expect(response.status).toBe(401);
295
+ const data = await response.json();
296
+ expect(data.message).toBe('Unauthorized');
297
+ }
298
+ });
299
+
300
+ it('should handle empty query parameters', async () => {
301
+ const client = createTypedFetcher<paths>({
302
+ baseURL: 'https://api.example.com',
303
+ });
304
+
305
+ const result = await client('GET /posts', {
306
+ query: { page: 1, sort: undefined as any },
307
+ });
308
+
309
+ expect(result.pagination.page).toBe(1);
310
+ expect(result.sort).toBe('asc'); // Default value when sort is undefined
311
+ });
312
+
313
+ it('should properly encode path parameters', async () => {
314
+ const client = createTypedFetcher<paths>({
315
+ baseURL: 'https://api.example.com',
316
+ });
317
+
318
+ const result = await client('GET /users/{id}', {
319
+ params: { id: 'user with spaces' },
320
+ });
321
+
322
+ // MSW decodes the URL parameters, so we get the original value back
323
+ expect(result).toEqual({
324
+ id: 'user with spaces',
325
+ name: 'John Doe',
326
+ email: 'john@example.com',
327
+ });
328
+ });
329
+
330
+ it('should correctly substitute multiple path parameters', async () => {
331
+ // Add a mock handler for this test
332
+ const mockFetch = vi.fn(async (url: string) => {
333
+ if (url === 'https://api.example.com/posts/123/comments/456') {
334
+ return new Response(
335
+ JSON.stringify({
336
+ postId: '123',
337
+ commentId: '456',
338
+ content: 'Test comment',
339
+ }),
340
+ { status: 200, headers: { 'Content-Type': 'application/json' } },
341
+ );
342
+ }
343
+ return new Response('Not found', { status: 404 });
344
+ }) as FetchFn;
345
+
346
+ const client = createTypedFetcher<paths>({
347
+ baseURL: 'https://api.example.com',
348
+ fetch: mockFetch,
349
+ });
350
+
351
+ const result = await client(
352
+ 'GET /posts/{postId}/comments/{commentId}' as any,
353
+ {
354
+ params: { postId: '123', commentId: '456' },
355
+ },
356
+ );
357
+
358
+ expect(mockFetch).toHaveBeenCalledWith(
359
+ 'https://api.example.com/posts/123/comments/456',
360
+ expect.any(Object),
361
+ );
362
+ expect(result).toEqual({
363
+ postId: '123',
364
+ commentId: '456',
365
+ content: 'Test comment',
366
+ });
367
+ });
368
+
369
+ it('should URL encode special characters in path parameters', async () => {
370
+ // Mock fetch to verify the actual URL being called
371
+ const mockFetch = vi.fn(async (url: string) => {
372
+ if (
373
+ url ===
374
+ 'https://api.example.com/users/user%20with%20spaces%2Fand%2Fslashes'
375
+ ) {
376
+ return new Response(
377
+ JSON.stringify({
378
+ id: 'user with spaces/and/slashes',
379
+ name: 'Test User',
380
+ email: 'test@example.com',
381
+ }),
382
+ { status: 200, headers: { 'Content-Type': 'application/json' } },
383
+ );
384
+ }
385
+ return new Response('Not found', { status: 404 });
386
+ }) as typeof fetch;
387
+
388
+ const client = createTypedFetcher<paths>({
389
+ baseURL: 'https://api.example.com',
390
+ fetch: mockFetch,
391
+ });
392
+
393
+ const result = await client('GET /users/{id}', {
394
+ params: { id: 'user with spaces/and/slashes' },
395
+ });
396
+
397
+ // Verify the URL was properly encoded
398
+ expect(mockFetch).toHaveBeenCalledWith(
399
+ 'https://api.example.com/users/user%20with%20spaces%2Fand%2Fslashes',
400
+ expect.any(Object),
401
+ );
402
+
403
+ expect(result).toEqual({
404
+ id: 'user with spaces/and/slashes',
405
+ name: 'Test User',
406
+ email: 'test@example.com',
407
+ });
408
+ });
409
409
  });