@navios/react-query 0.7.1 → 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 +171 -1
- package/README.md +152 -4
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/client/declare-client.mts.html +1264 -0
- package/coverage/client/index.html +116 -0
- package/coverage/clover.xml +160 -0
- package/coverage/coverage-final.json +8 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +146 -0
- package/coverage/mutation/index.html +131 -0
- package/coverage/mutation/key-creator.mts.html +277 -0
- package/coverage/mutation/make-hook.mts.html +952 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/query/index.html +161 -0
- package/coverage/query/key-creator.mts.html +415 -0
- package/coverage/query/make-infinite-options.mts.html +601 -0
- package/coverage/query/make-options.mts.html +838 -0
- package/coverage/query/prefetch.mts.html +1063 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/dist/src/__tests__/errorSchema.spec.d.mts +2 -0
- package/dist/src/__tests__/errorSchema.spec.d.mts.map +1 -0
- package/dist/src/__tests__/prefetch.spec.d.mts +2 -0
- package/dist/src/__tests__/prefetch.spec.d.mts.map +1 -0
- package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts +2 -0
- package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts.map +1 -0
- package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts +2 -0
- package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts.map +1 -0
- package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts +2 -0
- package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts.map +1 -0
- package/dist/src/client/__type-tests__/mutation.spec-d.d.mts +2 -0
- package/dist/src/client/__type-tests__/mutation.spec-d.d.mts.map +1 -0
- package/dist/src/client/__type-tests__/query.spec-d.d.mts +2 -0
- package/dist/src/client/__type-tests__/query.spec-d.d.mts.map +1 -0
- package/dist/src/client/declare-client.d.mts +15 -8
- package/dist/src/client/declare-client.d.mts.map +1 -1
- package/dist/src/client/types/from-endpoint.d.mts +130 -0
- package/dist/src/client/types/from-endpoint.d.mts.map +1 -0
- package/dist/src/client/types/helpers.d.mts +74 -0
- package/dist/src/client/types/helpers.d.mts.map +1 -0
- package/dist/src/client/types/index.d.mts +21 -0
- package/dist/src/client/types/index.d.mts.map +1 -0
- package/dist/src/client/types/infinite-query.d.mts +61 -0
- package/dist/src/client/types/infinite-query.d.mts.map +1 -0
- package/dist/src/client/types/multipart-mutation.d.mts +98 -0
- package/dist/src/client/types/multipart-mutation.d.mts.map +1 -0
- package/dist/src/client/types/mutation.d.mts +75 -0
- package/dist/src/client/types/mutation.d.mts.map +1 -0
- package/dist/src/client/types/query.d.mts +65 -0
- package/dist/src/client/types/query.d.mts.map +1 -0
- package/dist/src/client/types.d.mts +1 -608
- package/dist/src/client/types.d.mts.map +1 -1
- package/dist/src/common/types.d.mts +40 -3
- package/dist/src/common/types.d.mts.map +1 -1
- package/dist/src/mutation/index.d.mts +1 -0
- package/dist/src/mutation/index.d.mts.map +1 -1
- package/dist/src/mutation/make-hook.d.mts +42 -16
- package/dist/src/mutation/make-hook.d.mts.map +1 -1
- package/dist/src/mutation/optimistic.d.mts +172 -0
- package/dist/src/mutation/optimistic.d.mts.map +1 -0
- package/dist/src/mutation/types.d.mts +41 -20
- package/dist/src/mutation/types.d.mts.map +1 -1
- package/dist/src/query/index.d.mts +1 -0
- package/dist/src/query/index.d.mts.map +1 -1
- package/dist/src/query/key-creator.d.mts.map +1 -1
- package/dist/src/query/make-infinite-options.d.mts +3 -2
- package/dist/src/query/make-infinite-options.d.mts.map +1 -1
- package/dist/src/query/make-options.d.mts +42 -12
- package/dist/src/query/make-options.d.mts.map +1 -1
- package/dist/src/query/prefetch.d.mts +245 -0
- package/dist/src/query/prefetch.d.mts.map +1 -0
- package/dist/src/query/types.d.mts +25 -18
- package/dist/src/query/types.d.mts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/index.cjs +451 -28
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +1019 -600
- package/lib/index.d.cts.map +1 -1
- package/lib/index.d.mts +1016 -597
- package/lib/index.d.mts.map +1 -1
- package/lib/index.mjs +447 -29
- package/lib/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/declare-client.spec.mts +229 -2
- package/src/__tests__/errorSchema.spec.mts +391 -0
- package/src/__tests__/make-mutation.spec.mts +6 -5
- package/src/__tests__/makeDataTag.spec.mts +2 -1
- package/src/__tests__/makeQueryOptions.spec.mts +2 -1
- package/src/__tests__/prefetch.spec.mts +310 -0
- package/src/client/__type-tests__/from-endpoint.spec-d.mts +550 -0
- package/src/client/__type-tests__/infinite-query.spec-d.mts +648 -0
- package/src/client/__type-tests__/multipart-mutation.spec-d.mts +725 -0
- package/src/client/__type-tests__/mutation.spec-d.mts +757 -0
- package/src/client/__type-tests__/query.spec-d.mts +701 -0
- package/src/client/declare-client.mts +59 -34
- package/src/client/types/from-endpoint.mts +344 -0
- package/src/client/types/helpers.mts +140 -0
- package/src/client/types/index.mts +26 -0
- package/src/client/types/infinite-query.mts +133 -0
- package/src/client/types/multipart-mutation.mts +264 -0
- package/src/client/types/mutation.mts +176 -0
- package/src/client/types/query.mts +132 -0
- package/src/client/types.mts +1 -1935
- package/src/common/types.mts +67 -3
- package/src/mutation/index.mts +1 -0
- package/src/mutation/make-hook.mts +171 -63
- package/src/mutation/optimistic.mts +300 -0
- package/src/mutation/types.mts +87 -30
- package/src/query/index.mts +1 -0
- package/src/query/key-creator.mts +24 -13
- package/src/query/make-infinite-options.mts +53 -10
- package/src/query/make-options.mts +184 -43
- package/src/query/prefetch.mts +326 -0
- package/src/query/types.mts +56 -17
- package/src/client/__type-tests__/client-instance.spec-d.mts +0 -852
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import type { ErrorSchemaRecord } from '@navios/builder'
|
|
2
|
+
|
|
3
|
+
import { builder } from '@navios/builder'
|
|
4
|
+
import { create } from '@navios/http'
|
|
5
|
+
import { makeNaviosFakeAdapter } from '@navios/http/testing'
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
8
|
+
import { z } from 'zod/v4'
|
|
9
|
+
|
|
10
|
+
import { makeMutation } from '../mutation/make-hook.mjs'
|
|
11
|
+
import { makeQueryOptions } from '../query/make-options.mjs'
|
|
12
|
+
|
|
13
|
+
vi.mock('@tanstack/react-query', async (importReal) => {
|
|
14
|
+
const actual = await importReal<typeof import('@tanstack/react-query')>()
|
|
15
|
+
const mockMutationContext = { mutationId: 1, meta: undefined }
|
|
16
|
+
return {
|
|
17
|
+
...actual,
|
|
18
|
+
useQuery: vi.fn(),
|
|
19
|
+
useSuspenseQuery: vi.fn(),
|
|
20
|
+
useQueryClient: () => ({
|
|
21
|
+
getQueryData: vi.fn(),
|
|
22
|
+
setQueryData: vi.fn(),
|
|
23
|
+
invalidateQueries: vi.fn(),
|
|
24
|
+
removeQueries: vi.fn(),
|
|
25
|
+
}),
|
|
26
|
+
useMutation: vi.fn((req) => ({
|
|
27
|
+
...req,
|
|
28
|
+
mutateAsync: async (data: unknown) => {
|
|
29
|
+
try {
|
|
30
|
+
const onMutateResult = await req.onMutate?.(data, mockMutationContext)
|
|
31
|
+
const res = await req.mutationFn(data)
|
|
32
|
+
await req.onSuccess?.(res, data, onMutateResult, mockMutationContext)
|
|
33
|
+
await req.onSettled?.(
|
|
34
|
+
res,
|
|
35
|
+
null,
|
|
36
|
+
data,
|
|
37
|
+
onMutateResult,
|
|
38
|
+
mockMutationContext,
|
|
39
|
+
)
|
|
40
|
+
return res
|
|
41
|
+
} catch (err) {
|
|
42
|
+
const onMutateResult = undefined
|
|
43
|
+
await req.onError?.(err, data, onMutateResult, mockMutationContext)
|
|
44
|
+
await req.onSettled?.(
|
|
45
|
+
undefined,
|
|
46
|
+
err,
|
|
47
|
+
data,
|
|
48
|
+
onMutateResult,
|
|
49
|
+
mockMutationContext,
|
|
50
|
+
)
|
|
51
|
+
throw err
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
mutate: req.mutationFn,
|
|
55
|
+
})),
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
describe('errorSchema support', () => {
|
|
60
|
+
const adapter = makeNaviosFakeAdapter()
|
|
61
|
+
const api = builder({ useDiscriminatorResponse: true })
|
|
62
|
+
api.provideClient(create({ adapter: adapter.fetch }))
|
|
63
|
+
|
|
64
|
+
// Define schemas
|
|
65
|
+
const responseSchema = z.object({ id: z.string(), name: z.string() })
|
|
66
|
+
const requestSchema = z.object({ name: z.string() })
|
|
67
|
+
|
|
68
|
+
const errorSchema = {
|
|
69
|
+
400: z.object({ error: z.string(), code: z.number() }),
|
|
70
|
+
404: z.object({ notFound: z.literal(true) }),
|
|
71
|
+
500: z.object({ serverError: z.string() }),
|
|
72
|
+
} satisfies ErrorSchemaRecord
|
|
73
|
+
|
|
74
|
+
describe('makeQueryOptions with errorSchema', () => {
|
|
75
|
+
const endpoint = api.declareEndpoint({
|
|
76
|
+
method: 'GET',
|
|
77
|
+
url: '/users/$userId' as const,
|
|
78
|
+
responseSchema,
|
|
79
|
+
errorSchema,
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('should pass success response through processResponse', async () => {
|
|
83
|
+
adapter.mock('/users/1', 'GET', () => {
|
|
84
|
+
return new Response(JSON.stringify({ id: '1', name: 'Test User' }), {
|
|
85
|
+
status: 200,
|
|
86
|
+
headers: { 'content-type': 'application/json' },
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
// Use type assertion to work around vitest typecheck strict mode
|
|
91
|
+
const options = makeQueryOptions(endpoint, {
|
|
92
|
+
processResponse: (data: any) => {
|
|
93
|
+
if ('error' in data) {
|
|
94
|
+
return { type: 'error' as const, message: data.error }
|
|
95
|
+
}
|
|
96
|
+
if ('notFound' in data) {
|
|
97
|
+
return { type: 'notFound' as const }
|
|
98
|
+
}
|
|
99
|
+
if ('serverError' in data) {
|
|
100
|
+
return { type: 'serverError' as const, message: data.serverError }
|
|
101
|
+
}
|
|
102
|
+
return { type: 'success' as const, user: data }
|
|
103
|
+
},
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
const queryOptions = options({ urlParams: { userId: '1' } })
|
|
107
|
+
const result = await (queryOptions as any).queryFn?.({
|
|
108
|
+
queryKey: ['', 'users', '$userId'],
|
|
109
|
+
signal: new AbortController().signal,
|
|
110
|
+
meta: undefined,
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
expect(result).toEqual({
|
|
114
|
+
type: 'success',
|
|
115
|
+
user: { id: '1', name: 'Test User' },
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('should pass error response (400) through processResponse', async () => {
|
|
120
|
+
adapter.mock('/users/2', 'GET', () => {
|
|
121
|
+
return new Response(
|
|
122
|
+
JSON.stringify({ error: 'Invalid user ID', code: 400 }),
|
|
123
|
+
{
|
|
124
|
+
status: 400,
|
|
125
|
+
headers: { 'content-type': 'application/json' },
|
|
126
|
+
},
|
|
127
|
+
)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
const options = makeQueryOptions(endpoint, {
|
|
131
|
+
processResponse: (data: any) => {
|
|
132
|
+
if ('error' in data) {
|
|
133
|
+
return {
|
|
134
|
+
type: 'error' as const,
|
|
135
|
+
message: data.error,
|
|
136
|
+
code: data.code,
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if ('notFound' in data) {
|
|
140
|
+
return { type: 'notFound' as const }
|
|
141
|
+
}
|
|
142
|
+
if ('serverError' in data) {
|
|
143
|
+
return { type: 'serverError' as const, message: data.serverError }
|
|
144
|
+
}
|
|
145
|
+
return { type: 'success' as const, user: data }
|
|
146
|
+
},
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
const queryOptions = options({ urlParams: { userId: '2' } })
|
|
150
|
+
const result = await (queryOptions as any).queryFn({
|
|
151
|
+
queryKey: ['users', '2'],
|
|
152
|
+
signal: new AbortController().signal,
|
|
153
|
+
meta: undefined,
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
expect(result).toEqual({
|
|
157
|
+
type: 'error',
|
|
158
|
+
message: 'Invalid user ID',
|
|
159
|
+
code: 400,
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('should pass error response (404) through processResponse', async () => {
|
|
164
|
+
adapter.mock('/users/999', 'GET', () => {
|
|
165
|
+
return new Response(JSON.stringify({ notFound: true }), {
|
|
166
|
+
status: 404,
|
|
167
|
+
headers: { 'content-type': 'application/json' },
|
|
168
|
+
})
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
const options = makeQueryOptions(endpoint, {
|
|
172
|
+
processResponse: (data) => {
|
|
173
|
+
if ('error' in data) {
|
|
174
|
+
return { type: 'error' as const, message: data.error }
|
|
175
|
+
}
|
|
176
|
+
if ('notFound' in data) {
|
|
177
|
+
return { type: 'notFound' as const }
|
|
178
|
+
}
|
|
179
|
+
if ('serverError' in data) {
|
|
180
|
+
return { type: 'serverError' as const, message: data.serverError }
|
|
181
|
+
}
|
|
182
|
+
return { type: 'success' as const, user: data }
|
|
183
|
+
},
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
const queryOptions = options({ urlParams: { userId: '999' } })
|
|
187
|
+
const result = await (queryOptions as any).queryFn({
|
|
188
|
+
queryKey: ['users', '999'],
|
|
189
|
+
signal: new AbortController().signal,
|
|
190
|
+
meta: undefined,
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
expect(result).toEqual({ type: 'notFound' })
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
it('should call onFail for actual errors (not API error responses)', async () => {
|
|
197
|
+
adapter.mock('/users/error', 'GET', () => {
|
|
198
|
+
throw new Error('Network error')
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
const onFail = vi.fn()
|
|
202
|
+
const options = makeQueryOptions(endpoint, {
|
|
203
|
+
processResponse: (data) => data,
|
|
204
|
+
onFail,
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
const queryOptions = options({ urlParams: { userId: 'error' } })
|
|
208
|
+
|
|
209
|
+
await expect(
|
|
210
|
+
(queryOptions as any).queryFn({
|
|
211
|
+
queryKey: ['users', 'error'],
|
|
212
|
+
signal: new AbortController().signal,
|
|
213
|
+
meta: undefined,
|
|
214
|
+
}),
|
|
215
|
+
).rejects.toThrow('Network error')
|
|
216
|
+
|
|
217
|
+
expect(onFail).toHaveBeenCalledTimes(1)
|
|
218
|
+
expect(onFail.mock.calls[0][0]).toBeInstanceOf(Error)
|
|
219
|
+
})
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
describe('makeMutation with errorSchema', () => {
|
|
223
|
+
const mutationEndpoint = api.declareEndpoint({
|
|
224
|
+
method: 'POST',
|
|
225
|
+
url: '/users' as const,
|
|
226
|
+
requestSchema,
|
|
227
|
+
responseSchema,
|
|
228
|
+
errorSchema,
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
it('should return success response as data', async () => {
|
|
232
|
+
adapter.mock('/users', 'POST', () => {
|
|
233
|
+
return new Response(JSON.stringify({ id: '1', name: 'Created User' }), {
|
|
234
|
+
status: 201,
|
|
235
|
+
headers: { 'content-type': 'application/json' },
|
|
236
|
+
})
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
const mutation = makeMutation(mutationEndpoint, {
|
|
240
|
+
processResponse: (data) => {
|
|
241
|
+
if ('error' in data) {
|
|
242
|
+
return { ok: false as const, error: data.error }
|
|
243
|
+
}
|
|
244
|
+
if ('notFound' in data) {
|
|
245
|
+
return { ok: false as const, error: 'Not found' }
|
|
246
|
+
}
|
|
247
|
+
if ('serverError' in data) {
|
|
248
|
+
return { ok: false as const, error: data.serverError }
|
|
249
|
+
}
|
|
250
|
+
return { ok: true as const, user: data }
|
|
251
|
+
},
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
const mutationResult = mutation(undefined as never)
|
|
255
|
+
const result = await mutationResult.mutateAsync({
|
|
256
|
+
data: { name: 'New User' },
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
expect(result).toEqual({
|
|
260
|
+
ok: true,
|
|
261
|
+
user: { id: '1', name: 'Created User' },
|
|
262
|
+
})
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
it('should return error response (400) as data (not thrown)', async () => {
|
|
266
|
+
adapter.mock('/users', 'POST', () => {
|
|
267
|
+
return new Response(
|
|
268
|
+
JSON.stringify({ error: 'Name is required', code: 400 }),
|
|
269
|
+
{
|
|
270
|
+
status: 400,
|
|
271
|
+
headers: { 'content-type': 'application/json' },
|
|
272
|
+
},
|
|
273
|
+
)
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
const mutation = makeMutation(mutationEndpoint, {
|
|
277
|
+
processResponse: (data) => {
|
|
278
|
+
if ('error' in data) {
|
|
279
|
+
return { ok: false as const, error: data.error, code: data.code }
|
|
280
|
+
}
|
|
281
|
+
if ('notFound' in data) {
|
|
282
|
+
return { ok: false as const, error: 'Not found' }
|
|
283
|
+
}
|
|
284
|
+
if ('serverError' in data) {
|
|
285
|
+
return { ok: false as const, error: data.serverError }
|
|
286
|
+
}
|
|
287
|
+
return { ok: true as const, user: data }
|
|
288
|
+
},
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
const mutationResult = mutation(undefined as never)
|
|
292
|
+
const result = await mutationResult.mutateAsync({
|
|
293
|
+
data: { name: '' },
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
// Error response is returned, not thrown
|
|
297
|
+
expect(result).toEqual({
|
|
298
|
+
ok: false,
|
|
299
|
+
error: 'Name is required',
|
|
300
|
+
code: 400,
|
|
301
|
+
})
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
it('should call onSuccess with error response data (when error response is returned)', async () => {
|
|
305
|
+
adapter.mock('/users', 'POST', () => {
|
|
306
|
+
return new Response(
|
|
307
|
+
JSON.stringify({ error: 'Validation failed', code: 400 }),
|
|
308
|
+
{
|
|
309
|
+
status: 400,
|
|
310
|
+
headers: { 'content-type': 'application/json' },
|
|
311
|
+
},
|
|
312
|
+
)
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
const onSuccess = vi.fn()
|
|
316
|
+
|
|
317
|
+
const mutation = makeMutation(mutationEndpoint, {
|
|
318
|
+
processResponse: (data) => data,
|
|
319
|
+
onSuccess,
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
const mutationResult = mutation(undefined as never)
|
|
323
|
+
await mutationResult.mutateAsync({
|
|
324
|
+
data: { name: 'Test' },
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
expect(onSuccess).toHaveBeenCalledTimes(1)
|
|
328
|
+
expect(onSuccess.mock.calls[0][0]).toEqual({
|
|
329
|
+
error: 'Validation failed',
|
|
330
|
+
code: 400,
|
|
331
|
+
__status: 400,
|
|
332
|
+
})
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
it('should call onError for actual network errors (not API error responses)', async () => {
|
|
336
|
+
adapter.mock('/users', 'POST', () => {
|
|
337
|
+
throw new Error('Network failure')
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
const onError = vi.fn()
|
|
341
|
+
const onSuccess = vi.fn()
|
|
342
|
+
|
|
343
|
+
const mutation = makeMutation(mutationEndpoint, {
|
|
344
|
+
processResponse: (data) => data,
|
|
345
|
+
onError,
|
|
346
|
+
onSuccess,
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
const mutationResult = mutation(undefined as never)
|
|
350
|
+
|
|
351
|
+
await expect(
|
|
352
|
+
mutationResult.mutateAsync({
|
|
353
|
+
data: { name: 'Test' },
|
|
354
|
+
}),
|
|
355
|
+
).rejects.toThrow('Network failure')
|
|
356
|
+
|
|
357
|
+
expect(onError).toHaveBeenCalledTimes(1)
|
|
358
|
+
expect(onSuccess).not.toHaveBeenCalled()
|
|
359
|
+
})
|
|
360
|
+
})
|
|
361
|
+
|
|
362
|
+
describe('endpoints without errorSchema', () => {
|
|
363
|
+
const noErrorSchemaEndpoint = api.declareEndpoint({
|
|
364
|
+
method: 'GET',
|
|
365
|
+
url: '/simple' as const,
|
|
366
|
+
responseSchema,
|
|
367
|
+
})
|
|
368
|
+
|
|
369
|
+
it('should work without errorSchema (backwards compatibility)', async () => {
|
|
370
|
+
adapter.mock('/simple', 'GET', () => {
|
|
371
|
+
return new Response(JSON.stringify({ id: '1', name: 'Simple' }), {
|
|
372
|
+
status: 200,
|
|
373
|
+
headers: { 'content-type': 'application/json' },
|
|
374
|
+
})
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
const options = makeQueryOptions(noErrorSchemaEndpoint, {
|
|
378
|
+
processResponse: (data) => data,
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
const queryOptions = options({})
|
|
382
|
+
const result = await (queryOptions as any).queryFn({
|
|
383
|
+
queryKey: ['simple'],
|
|
384
|
+
signal: new AbortController().signal,
|
|
385
|
+
meta: undefined,
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
expect(result).toEqual({ id: '1', name: 'Simple' })
|
|
389
|
+
})
|
|
390
|
+
})
|
|
391
|
+
})
|
|
@@ -59,6 +59,7 @@ describe('makeMutation', () => {
|
|
|
59
59
|
z.object({ success: z.literal(true), test: z.string() }),
|
|
60
60
|
z.object({ success: z.literal(false), message: z.string() }),
|
|
61
61
|
])
|
|
62
|
+
type ResponseType = z.output<typeof responseSchema>
|
|
62
63
|
const endpoint = api.declareEndpoint({
|
|
63
64
|
method: 'POST',
|
|
64
65
|
url: '/test/$testId/foo/$fooId' as const,
|
|
@@ -87,7 +88,7 @@ describe('makeMutation', () => {
|
|
|
87
88
|
|
|
88
89
|
it('should just work', async () => {
|
|
89
90
|
const mutation = makeMutation(endpoint, {
|
|
90
|
-
processResponse: (data) => {
|
|
91
|
+
processResponse: (data: ResponseType) => {
|
|
91
92
|
if (!data.success) {
|
|
92
93
|
throw new Error(data.message)
|
|
93
94
|
}
|
|
@@ -138,7 +139,7 @@ describe('makeMutation', () => {
|
|
|
138
139
|
|
|
139
140
|
it('should work with a key', async () => {
|
|
140
141
|
const mutation = makeMutation(endpoint, {
|
|
141
|
-
processResponse: (data) => {
|
|
142
|
+
processResponse: (data: ResponseType) => {
|
|
142
143
|
if (!data.success) {
|
|
143
144
|
throw new Error(data.message)
|
|
144
145
|
}
|
|
@@ -245,7 +246,7 @@ describe('makeMutation', () => {
|
|
|
245
246
|
})
|
|
246
247
|
|
|
247
248
|
const mutation = makeMutation(endpoint, {
|
|
248
|
-
processResponse: (data) => {
|
|
249
|
+
processResponse: (data: ResponseType) => {
|
|
249
250
|
if (!data.success) throw new Error(data.message)
|
|
250
251
|
return data
|
|
251
252
|
},
|
|
@@ -280,7 +281,7 @@ describe('makeMutation', () => {
|
|
|
280
281
|
const onSettled = vi.fn()
|
|
281
282
|
|
|
282
283
|
const mutation = makeMutation(endpoint, {
|
|
283
|
-
processResponse: (data) => {
|
|
284
|
+
processResponse: (data: ResponseType) => {
|
|
284
285
|
if (!data.success) throw new Error(data.message)
|
|
285
286
|
return data
|
|
286
287
|
},
|
|
@@ -323,7 +324,7 @@ describe('makeMutation', () => {
|
|
|
323
324
|
const onSuccess = vi.fn()
|
|
324
325
|
|
|
325
326
|
const mutation = makeMutation(endpoint, {
|
|
326
|
-
processResponse: (data) => {
|
|
327
|
+
processResponse: (data: ResponseType) => {
|
|
327
328
|
if (!data.success) throw new Error(data.message)
|
|
328
329
|
return data
|
|
329
330
|
},
|
|
@@ -46,6 +46,7 @@ describe('makeDataTag', () => {
|
|
|
46
46
|
z.object({ success: z.literal(true), test: z.string() }),
|
|
47
47
|
z.object({ success: z.literal(false), message: z.string() }),
|
|
48
48
|
])
|
|
49
|
+
type ResponseType = z.output<typeof responseSchema>
|
|
49
50
|
const endpoint = api.declareEndpoint({
|
|
50
51
|
method: 'GET',
|
|
51
52
|
url: '/test/$testId/foo/$fooId' as const,
|
|
@@ -53,7 +54,7 @@ describe('makeDataTag', () => {
|
|
|
53
54
|
})
|
|
54
55
|
|
|
55
56
|
const result = makeQueryOptions(endpoint, {
|
|
56
|
-
processResponse(data) {
|
|
57
|
+
processResponse(data: ResponseType) {
|
|
57
58
|
if (!data.success) {
|
|
58
59
|
throw new Error(data.message)
|
|
59
60
|
}
|
|
@@ -11,6 +11,7 @@ describe('makeQueryOptions', () => {
|
|
|
11
11
|
z.object({ success: z.literal(true), test: z.string() }),
|
|
12
12
|
z.object({ success: z.literal(false), message: z.string() }),
|
|
13
13
|
])
|
|
14
|
+
type ResponseType = z.output<typeof responseSchema>
|
|
14
15
|
const endpoint = api.declareEndpoint({
|
|
15
16
|
method: 'GET',
|
|
16
17
|
url: '/test/$testId/foo/$fooId' as const,
|
|
@@ -21,7 +22,7 @@ describe('makeQueryOptions', () => {
|
|
|
21
22
|
const makeOptions = makeQueryOptions(
|
|
22
23
|
endpoint,
|
|
23
24
|
{
|
|
24
|
-
processResponse: (data) => {
|
|
25
|
+
processResponse: (data: ResponseType) => {
|
|
25
26
|
if (!data.success) {
|
|
26
27
|
throw new Error(data.message)
|
|
27
28
|
}
|