@navios/react-query 0.7.0 → 1.0.0-alpha.1

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 (124) hide show
  1. package/CHANGELOG.md +166 -0
  2. package/README.md +152 -4
  3. package/dist/src/__tests__/errorSchema.spec.d.mts +2 -0
  4. package/dist/src/__tests__/errorSchema.spec.d.mts.map +1 -0
  5. package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts +2 -0
  6. package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts.map +1 -0
  7. package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts +2 -0
  8. package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts.map +1 -0
  9. package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts +2 -0
  10. package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts.map +1 -0
  11. package/dist/src/client/__type-tests__/mutation.spec-d.d.mts +2 -0
  12. package/dist/src/client/__type-tests__/mutation.spec-d.d.mts.map +1 -0
  13. package/dist/src/client/__type-tests__/query.spec-d.d.mts +2 -0
  14. package/dist/src/client/__type-tests__/query.spec-d.d.mts.map +1 -0
  15. package/dist/src/client/declare-client.d.mts +15 -8
  16. package/dist/src/client/declare-client.d.mts.map +1 -1
  17. package/dist/src/client/types/from-endpoint.d.mts +130 -0
  18. package/dist/src/client/types/from-endpoint.d.mts.map +1 -0
  19. package/dist/src/client/types/helpers.d.mts +74 -0
  20. package/dist/src/client/types/helpers.d.mts.map +1 -0
  21. package/dist/src/client/types/index.d.mts +21 -0
  22. package/dist/src/client/types/index.d.mts.map +1 -0
  23. package/dist/src/client/types/infinite-query.d.mts +61 -0
  24. package/dist/src/client/types/infinite-query.d.mts.map +1 -0
  25. package/dist/src/client/types/multipart-mutation.d.mts +98 -0
  26. package/dist/src/client/types/multipart-mutation.d.mts.map +1 -0
  27. package/dist/src/client/types/mutation.d.mts +75 -0
  28. package/dist/src/client/types/mutation.d.mts.map +1 -0
  29. package/dist/src/client/types/query.d.mts +65 -0
  30. package/dist/src/client/types/query.d.mts.map +1 -0
  31. package/dist/src/client/types.d.mts +1 -608
  32. package/dist/src/client/types.d.mts.map +1 -1
  33. package/dist/src/common/types.d.mts +30 -3
  34. package/dist/src/common/types.d.mts.map +1 -1
  35. package/dist/src/mutation/index.d.mts +1 -0
  36. package/dist/src/mutation/index.d.mts.map +1 -1
  37. package/dist/src/mutation/make-hook.d.mts +42 -16
  38. package/dist/src/mutation/make-hook.d.mts.map +1 -1
  39. package/dist/src/mutation/optimistic.d.mts +166 -0
  40. package/dist/src/mutation/optimistic.d.mts.map +1 -0
  41. package/dist/src/mutation/types.d.mts +51 -19
  42. package/dist/src/mutation/types.d.mts.map +1 -1
  43. package/dist/src/query/index.d.mts +1 -0
  44. package/dist/src/query/index.d.mts.map +1 -1
  45. package/dist/src/query/key-creator.d.mts.map +1 -1
  46. package/dist/src/query/make-infinite-options.d.mts +3 -2
  47. package/dist/src/query/make-infinite-options.d.mts.map +1 -1
  48. package/dist/src/query/make-options.d.mts +42 -12
  49. package/dist/src/query/make-options.d.mts.map +1 -1
  50. package/dist/src/query/prefetch.d.mts +245 -0
  51. package/dist/src/query/prefetch.d.mts.map +1 -0
  52. package/dist/src/query/types.d.mts +35 -17
  53. package/dist/src/query/types.d.mts.map +1 -1
  54. package/dist/tsconfig.tsbuildinfo +1 -1
  55. package/lib/index.cjs +454 -37
  56. package/lib/index.cjs.map +1 -1
  57. package/lib/index.d.cts +1021 -598
  58. package/lib/index.d.cts.map +1 -1
  59. package/lib/index.d.mts +1019 -596
  60. package/lib/index.d.mts.map +1 -1
  61. package/lib/index.mjs +441 -29
  62. package/lib/index.mjs.map +1 -1
  63. package/package.json +8 -8
  64. package/src/__tests__/declare-client.spec.mts +1 -2
  65. package/src/__tests__/errorSchema.spec.mts +391 -0
  66. package/src/__tests__/make-mutation.spec.mts +6 -5
  67. package/src/__tests__/makeDataTag.spec.mts +2 -1
  68. package/src/__tests__/makeQueryOptions.spec.mts +2 -1
  69. package/src/client/__type-tests__/from-endpoint.spec-d.mts +550 -0
  70. package/src/client/__type-tests__/infinite-query.spec-d.mts +648 -0
  71. package/src/client/__type-tests__/multipart-mutation.spec-d.mts +725 -0
  72. package/src/client/__type-tests__/mutation.spec-d.mts +757 -0
  73. package/src/client/__type-tests__/query.spec-d.mts +701 -0
  74. package/src/client/declare-client.mts +59 -34
  75. package/src/client/types/from-endpoint.mts +345 -0
  76. package/src/client/types/helpers.mts +140 -0
  77. package/src/client/types/index.mts +26 -0
  78. package/src/client/types/infinite-query.mts +133 -0
  79. package/src/client/types/multipart-mutation.mts +264 -0
  80. package/src/client/types/mutation.mts +176 -0
  81. package/src/client/types/query.mts +132 -0
  82. package/src/client/types.mts +1 -1935
  83. package/src/common/types.mts +48 -3
  84. package/src/mutation/index.mts +1 -0
  85. package/src/mutation/make-hook.mts +171 -63
  86. package/src/mutation/optimistic.mts +294 -0
  87. package/src/mutation/types.mts +102 -29
  88. package/src/query/index.mts +1 -0
  89. package/src/query/key-creator.mts +24 -13
  90. package/src/query/make-infinite-options.mts +53 -10
  91. package/src/query/make-options.mts +184 -43
  92. package/src/query/prefetch.mts +326 -0
  93. package/src/query/types.mts +76 -16
  94. package/dist/src/declare-client.d.mts +0 -31
  95. package/dist/src/declare-client.d.mts.map +0 -1
  96. package/dist/src/make-infinite-query-options.d.mts +0 -13
  97. package/dist/src/make-infinite-query-options.d.mts.map +0 -1
  98. package/dist/src/make-mutation.d.mts +0 -15
  99. package/dist/src/make-mutation.d.mts.map +0 -1
  100. package/dist/src/make-query-options.d.mts +0 -15
  101. package/dist/src/make-query-options.d.mts.map +0 -1
  102. package/dist/src/types/client-endpoint-helper.d.mts +0 -8
  103. package/dist/src/types/client-endpoint-helper.d.mts.map +0 -1
  104. package/dist/src/types/client-instance.d.mts +0 -211
  105. package/dist/src/types/client-instance.d.mts.map +0 -1
  106. package/dist/src/types/index.d.mts +0 -8
  107. package/dist/src/types/index.d.mts.map +0 -1
  108. package/dist/src/types/mutation-args.d.mts +0 -10
  109. package/dist/src/types/mutation-args.d.mts.map +0 -1
  110. package/dist/src/types/mutation-helpers.d.mts +0 -10
  111. package/dist/src/types/mutation-helpers.d.mts.map +0 -1
  112. package/dist/src/types/query-args.d.mts +0 -8
  113. package/dist/src/types/query-args.d.mts.map +0 -1
  114. package/dist/src/types/query-helpers.d.mts +0 -14
  115. package/dist/src/types/query-helpers.d.mts.map +0 -1
  116. package/dist/src/types/query-url-params-args.d.mts +0 -5
  117. package/dist/src/types/query-url-params-args.d.mts.map +0 -1
  118. package/dist/src/types.d.mts +0 -49
  119. package/dist/src/types.d.mts.map +0 -1
  120. package/dist/src/utils/mutation-key.creator.d.mts +0 -39
  121. package/dist/src/utils/mutation-key.creator.d.mts.map +0 -1
  122. package/dist/src/utils/query-key-creator.d.mts +0 -24
  123. package/dist/src/utils/query-key-creator.d.mts.map +0 -1
  124. package/src/client/__type-tests__/client-instance.spec-d.mts +0 -852
@@ -1,852 +0,0 @@
1
- import type {
2
- DataTag,
3
- InfiniteData,
4
- UseMutationResult,
5
- UseSuspenseInfiniteQueryOptions,
6
- UseSuspenseQueryOptions,
7
- } from '@tanstack/react-query'
8
- import type { z } from 'zod/v4'
9
-
10
- import { assertType, describe, test } from 'vitest'
11
- import { z as zod } from 'zod/v4'
12
-
13
- import type { ClientInstance, StreamHelper } from '../types.mjs'
14
- import type { QueryHelpers } from '../../query/types.mjs'
15
- import type { MutationHelpers } from '../../mutation/types.mjs'
16
- import type { EndpointHelper } from '../types.mjs'
17
- import type { Split } from '../../common/types.mjs'
18
- import type { BaseStreamConfig } from '@navios/builder'
19
-
20
- declare const client: ClientInstance
21
-
22
- const responseSchema = zod.object({
23
- id: zod.string(),
24
- name: zod.string(),
25
- })
26
-
27
- const querySchema = zod.object({
28
- page: zod.number(),
29
- limit: zod.number(),
30
- })
31
-
32
- const requestSchema = zod.object({
33
- name: zod.string(),
34
- email: zod.string(),
35
- })
36
-
37
- type ResponseType = z.output<typeof responseSchema>
38
- type QueryType = z.input<typeof querySchema>
39
- type RequestType = z.input<typeof requestSchema>
40
-
41
- describe('ClientInstance', () => {
42
- describe('query() method', () => {
43
- describe('GET endpoints', () => {
44
- test('simple GET query without params', () => {
45
- const query = client.query({
46
- method: 'GET',
47
- url: '/users',
48
- responseSchema,
49
- })
50
-
51
- // Return type should be callable returning UseSuspenseQueryOptions
52
- assertType<
53
- (params: {}) => UseSuspenseQueryOptions<
54
- ResponseType,
55
- Error,
56
- ResponseType,
57
- DataTag<Split<'/users', '/'>, ResponseType, Error>
58
- >
59
- >(query)
60
-
61
- // Should have QueryHelpers
62
- assertType<QueryHelpers<'/users', undefined, ResponseType>['queryKey']>(
63
- query.queryKey,
64
- )
65
- assertType<QueryHelpers<'/users', undefined, ResponseType>['use']>(
66
- query.use,
67
- )
68
- assertType<
69
- QueryHelpers<'/users', undefined, ResponseType>['useSuspense']
70
- >(query.useSuspense)
71
- assertType<
72
- QueryHelpers<'/users', undefined, ResponseType>['invalidate']
73
- >(query.invalidate)
74
- assertType<
75
- QueryHelpers<'/users', undefined, ResponseType>['invalidateAll']
76
- >(query.invalidateAll)
77
-
78
- // Should have EndpointHelper
79
- assertType<
80
- EndpointHelper<
81
- 'GET',
82
- '/users',
83
- undefined,
84
- typeof responseSchema
85
- >['endpoint']
86
- >(query.endpoint)
87
- })
88
-
89
- test('GET query with URL params', () => {
90
- const query = client.query({
91
- method: 'GET',
92
- url: '/users/$userId',
93
- responseSchema,
94
- })
95
-
96
- // Should require urlParams
97
- assertType<
98
- (params: {
99
- urlParams: { userId: string | number }
100
- }) => UseSuspenseQueryOptions<
101
- ResponseType,
102
- Error,
103
- ResponseType,
104
- DataTag<Split<'/users/$userId', '/'>, ResponseType, Error>
105
- >
106
- >(query)
107
- })
108
-
109
- test('GET query with query schema', () => {
110
- const query = client.query({
111
- method: 'GET',
112
- url: '/users',
113
- querySchema,
114
- responseSchema,
115
- })
116
-
117
- // Should require params
118
- assertType<
119
- (params: { params: QueryType }) => UseSuspenseQueryOptions<
120
- ResponseType,
121
- Error,
122
- ResponseType,
123
- DataTag<Split<'/users', '/'>, ResponseType, Error>
124
- >
125
- >(query)
126
-
127
- // Should have QueryHelpers with query schema
128
- assertType<
129
- QueryHelpers<'/users', typeof querySchema, ResponseType>['queryKey']
130
- >(query.queryKey)
131
- })
132
-
133
- test('GET query with URL params and query schema', () => {
134
- const query = client.query({
135
- method: 'GET',
136
- url: '/users/$userId/posts',
137
- querySchema,
138
- responseSchema,
139
- })
140
-
141
- // Should require both urlParams and params
142
- assertType<
143
- (params: {
144
- urlParams: { userId: string | number }
145
- params: QueryType
146
- }) => UseSuspenseQueryOptions<
147
- ResponseType,
148
- Error,
149
- ResponseType,
150
- DataTag<Split<'/users/$userId/posts', '/'>, ResponseType, Error>
151
- >
152
- >(query)
153
- })
154
-
155
- test('GET query with processResponse transformation', () => {
156
- const query = client.query({
157
- method: 'GET',
158
- url: '/users',
159
- responseSchema,
160
- processResponse: (data) => data.name.toUpperCase(),
161
- })
162
-
163
- // Result type should be string (transformed)
164
- assertType<
165
- (params: {}) => UseSuspenseQueryOptions<
166
- string,
167
- Error,
168
- string,
169
- DataTag<Split<'/users', '/'>, string, Error>
170
- >
171
- >(query)
172
- })
173
- })
174
-
175
- describe('HEAD and OPTIONS endpoints', () => {
176
- test('HEAD query', () => {
177
- const query = client.query({
178
- method: 'HEAD',
179
- url: '/ping',
180
- responseSchema,
181
- })
182
-
183
- assertType<
184
- (params: {}) => UseSuspenseQueryOptions<
185
- ResponseType,
186
- Error,
187
- ResponseType,
188
- DataTag<Split<'/ping', '/'>, ResponseType, Error>
189
- >
190
- >(query)
191
- })
192
-
193
- test('OPTIONS query', () => {
194
- const query = client.query({
195
- method: 'OPTIONS',
196
- url: '/cors',
197
- responseSchema,
198
- })
199
-
200
- assertType<
201
- (params: {}) => UseSuspenseQueryOptions<
202
- ResponseType,
203
- Error,
204
- ResponseType,
205
- DataTag<Split<'/cors', '/'>, ResponseType, Error>
206
- >
207
- >(query)
208
- })
209
- })
210
-
211
- describe('POST query endpoints (for search)', () => {
212
- test('POST query with request schema', () => {
213
- const query = client.query({
214
- method: 'POST',
215
- url: '/search',
216
- requestSchema,
217
- responseSchema,
218
- })
219
-
220
- // Should require data
221
- assertType<
222
- (params: { data: RequestType }) => UseSuspenseQueryOptions<
223
- ResponseType,
224
- Error,
225
- ResponseType,
226
- DataTag<Split<'/search', '/'>, ResponseType, Error>
227
- >
228
- >(query)
229
-
230
- // Should have EndpointHelper with request schema
231
- assertType<
232
- EndpointHelper<
233
- 'POST',
234
- '/search',
235
- typeof requestSchema,
236
- typeof responseSchema
237
- >['endpoint']
238
- >(query.endpoint)
239
- })
240
-
241
- test('POST query with URL params and request schema', () => {
242
- const query = client.query({
243
- method: 'POST',
244
- url: '/users/$userId/search',
245
- requestSchema,
246
- responseSchema,
247
- })
248
-
249
- // Should require urlParams and data
250
- assertType<
251
- (params: {
252
- urlParams: { userId: string | number }
253
- data: RequestType
254
- }) => UseSuspenseQueryOptions<
255
- ResponseType,
256
- Error,
257
- ResponseType,
258
- DataTag<Split<'/users/$userId/search', '/'>, ResponseType, Error>
259
- >
260
- >(query)
261
- })
262
-
263
- test('POST query with all schemas', () => {
264
- const query = client.query({
265
- method: 'POST',
266
- url: '/search',
267
- querySchema,
268
- requestSchema,
269
- responseSchema,
270
- })
271
-
272
- // Should require params and data
273
- assertType<
274
- (params: {
275
- params: QueryType
276
- data: RequestType
277
- }) => UseSuspenseQueryOptions<
278
- ResponseType,
279
- Error,
280
- ResponseType,
281
- DataTag<Split<'/search', '/'>, ResponseType, Error>
282
- >
283
- >(query)
284
- })
285
- })
286
- })
287
-
288
- describe('infiniteQuery() method', () => {
289
- test('GET infinite query', () => {
290
- const query = client.infiniteQuery({
291
- method: 'GET',
292
- url: '/users',
293
- querySchema,
294
- responseSchema,
295
- getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
296
- undefined,
297
- })
298
-
299
- // Should return UseSuspenseInfiniteQueryOptions
300
- assertType<
301
- (params: { params: QueryType }) => UseSuspenseInfiniteQueryOptions<
302
- ResponseType,
303
- Error,
304
- InfiniteData<ResponseType>,
305
- DataTag<Split<'/users', '/'>, ResponseType, Error>,
306
- z.output<typeof querySchema>
307
- >
308
- >(query)
309
-
310
- // Should have QueryHelpers with isInfinite=true
311
- assertType<
312
- QueryHelpers<
313
- '/users',
314
- typeof querySchema,
315
- ResponseType,
316
- true
317
- >['queryKey']
318
- >(query.queryKey)
319
- })
320
-
321
- test('GET infinite query with URL params', () => {
322
- const query = client.infiniteQuery({
323
- method: 'GET',
324
- url: '/users/$userId/posts',
325
- querySchema,
326
- responseSchema,
327
- getNextPageParam: () => undefined,
328
- })
329
-
330
- // Should require urlParams and params
331
- assertType<
332
- (params: {
333
- urlParams: { userId: string | number }
334
- params: QueryType
335
- }) => UseSuspenseInfiniteQueryOptions<
336
- ResponseType,
337
- Error,
338
- InfiniteData<ResponseType>,
339
- DataTag<Split<'/users/$userId/posts', '/'>, ResponseType, Error>,
340
- z.output<typeof querySchema>
341
- >
342
- >(query)
343
- })
344
-
345
- test('POST infinite query', () => {
346
- const query = client.infiniteQuery({
347
- method: 'POST',
348
- url: '/search',
349
- querySchema,
350
- requestSchema,
351
- responseSchema,
352
- getNextPageParam: () => undefined,
353
- })
354
-
355
- // Should require params and data
356
- assertType<
357
- (params: {
358
- params: QueryType
359
- data: RequestType
360
- }) => UseSuspenseInfiniteQueryOptions<
361
- ResponseType,
362
- Error,
363
- InfiniteData<ResponseType>,
364
- DataTag<Split<'/search', '/'>, ResponseType, Error>,
365
- z.output<typeof querySchema>
366
- >
367
- >(query)
368
- })
369
- })
370
-
371
- describe('mutation() method', () => {
372
- describe('POST/PUT/PATCH mutations', () => {
373
- test('POST mutation with request schema only', () => {
374
- const mutation = client.mutation({
375
- method: 'POST',
376
- url: '/users',
377
- requestSchema,
378
- responseSchema,
379
- processResponse: (data) => data,
380
- })
381
-
382
- // Should return a function that returns UseMutationResult
383
- assertType<
384
- () => UseMutationResult<
385
- ResponseType,
386
- Error,
387
- { data: RequestType }
388
- >
389
- >(mutation)
390
-
391
- // Should have MutationHelpers
392
- assertType<MutationHelpers<'/users', ResponseType>['mutationKey']>(
393
- mutation.mutationKey,
394
- )
395
- assertType<MutationHelpers<'/users', ResponseType>['useIsMutating']>(
396
- mutation.useIsMutating,
397
- )
398
-
399
- // Should have EndpointHelper
400
- assertType<
401
- EndpointHelper<
402
- 'POST',
403
- '/users',
404
- typeof requestSchema,
405
- typeof responseSchema
406
- >['endpoint']
407
- >(mutation.endpoint)
408
- })
409
-
410
- test('POST mutation with useKey', () => {
411
- const mutation = client.mutation({
412
- method: 'POST',
413
- url: '/users/$userId',
414
- useKey: true,
415
- requestSchema,
416
- responseSchema,
417
- processResponse: (data) => data,
418
- })
419
-
420
- // With useKey, should require urlParams in the call
421
- assertType<
422
- (params: {
423
- urlParams: { userId: string | number }
424
- }) => UseMutationResult<
425
- ResponseType,
426
- Error,
427
- { urlParams: { userId: string | number }; data: RequestType }
428
- >
429
- >(mutation)
430
- })
431
-
432
- test('PUT mutation with URL params', () => {
433
- const mutation = client.mutation({
434
- method: 'PUT',
435
- url: '/users/$userId',
436
- requestSchema,
437
- responseSchema,
438
- processResponse: (data) => data,
439
- })
440
-
441
- // Without useKey, should return hook directly
442
- assertType<
443
- () => UseMutationResult<
444
- ResponseType,
445
- Error,
446
- { urlParams: { userId: string | number }; data: RequestType }
447
- >
448
- >(mutation)
449
- })
450
-
451
- test('PATCH mutation with query schema', () => {
452
- const mutation = client.mutation({
453
- method: 'PATCH',
454
- url: '/users/$userId',
455
- requestSchema,
456
- querySchema,
457
- responseSchema,
458
- processResponse: (data) => data,
459
- })
460
-
461
- // Should include params in variables
462
- assertType<
463
- () => UseMutationResult<
464
- ResponseType,
465
- Error,
466
- {
467
- urlParams: { userId: string | number }
468
- data: RequestType
469
- params: QueryType
470
- }
471
- >
472
- >(mutation)
473
- })
474
-
475
- test('mutation with custom result type', () => {
476
- const mutation = client.mutation({
477
- method: 'POST',
478
- url: '/users',
479
- requestSchema,
480
- responseSchema,
481
- processResponse: (data) => ({ processed: true, name: data.name }),
482
- })
483
-
484
- // Result type should be the transformed type
485
- assertType<
486
- () => UseMutationResult<
487
- { processed: boolean; name: string },
488
- Error,
489
- { data: RequestType }
490
- >
491
- >(mutation)
492
- })
493
- })
494
-
495
- describe('DELETE mutations', () => {
496
- test('DELETE mutation without schemas', () => {
497
- const mutation = client.mutation({
498
- method: 'DELETE',
499
- url: '/users/$userId',
500
- responseSchema,
501
- processResponse: (data) => data,
502
- })
503
-
504
- assertType<
505
- () => UseMutationResult<
506
- ResponseType,
507
- Error,
508
- { urlParams: { userId: string | number } }
509
- >
510
- >(mutation)
511
- })
512
-
513
- test('DELETE mutation with useKey and URL params and querySchema', () => {
514
- const mutation = client.mutation({
515
- method: 'DELETE',
516
- url: '/users/$userId',
517
- useKey: true,
518
- querySchema,
519
- responseSchema,
520
- processResponse: (data) => data,
521
- })
522
-
523
- // With useKey and URL params, should require urlParams in the call
524
- assertType<
525
- (params: {
526
- urlParams: { userId: string | number }
527
- }) => UseMutationResult<
528
- ResponseType,
529
- Error,
530
- { urlParams: { userId: string | number }; params: QueryType }
531
- >
532
- >(mutation)
533
- })
534
-
535
- test('DELETE mutation with useKey and URL params (no querySchema)', () => {
536
- const mutation = client.mutation({
537
- method: 'DELETE',
538
- url: '/users/$userId',
539
- useKey: true,
540
- responseSchema,
541
- processResponse: (data) => data,
542
- })
543
-
544
- // With useKey and URL params, should require urlParams in the call
545
- assertType<
546
- (params: {
547
- urlParams: { userId: string | number }
548
- }) => UseMutationResult<
549
- ResponseType,
550
- Error,
551
- { urlParams: { userId: string | number } }
552
- >
553
- >(mutation)
554
- })
555
-
556
- test('DELETE mutation with useKey without URL params', () => {
557
- const mutation = client.mutation({
558
- method: 'DELETE',
559
- url: '/cache',
560
- useKey: true,
561
- responseSchema,
562
- processResponse: (data) => data,
563
- })
564
-
565
- // With useKey but no URL params, should require empty params
566
- assertType<
567
- (params: {}) => UseMutationResult<ResponseType, Error, {}>
568
- >(mutation)
569
- })
570
-
571
- test('DELETE mutation with query schema', () => {
572
- const mutation = client.mutation({
573
- method: 'DELETE',
574
- url: '/cache',
575
- querySchema,
576
- responseSchema,
577
- processResponse: (data) => data,
578
- })
579
-
580
- assertType<
581
- () => UseMutationResult<
582
- ResponseType,
583
- Error,
584
- { params: QueryType }
585
- >
586
- >(mutation)
587
- })
588
- })
589
- })
590
-
591
- describe('multipartMutation() method', () => {
592
- test('multipart mutation with request schema', () => {
593
- const mutation = client.multipartMutation({
594
- method: 'POST',
595
- url: '/upload',
596
- requestSchema,
597
- responseSchema,
598
- processResponse: (data) => data,
599
- })
600
-
601
- assertType<
602
- () => UseMutationResult<ResponseType, Error, { data: RequestType }>
603
- >(mutation)
604
- })
605
-
606
- test('multipart mutation with useKey', () => {
607
- const mutation = client.multipartMutation({
608
- method: 'POST',
609
- url: '/users/$userId/avatar',
610
- useKey: true,
611
- requestSchema,
612
- responseSchema,
613
- processResponse: (data) => data,
614
- })
615
-
616
- assertType<
617
- (params: {
618
- urlParams: { userId: string | number }
619
- }) => UseMutationResult<
620
- ResponseType,
621
- Error,
622
- { urlParams: { userId: string | number }; data: RequestType }
623
- >
624
- >(mutation)
625
- })
626
-
627
- test('multipart mutation with query schema', () => {
628
- const mutation = client.multipartMutation({
629
- method: 'POST',
630
- url: '/upload',
631
- requestSchema,
632
- querySchema,
633
- responseSchema,
634
- processResponse: (data) => data,
635
- })
636
-
637
- assertType<
638
- () => UseMutationResult<
639
- ResponseType,
640
- Error,
641
- { data: RequestType; params: QueryType }
642
- >
643
- >(mutation)
644
- })
645
- })
646
- })
647
-
648
- // Stream endpoint type declarations for testing
649
- declare const streamEndpointGet: {
650
- config: BaseStreamConfig<'GET', '/files/$fileId/download', undefined, undefined>
651
- } & ((params: { urlParams: { fileId: string | number } }) => Promise<Blob>)
652
-
653
- declare const streamEndpointGetWithQuery: {
654
- config: BaseStreamConfig<
655
- 'GET',
656
- '/files/$fileId/download',
657
- typeof querySchema,
658
- undefined
659
- >
660
- } & ((params: {
661
- urlParams: { fileId: string | number }
662
- params: z.input<typeof querySchema>
663
- }) => Promise<Blob>)
664
-
665
- declare const streamEndpointPost: {
666
- config: BaseStreamConfig<
667
- 'POST',
668
- '/files/$fileId/export',
669
- undefined,
670
- typeof requestSchema
671
- >
672
- } & ((params: {
673
- urlParams: { fileId: string | number }
674
- data: z.input<typeof requestSchema>
675
- }) => Promise<Blob>)
676
-
677
- describe('mutationFromEndpoint() with stream endpoints', () => {
678
- test('GET stream mutation without options (returns Blob)', () => {
679
- const mutation = client.mutationFromEndpoint(streamEndpointGet)
680
-
681
- // Should return a function that returns UseMutationResult with Blob
682
- assertType<
683
- () => UseMutationResult<Blob, Error, { urlParams: { fileId: string | number } }>
684
- >(mutation)
685
-
686
- // Should have StreamHelper
687
- assertType<
688
- StreamHelper<'GET', '/files/$fileId/download', undefined, undefined>['endpoint']
689
- >(mutation.endpoint)
690
- })
691
-
692
- test('GET stream mutation with processResponse transformation', () => {
693
- const mutation = client.mutationFromEndpoint(streamEndpointGet, {
694
- processResponse: (blob) => URL.createObjectURL(blob),
695
- })
696
-
697
- // Result type should be string (transformed)
698
- assertType<
699
- () => UseMutationResult<
700
- string,
701
- Error,
702
- { urlParams: { fileId: string | number } }
703
- >
704
- >(mutation)
705
- })
706
-
707
- test('GET stream mutation with useKey', () => {
708
- const mutation = client.mutationFromEndpoint(streamEndpointGet, {
709
- useKey: true,
710
- })
711
-
712
- // With useKey, should require urlParams in the call
713
- assertType<
714
- (params: {
715
- urlParams: { fileId: string | number }
716
- }) => UseMutationResult<
717
- Blob,
718
- Error,
719
- { urlParams: { fileId: string | number } }
720
- >
721
- >(mutation)
722
-
723
- // Should have MutationHelpers
724
- assertType<
725
- MutationHelpers<'/files/$fileId/download', Blob>['mutationKey']
726
- >(mutation.mutationKey)
727
- })
728
-
729
- test('GET stream mutation with querySchema', () => {
730
- const mutation = client.mutationFromEndpoint(streamEndpointGetWithQuery)
731
-
732
- assertType<
733
- () => UseMutationResult<
734
- Blob,
735
- Error,
736
- {
737
- urlParams: { fileId: string | number }
738
- params: z.input<typeof querySchema>
739
- }
740
- >
741
- >(mutation)
742
- })
743
-
744
- test('POST stream mutation with requestSchema', () => {
745
- const mutation = client.mutationFromEndpoint(streamEndpointPost)
746
-
747
- assertType<
748
- () => UseMutationResult<
749
- Blob,
750
- Error,
751
- {
752
- urlParams: { fileId: string | number }
753
- data: z.input<typeof requestSchema>
754
- }
755
- >
756
- >(mutation)
757
- })
758
-
759
- test('stream mutation with onSuccess callback', () => {
760
- const mutation = client.mutationFromEndpoint(streamEndpointGet, {
761
- onSuccess: (data, variables) => {
762
- // data should be Blob
763
- assertType<Blob>(data)
764
- // variables should have urlParams
765
- assertType<{ urlParams: { fileId: string | number } }>(variables)
766
- },
767
- })
768
-
769
- assertType<
770
- () => UseMutationResult<
771
- Blob,
772
- Error,
773
- { urlParams: { fileId: string | number } }
774
- >
775
- >(mutation)
776
- })
777
-
778
- test('stream mutation with custom processResponse and onSuccess', () => {
779
- const mutation = client.mutationFromEndpoint(streamEndpointGet, {
780
- processResponse: (blob) => ({ url: URL.createObjectURL(blob), size: blob.size }),
781
- onSuccess: (data) => {
782
- // data should be the transformed type
783
- assertType<{ url: string; size: number }>(data)
784
- },
785
- })
786
-
787
- assertType<
788
- () => UseMutationResult<
789
- { url: string; size: number },
790
- Error,
791
- { urlParams: { fileId: string | number } }
792
- >
793
- >(mutation)
794
- })
795
- })
796
-
797
- describe('Error cases - should fail type checking', () => {
798
- test('GET query without urlParams when URL has params', () => {
799
- const query = client.query({
800
- method: 'GET',
801
- url: '/users/$userId',
802
- responseSchema,
803
- })
804
-
805
- // @ts-expect-error - missing urlParams
806
- query({})
807
- })
808
-
809
- test('GET query without params when querySchema is defined', () => {
810
- const query = client.query({
811
- method: 'GET',
812
- url: '/users',
813
- querySchema,
814
- responseSchema,
815
- })
816
-
817
- // @ts-expect-error - missing params
818
- query({})
819
- })
820
-
821
- test('POST query without data when requestSchema is defined', () => {
822
- const query = client.query({
823
- method: 'POST',
824
- url: '/search',
825
- requestSchema,
826
- responseSchema,
827
- })
828
-
829
- // @ts-expect-error - missing data
830
- query({})
831
- })
832
-
833
- test('mutation without processResponse', () => {
834
- client.mutation({
835
- // @ts-expect-error - missing processResponse causes method to not match any overload
836
- method: 'POST',
837
- url: '/users',
838
- requestSchema,
839
- responseSchema,
840
- })
841
- })
842
-
843
- test('infiniteQuery without getNextPageParam', () => {
844
- // @ts-expect-error - missing getNextPageParam
845
- client.infiniteQuery({
846
- method: 'GET',
847
- url: '/users',
848
- querySchema,
849
- responseSchema,
850
- })
851
- })
852
- })