@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.
- package/CHANGELOG.md +166 -0
- package/README.md +152 -4
- package/dist/src/__tests__/errorSchema.spec.d.mts +2 -0
- package/dist/src/__tests__/errorSchema.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 +30 -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 +166 -0
- package/dist/src/mutation/optimistic.d.mts.map +1 -0
- package/dist/src/mutation/types.d.mts +51 -19
- 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 +35 -17
- package/dist/src/query/types.d.mts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/index.cjs +454 -37
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +1021 -598
- package/lib/index.d.cts.map +1 -1
- package/lib/index.d.mts +1019 -596
- package/lib/index.d.mts.map +1 -1
- package/lib/index.mjs +441 -29
- package/lib/index.mjs.map +1 -1
- package/package.json +8 -8
- package/src/__tests__/declare-client.spec.mts +1 -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/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 +345 -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 +48 -3
- package/src/mutation/index.mts +1 -0
- package/src/mutation/make-hook.mts +171 -63
- package/src/mutation/optimistic.mts +294 -0
- package/src/mutation/types.mts +102 -29
- 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 +76 -16
- package/dist/src/declare-client.d.mts +0 -31
- package/dist/src/declare-client.d.mts.map +0 -1
- package/dist/src/make-infinite-query-options.d.mts +0 -13
- package/dist/src/make-infinite-query-options.d.mts.map +0 -1
- package/dist/src/make-mutation.d.mts +0 -15
- package/dist/src/make-mutation.d.mts.map +0 -1
- package/dist/src/make-query-options.d.mts +0 -15
- package/dist/src/make-query-options.d.mts.map +0 -1
- package/dist/src/types/client-endpoint-helper.d.mts +0 -8
- package/dist/src/types/client-endpoint-helper.d.mts.map +0 -1
- package/dist/src/types/client-instance.d.mts +0 -211
- package/dist/src/types/client-instance.d.mts.map +0 -1
- package/dist/src/types/index.d.mts +0 -8
- package/dist/src/types/index.d.mts.map +0 -1
- package/dist/src/types/mutation-args.d.mts +0 -10
- package/dist/src/types/mutation-args.d.mts.map +0 -1
- package/dist/src/types/mutation-helpers.d.mts +0 -10
- package/dist/src/types/mutation-helpers.d.mts.map +0 -1
- package/dist/src/types/query-args.d.mts +0 -8
- package/dist/src/types/query-args.d.mts.map +0 -1
- package/dist/src/types/query-helpers.d.mts +0 -14
- package/dist/src/types/query-helpers.d.mts.map +0 -1
- package/dist/src/types/query-url-params-args.d.mts +0 -5
- package/dist/src/types/query-url-params-args.d.mts.map +0 -1
- package/dist/src/types.d.mts +0 -49
- package/dist/src/types.d.mts.map +0 -1
- package/dist/src/utils/mutation-key.creator.d.mts +0 -39
- package/dist/src/utils/mutation-key.creator.d.mts.map +0 -1
- package/dist/src/utils/query-key-creator.d.mts +0 -24
- package/dist/src/utils/query-key-creator.d.mts.map +0 -1
- package/src/client/__type-tests__/client-instance.spec-d.mts +0 -852
|
@@ -3,7 +3,12 @@ import type {
|
|
|
3
3
|
AbstractStream,
|
|
4
4
|
AnyEndpointConfig,
|
|
5
5
|
AnyStreamConfig,
|
|
6
|
+
BaseEndpointOptions,
|
|
7
|
+
EndpointHandler,
|
|
8
|
+
EndpointOptions,
|
|
9
|
+
ErrorSchemaRecord,
|
|
6
10
|
HttpMethod,
|
|
11
|
+
StreamHandler,
|
|
7
12
|
} from '@navios/builder'
|
|
8
13
|
import type {
|
|
9
14
|
InfiniteData,
|
|
@@ -17,6 +22,7 @@ import type {
|
|
|
17
22
|
} from '../common/types.mjs'
|
|
18
23
|
import type { MutationArgs } from '../mutation/types.mjs'
|
|
19
24
|
import type { ClientInstance } from './types.mjs'
|
|
25
|
+
import type { ComputeBaseResult } from './types/helpers.mjs'
|
|
20
26
|
|
|
21
27
|
import { makeMutation } from '../mutation/make-hook.mjs'
|
|
22
28
|
import { makeInfiniteQueryOptions } from '../query/make-infinite-options.mjs'
|
|
@@ -26,39 +32,47 @@ import { makeQueryOptions } from '../query/make-options.mjs'
|
|
|
26
32
|
* Configuration for declaring a query endpoint.
|
|
27
33
|
*/
|
|
28
34
|
export interface QueryConfig<
|
|
29
|
-
Method = HttpMethod,
|
|
30
|
-
Url = string,
|
|
31
|
-
QuerySchema =
|
|
35
|
+
Method extends HttpMethod = HttpMethod,
|
|
36
|
+
Url extends string = string,
|
|
37
|
+
QuerySchema extends ZodObject | undefined = undefined,
|
|
32
38
|
Response extends ZodType = ZodType,
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
ErrorSchema extends ErrorSchemaRecord | undefined = undefined,
|
|
40
|
+
Result = ComputeBaseResult<true, Response, ErrorSchema>,
|
|
41
|
+
RequestSchema extends ZodType | undefined = undefined,
|
|
35
42
|
> {
|
|
36
43
|
method: Method
|
|
37
44
|
url: Url
|
|
38
45
|
querySchema?: QuerySchema
|
|
39
46
|
responseSchema: Response
|
|
47
|
+
errorSchema?: ErrorSchema
|
|
40
48
|
requestSchema?: RequestSchema
|
|
41
|
-
processResponse?: (
|
|
49
|
+
processResponse?: (
|
|
50
|
+
data: ComputeBaseResult<true, Response, ErrorSchema>,
|
|
51
|
+
) => Result
|
|
42
52
|
}
|
|
43
53
|
|
|
44
54
|
/**
|
|
45
55
|
* Configuration for declaring an infinite query endpoint.
|
|
46
56
|
*/
|
|
47
57
|
export type InfiniteQueryConfig<
|
|
48
|
-
Method = HttpMethod,
|
|
49
|
-
Url = string,
|
|
58
|
+
Method extends HttpMethod = HttpMethod,
|
|
59
|
+
Url extends string = string,
|
|
50
60
|
QuerySchema extends ZodObject = ZodObject,
|
|
51
61
|
Response extends ZodType = ZodType,
|
|
52
|
-
|
|
62
|
+
ErrorSchema extends ErrorSchemaRecord | undefined = undefined,
|
|
63
|
+
PageResult = ComputeBaseResult<true, Response, ErrorSchema>,
|
|
53
64
|
Result = InfiniteData<PageResult>,
|
|
54
|
-
RequestSchema =
|
|
65
|
+
RequestSchema extends ZodType | undefined = undefined,
|
|
55
66
|
> = {
|
|
56
67
|
method: Method
|
|
57
68
|
url: Url
|
|
58
69
|
querySchema: QuerySchema
|
|
59
70
|
responseSchema: Response
|
|
71
|
+
errorSchema?: ErrorSchema
|
|
60
72
|
requestSchema?: RequestSchema
|
|
61
|
-
processResponse?: (
|
|
73
|
+
processResponse?: (
|
|
74
|
+
data: ComputeBaseResult<true, Response, ErrorSchema>,
|
|
75
|
+
) => PageResult
|
|
62
76
|
select?: (data: InfiniteData<PageResult>) => Result
|
|
63
77
|
getNextPageParam: (
|
|
64
78
|
lastPage: PageResult,
|
|
@@ -85,10 +99,13 @@ export interface MutationConfig<
|
|
|
85
99
|
| 'PATCH'
|
|
86
100
|
| 'DELETE',
|
|
87
101
|
Url extends string = string,
|
|
88
|
-
RequestSchema = Method extends 'DELETE'
|
|
89
|
-
|
|
102
|
+
RequestSchema extends ZodType | undefined = Method extends 'DELETE'
|
|
103
|
+
? undefined
|
|
104
|
+
: ZodType,
|
|
105
|
+
QuerySchema extends ZodObject | undefined = undefined,
|
|
90
106
|
Response extends ZodType = ZodType,
|
|
91
|
-
|
|
107
|
+
ErrorSchema extends ErrorSchemaRecord | undefined = undefined,
|
|
108
|
+
ReqResult = ComputeBaseResult<true, Response, ErrorSchema>,
|
|
92
109
|
Result = unknown,
|
|
93
110
|
TOnMutateResult = unknown,
|
|
94
111
|
Context = unknown,
|
|
@@ -98,6 +115,7 @@ export interface MutationConfig<
|
|
|
98
115
|
url: Url
|
|
99
116
|
querySchema?: QuerySchema
|
|
100
117
|
responseSchema: Response
|
|
118
|
+
errorSchema?: ErrorSchema
|
|
101
119
|
requestSchema?: RequestSchema
|
|
102
120
|
processResponse: ProcessResponseFunction<Result, ReqResult>
|
|
103
121
|
useContext?: () => Context
|
|
@@ -131,12 +149,15 @@ export interface MutationConfig<
|
|
|
131
149
|
/**
|
|
132
150
|
* Creates a client instance for making type-safe queries and mutations.
|
|
133
151
|
*
|
|
152
|
+
* @template UseDiscriminator - When `true`, errors are returned as union types.
|
|
153
|
+
* When `false` (default), errors are thrown and not included in TData.
|
|
154
|
+
*
|
|
134
155
|
* @param options - Client configuration including the API builder and defaults
|
|
135
156
|
* @returns A client instance with query, infiniteQuery, and mutation methods
|
|
136
157
|
*
|
|
137
158
|
* @example
|
|
138
159
|
* ```typescript
|
|
139
|
-
* const api =
|
|
160
|
+
* const api = builder({});
|
|
140
161
|
* const client = declareClient({ api });
|
|
141
162
|
*
|
|
142
163
|
* const getUser = client.query({
|
|
@@ -149,18 +170,18 @@ export interface MutationConfig<
|
|
|
149
170
|
* const { data } = useSuspenseQuery(getUser({ urlParams: { id: '123' } }));
|
|
150
171
|
* ```
|
|
151
172
|
*/
|
|
152
|
-
export function declareClient<
|
|
173
|
+
export function declareClient<UseDiscriminator extends boolean = false>({
|
|
153
174
|
api,
|
|
154
175
|
defaults = {},
|
|
155
|
-
}:
|
|
176
|
+
}: ClientOptions<UseDiscriminator>): ClientInstance<UseDiscriminator> {
|
|
156
177
|
function query(config: QueryConfig) {
|
|
157
178
|
const endpoint = api.declareEndpoint({
|
|
158
|
-
// @ts-expect-error we accept only specific methods
|
|
159
179
|
method: config.method,
|
|
160
180
|
url: config.url,
|
|
161
181
|
querySchema: config.querySchema,
|
|
162
182
|
requestSchema: config.requestSchema,
|
|
163
183
|
responseSchema: config.responseSchema,
|
|
184
|
+
errorSchema: config.errorSchema,
|
|
164
185
|
})
|
|
165
186
|
|
|
166
187
|
const queryOptions = makeQueryOptions(endpoint, {
|
|
@@ -173,14 +194,16 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
173
194
|
}
|
|
174
195
|
|
|
175
196
|
function queryFromEndpoint(
|
|
176
|
-
endpoint:
|
|
197
|
+
endpoint:
|
|
198
|
+
| AbstractEndpoint<AnyEndpointConfig>
|
|
199
|
+
| EndpointHandler<EndpointOptions, UseDiscriminator>,
|
|
177
200
|
options?: {
|
|
178
201
|
processResponse?: (
|
|
179
202
|
data: z.output<AnyEndpointConfig['responseSchema']>,
|
|
180
203
|
) => unknown
|
|
181
204
|
},
|
|
182
205
|
) {
|
|
183
|
-
return makeQueryOptions(endpoint, {
|
|
206
|
+
return makeQueryOptions(endpoint as any, {
|
|
184
207
|
...defaults,
|
|
185
208
|
processResponse: options?.processResponse ?? ((data) => data),
|
|
186
209
|
})
|
|
@@ -188,16 +211,16 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
188
211
|
|
|
189
212
|
function infiniteQuery(config: InfiniteQueryConfig) {
|
|
190
213
|
const endpoint = api.declareEndpoint({
|
|
191
|
-
// @ts-expect-error we accept only specific methods
|
|
192
214
|
method: config.method,
|
|
193
215
|
url: config.url,
|
|
194
216
|
querySchema: config.querySchema,
|
|
195
217
|
requestSchema: config.requestSchema,
|
|
196
218
|
responseSchema: config.responseSchema,
|
|
219
|
+
errorSchema: config.errorSchema,
|
|
197
220
|
})
|
|
198
221
|
const infiniteQueryOptions = makeInfiniteQueryOptions(endpoint, {
|
|
199
222
|
...defaults,
|
|
200
|
-
processResponse: config.processResponse ?? ((data) => data),
|
|
223
|
+
processResponse: config.processResponse ?? ((data: unknown) => data),
|
|
201
224
|
getNextPageParam: config.getNextPageParam,
|
|
202
225
|
getPreviousPageParam: config.getPreviousPageParam,
|
|
203
226
|
initialPageParam: config.initialPageParam,
|
|
@@ -209,7 +232,9 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
209
232
|
}
|
|
210
233
|
|
|
211
234
|
function infiniteQueryFromEndpoint(
|
|
212
|
-
endpoint:
|
|
235
|
+
endpoint:
|
|
236
|
+
| AbstractEndpoint<AnyEndpointConfig>
|
|
237
|
+
| EndpointHandler<EndpointOptions, UseDiscriminator>,
|
|
213
238
|
options: {
|
|
214
239
|
processResponse?: (
|
|
215
240
|
data: z.output<AnyEndpointConfig['responseSchema']>,
|
|
@@ -240,21 +265,22 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
240
265
|
|
|
241
266
|
function mutation(config: MutationConfig) {
|
|
242
267
|
const endpoint = api.declareEndpoint({
|
|
243
|
-
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
244
268
|
method: config.method,
|
|
245
269
|
url: config.url,
|
|
246
270
|
querySchema: config.querySchema,
|
|
247
271
|
requestSchema: config.requestSchema,
|
|
248
272
|
responseSchema: config.responseSchema,
|
|
273
|
+
errorSchema: config.errorSchema,
|
|
249
274
|
})
|
|
250
275
|
|
|
276
|
+
// @ts-expect-error Type inference for errorSchema variants
|
|
251
277
|
const useMutation = makeMutation(endpoint, {
|
|
252
|
-
processResponse: config.processResponse ?? ((data) => data),
|
|
278
|
+
processResponse: config.processResponse ?? ((data: unknown) => data),
|
|
253
279
|
useContext: config.useContext,
|
|
254
|
-
|
|
280
|
+
onMutate: config.onMutate,
|
|
255
281
|
onSuccess: config.onSuccess,
|
|
256
|
-
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
257
282
|
onError: config.onError,
|
|
283
|
+
onSettled: config.onSettled,
|
|
258
284
|
useKey: config.useKey,
|
|
259
285
|
meta: config.meta,
|
|
260
286
|
...defaults,
|
|
@@ -268,7 +294,9 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
268
294
|
function mutationFromEndpoint(
|
|
269
295
|
endpoint:
|
|
270
296
|
| AbstractEndpoint<AnyEndpointConfig>
|
|
271
|
-
| AbstractStream<AnyStreamConfig
|
|
297
|
+
| AbstractStream<AnyStreamConfig>
|
|
298
|
+
| EndpointHandler<EndpointOptions, UseDiscriminator>
|
|
299
|
+
| StreamHandler<BaseEndpointOptions, UseDiscriminator>,
|
|
272
300
|
options?: {
|
|
273
301
|
processResponse?: ProcessResponseFunction
|
|
274
302
|
useContext?: () => unknown
|
|
@@ -321,24 +349,21 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
321
349
|
|
|
322
350
|
function multipartMutation(config: MutationConfig) {
|
|
323
351
|
const endpoint = api.declareMultipart({
|
|
324
|
-
// @ts-expect-error we accept only specific methods
|
|
325
352
|
method: config.method,
|
|
326
353
|
url: config.url,
|
|
327
354
|
querySchema: config.querySchema,
|
|
328
355
|
requestSchema: config.requestSchema,
|
|
329
356
|
responseSchema: config.responseSchema,
|
|
357
|
+
errorSchema: config.errorSchema,
|
|
330
358
|
})
|
|
331
359
|
|
|
360
|
+
// @ts-expect-error Type inference for errorSchema variants
|
|
332
361
|
const useMutation = makeMutation(endpoint, {
|
|
333
|
-
processResponse: config.processResponse ?? ((data) => data),
|
|
362
|
+
processResponse: config.processResponse ?? ((data: unknown) => data),
|
|
334
363
|
useContext: config.useContext,
|
|
335
|
-
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
336
364
|
onSuccess: config.onSuccess,
|
|
337
|
-
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
338
365
|
onError: config.onError,
|
|
339
|
-
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
340
366
|
onMutate: config.onMutate,
|
|
341
|
-
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
342
367
|
onSettled: config.onSettled,
|
|
343
368
|
useKey: config.useKey,
|
|
344
369
|
...defaults,
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BaseEndpointConfig,
|
|
3
|
+
BaseEndpointOptions,
|
|
4
|
+
EndpointOptions,
|
|
5
|
+
InferEndpointParams,
|
|
6
|
+
RequestArgs,
|
|
7
|
+
Simplify,
|
|
8
|
+
UrlHasParams,
|
|
9
|
+
UrlParams,
|
|
10
|
+
} from '@navios/builder'
|
|
11
|
+
import type {
|
|
12
|
+
DataTag,
|
|
13
|
+
InfiniteData,
|
|
14
|
+
MutationFunctionContext,
|
|
15
|
+
UseMutationResult,
|
|
16
|
+
UseSuspenseInfiniteQueryOptions,
|
|
17
|
+
UseSuspenseQueryOptions,
|
|
18
|
+
} from '@tanstack/react-query'
|
|
19
|
+
import type { z, ZodObject, ZodType } from 'zod/v4'
|
|
20
|
+
|
|
21
|
+
import type { Split } from '../../common/types.mjs'
|
|
22
|
+
import type { MutationHelpers } from '../../mutation/types.mjs'
|
|
23
|
+
import type { QueryHelpers } from '../../query/types.mjs'
|
|
24
|
+
import type {
|
|
25
|
+
ComputeBaseResult,
|
|
26
|
+
EndpointHelper,
|
|
27
|
+
StreamHelper,
|
|
28
|
+
} from './helpers.mjs'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Helper type to extract useKey from mutation options
|
|
32
|
+
*/
|
|
33
|
+
type ExtractUseKey<Options> = Options extends { useKey: infer U }
|
|
34
|
+
? U extends true
|
|
35
|
+
? true
|
|
36
|
+
: false
|
|
37
|
+
: false
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* FromEndpoint methods using const generics pattern (simplified from multiple overloads).
|
|
41
|
+
*
|
|
42
|
+
* @template UseDiscriminator - When `true`, errors are returned as union types.
|
|
43
|
+
* When `false` (default), errors are thrown and not included in TData.
|
|
44
|
+
*/
|
|
45
|
+
export interface ClientFromEndpointMethods<
|
|
46
|
+
UseDiscriminator extends boolean = false,
|
|
47
|
+
> {
|
|
48
|
+
/**
|
|
49
|
+
* Creates a type-safe query from an existing endpoint with automatic type inference.
|
|
50
|
+
*
|
|
51
|
+
* Uses const generic pattern to infer types from the endpoint configuration.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const getUserEndpoint = api.declareEndpoint({
|
|
56
|
+
* method: 'GET',
|
|
57
|
+
* url: '/users/$userId',
|
|
58
|
+
* responseSchema: userSchema,
|
|
59
|
+
* })
|
|
60
|
+
*
|
|
61
|
+
* const getUser = client.queryFromEndpoint(getUserEndpoint, {
|
|
62
|
+
* processResponse: (data) => data,
|
|
63
|
+
* })
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
queryFromEndpoint<
|
|
67
|
+
const Config extends EndpointOptions,
|
|
68
|
+
TBaseResult = ComputeBaseResult<
|
|
69
|
+
UseDiscriminator,
|
|
70
|
+
Config['responseSchema'],
|
|
71
|
+
Config['errorSchema']
|
|
72
|
+
>,
|
|
73
|
+
Result = TBaseResult,
|
|
74
|
+
>(
|
|
75
|
+
endpoint: { config: Config },
|
|
76
|
+
options?: {
|
|
77
|
+
processResponse?: (data: TBaseResult) => Result
|
|
78
|
+
},
|
|
79
|
+
): ((
|
|
80
|
+
params: Simplify<InferEndpointParams<Config>>,
|
|
81
|
+
) => UseSuspenseQueryOptions<
|
|
82
|
+
Result,
|
|
83
|
+
Error,
|
|
84
|
+
Result,
|
|
85
|
+
DataTag<Split<Config['url'], '/'>, Result, Error>
|
|
86
|
+
>) &
|
|
87
|
+
QueryHelpers<
|
|
88
|
+
Config['url'],
|
|
89
|
+
Config['querySchema'] extends ZodObject
|
|
90
|
+
? Config['querySchema']
|
|
91
|
+
: undefined,
|
|
92
|
+
Result,
|
|
93
|
+
false,
|
|
94
|
+
Config['requestSchema'] extends ZodType
|
|
95
|
+
? Config['requestSchema']
|
|
96
|
+
: undefined
|
|
97
|
+
>
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Creates a type-safe infinite query from an existing endpoint with automatic type inference.
|
|
101
|
+
*
|
|
102
|
+
* Uses const generic pattern to infer types from the endpoint configuration.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* const getUsersEndpoint = api.declareEndpoint({
|
|
107
|
+
* method: 'GET',
|
|
108
|
+
* url: '/users',
|
|
109
|
+
* querySchema: z.object({ page: z.number() }),
|
|
110
|
+
* responseSchema: z.array(userSchema),
|
|
111
|
+
* })
|
|
112
|
+
*
|
|
113
|
+
* const getUsers = client.infiniteQueryFromEndpoint(getUsersEndpoint, {
|
|
114
|
+
* getNextPageParam: (lastPage, allPages, lastPageParam) => {
|
|
115
|
+
* return lastPage.length > 0 ? { page: (lastPageParam?.page ?? 0) + 1 } : undefined
|
|
116
|
+
* },
|
|
117
|
+
* })
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
infiniteQueryFromEndpoint<
|
|
121
|
+
const Config extends EndpointOptions & {
|
|
122
|
+
querySchema: ZodObject
|
|
123
|
+
},
|
|
124
|
+
TBaseResult = ComputeBaseResult<
|
|
125
|
+
UseDiscriminator,
|
|
126
|
+
Config['responseSchema'],
|
|
127
|
+
Config['errorSchema']
|
|
128
|
+
>,
|
|
129
|
+
PageResult = TBaseResult,
|
|
130
|
+
Result = InfiniteData<PageResult>,
|
|
131
|
+
>(
|
|
132
|
+
endpoint: { config: Config },
|
|
133
|
+
options: {
|
|
134
|
+
processResponse?: (data: TBaseResult) => PageResult
|
|
135
|
+
getNextPageParam: (
|
|
136
|
+
lastPage: PageResult,
|
|
137
|
+
allPages: PageResult[],
|
|
138
|
+
lastPageParam: z.infer<Config['querySchema']> | undefined,
|
|
139
|
+
allPageParams: z.infer<Config['querySchema']>[] | undefined,
|
|
140
|
+
) => z.input<Config['querySchema']> | undefined
|
|
141
|
+
getPreviousPageParam?: (
|
|
142
|
+
firstPage: PageResult,
|
|
143
|
+
allPages: PageResult[],
|
|
144
|
+
lastPageParam: z.infer<Config['querySchema']> | undefined,
|
|
145
|
+
allPageParams: z.infer<Config['querySchema']>[] | undefined,
|
|
146
|
+
) => z.input<Config['querySchema']>
|
|
147
|
+
},
|
|
148
|
+
): ((
|
|
149
|
+
params: Simplify<InferEndpointParams<Config>>,
|
|
150
|
+
) => UseSuspenseInfiniteQueryOptions<
|
|
151
|
+
PageResult,
|
|
152
|
+
Error,
|
|
153
|
+
Result,
|
|
154
|
+
DataTag<Split<Config['url'], '/'>, PageResult, Error>,
|
|
155
|
+
z.output<Config['querySchema']>
|
|
156
|
+
>) &
|
|
157
|
+
QueryHelpers<
|
|
158
|
+
Config['url'],
|
|
159
|
+
Config['querySchema'],
|
|
160
|
+
PageResult,
|
|
161
|
+
true,
|
|
162
|
+
Config['requestSchema'] extends ZodType
|
|
163
|
+
? Config['requestSchema']
|
|
164
|
+
: undefined
|
|
165
|
+
>
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Creates a type-safe mutation from an existing endpoint with automatic type inference.
|
|
169
|
+
*
|
|
170
|
+
* Uses const generic pattern to infer types from the endpoint configuration.
|
|
171
|
+
* Handles both regular endpoints and stream endpoints.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* const createUserEndpoint = api.declareEndpoint({
|
|
176
|
+
* method: 'POST',
|
|
177
|
+
* url: '/users',
|
|
178
|
+
* requestSchema: createUserSchema,
|
|
179
|
+
* responseSchema: userSchema,
|
|
180
|
+
* })
|
|
181
|
+
*
|
|
182
|
+
* const createUser = client.mutationFromEndpoint(createUserEndpoint, {
|
|
183
|
+
* processResponse: (data) => data,
|
|
184
|
+
* })
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
mutationFromEndpoint<
|
|
188
|
+
const Config extends EndpointOptions | BaseEndpointOptions,
|
|
189
|
+
TBaseResult = Config extends EndpointOptions
|
|
190
|
+
? ComputeBaseResult<
|
|
191
|
+
UseDiscriminator,
|
|
192
|
+
Config['responseSchema'],
|
|
193
|
+
Config['errorSchema']
|
|
194
|
+
>
|
|
195
|
+
: Blob,
|
|
196
|
+
Result = Config extends EndpointOptions
|
|
197
|
+
? ComputeBaseResult<
|
|
198
|
+
UseDiscriminator,
|
|
199
|
+
Config['responseSchema'],
|
|
200
|
+
Config['errorSchema']
|
|
201
|
+
>
|
|
202
|
+
: Blob,
|
|
203
|
+
OnMutateResult = unknown,
|
|
204
|
+
Context = unknown,
|
|
205
|
+
>(
|
|
206
|
+
endpoint: { config: Config },
|
|
207
|
+
mutationOptions?: {
|
|
208
|
+
processResponse?: (data: TBaseResult) => Result | Promise<Result>
|
|
209
|
+
useContext?: () => Context
|
|
210
|
+
useKey?: boolean
|
|
211
|
+
onMutate?: (
|
|
212
|
+
variables: Simplify<
|
|
213
|
+
RequestArgs<
|
|
214
|
+
Config['url'],
|
|
215
|
+
Config['querySchema'] extends ZodObject
|
|
216
|
+
? Config['querySchema']
|
|
217
|
+
: undefined,
|
|
218
|
+
Config['requestSchema'] extends ZodType
|
|
219
|
+
? Config['requestSchema']
|
|
220
|
+
: undefined,
|
|
221
|
+
Config['urlParamsSchema'] extends ZodObject
|
|
222
|
+
? Config['urlParamsSchema']
|
|
223
|
+
: undefined
|
|
224
|
+
>
|
|
225
|
+
>,
|
|
226
|
+
context: Context & MutationFunctionContext,
|
|
227
|
+
) => OnMutateResult | Promise<OnMutateResult>
|
|
228
|
+
onSuccess?: (
|
|
229
|
+
data: NoInfer<Result>,
|
|
230
|
+
variables: Simplify<
|
|
231
|
+
RequestArgs<
|
|
232
|
+
Config['url'],
|
|
233
|
+
Config['querySchema'] extends ZodObject
|
|
234
|
+
? Config['querySchema']
|
|
235
|
+
: undefined,
|
|
236
|
+
Config['requestSchema'] extends ZodType
|
|
237
|
+
? Config['requestSchema']
|
|
238
|
+
: undefined,
|
|
239
|
+
Config['urlParamsSchema'] extends ZodObject
|
|
240
|
+
? Config['urlParamsSchema']
|
|
241
|
+
: undefined
|
|
242
|
+
>
|
|
243
|
+
>,
|
|
244
|
+
context: Context &
|
|
245
|
+
MutationFunctionContext & {
|
|
246
|
+
onMutateResult: OnMutateResult | undefined
|
|
247
|
+
},
|
|
248
|
+
) => void | Promise<void>
|
|
249
|
+
onError?: (
|
|
250
|
+
error: Error,
|
|
251
|
+
variables: Simplify<
|
|
252
|
+
RequestArgs<
|
|
253
|
+
Config['url'],
|
|
254
|
+
Config['querySchema'] extends ZodObject
|
|
255
|
+
? Config['querySchema']
|
|
256
|
+
: undefined,
|
|
257
|
+
Config['requestSchema'] extends ZodType
|
|
258
|
+
? Config['requestSchema']
|
|
259
|
+
: undefined,
|
|
260
|
+
Config['urlParamsSchema'] extends ZodObject
|
|
261
|
+
? Config['urlParamsSchema']
|
|
262
|
+
: undefined
|
|
263
|
+
>
|
|
264
|
+
>,
|
|
265
|
+
context: Context &
|
|
266
|
+
MutationFunctionContext & {
|
|
267
|
+
onMutateResult: OnMutateResult | undefined
|
|
268
|
+
},
|
|
269
|
+
) => void | Promise<void>
|
|
270
|
+
onSettled?: (
|
|
271
|
+
data: NoInfer<Result> | undefined,
|
|
272
|
+
error: Error | null,
|
|
273
|
+
variables: Simplify<
|
|
274
|
+
RequestArgs<
|
|
275
|
+
Config['url'],
|
|
276
|
+
Config['querySchema'] extends ZodObject
|
|
277
|
+
? Config['querySchema']
|
|
278
|
+
: undefined,
|
|
279
|
+
Config['requestSchema'] extends ZodType
|
|
280
|
+
? Config['requestSchema']
|
|
281
|
+
: undefined,
|
|
282
|
+
Config['urlParamsSchema'] extends ZodObject
|
|
283
|
+
? Config['urlParamsSchema']
|
|
284
|
+
: undefined
|
|
285
|
+
>
|
|
286
|
+
>,
|
|
287
|
+
context: Context &
|
|
288
|
+
MutationFunctionContext & {
|
|
289
|
+
onMutateResult: OnMutateResult | undefined
|
|
290
|
+
},
|
|
291
|
+
) => void | Promise<void>
|
|
292
|
+
},
|
|
293
|
+
): ((
|
|
294
|
+
...args: ExtractUseKey<typeof mutationOptions> extends true
|
|
295
|
+
? UrlHasParams<Config['url']> extends true
|
|
296
|
+
? [{ urlParams: UrlParams<Config['url']> }]
|
|
297
|
+
: []
|
|
298
|
+
: []
|
|
299
|
+
) => UseMutationResult<
|
|
300
|
+
Result,
|
|
301
|
+
Error,
|
|
302
|
+
Simplify<
|
|
303
|
+
RequestArgs<
|
|
304
|
+
Config['url'],
|
|
305
|
+
Config['querySchema'] extends ZodObject
|
|
306
|
+
? Config['querySchema']
|
|
307
|
+
: undefined,
|
|
308
|
+
Config['requestSchema'] extends ZodType
|
|
309
|
+
? Config['requestSchema']
|
|
310
|
+
: undefined,
|
|
311
|
+
Config['urlParamsSchema'] extends ZodObject
|
|
312
|
+
? Config['urlParamsSchema']
|
|
313
|
+
: undefined
|
|
314
|
+
>
|
|
315
|
+
>,
|
|
316
|
+
OnMutateResult
|
|
317
|
+
>) &
|
|
318
|
+
(ExtractUseKey<typeof mutationOptions> extends true
|
|
319
|
+
? MutationHelpers<Config['url'], Result>
|
|
320
|
+
: {}) &
|
|
321
|
+
(Config extends EndpointOptions
|
|
322
|
+
? EndpointHelper<
|
|
323
|
+
{
|
|
324
|
+
method: Config['method']
|
|
325
|
+
url: Config['url']
|
|
326
|
+
responseSchema: Config['responseSchema']
|
|
327
|
+
querySchema: Config['querySchema']
|
|
328
|
+
requestSchema: Config['requestSchema']
|
|
329
|
+
errorSchema: Config['errorSchema']
|
|
330
|
+
urlParamsSchema: Config['urlParamsSchema']
|
|
331
|
+
},
|
|
332
|
+
UseDiscriminator
|
|
333
|
+
>
|
|
334
|
+
: StreamHelper<
|
|
335
|
+
{
|
|
336
|
+
method: Config['method']
|
|
337
|
+
url: Config['url']
|
|
338
|
+
querySchema: Config['querySchema']
|
|
339
|
+
requestSchema: Config['requestSchema']
|
|
340
|
+
errorSchema: Config['errorSchema']
|
|
341
|
+
urlParamsSchema: Config['urlParamsSchema']
|
|
342
|
+
},
|
|
343
|
+
UseDiscriminator
|
|
344
|
+
>)
|
|
345
|
+
}
|