@navios/react-query 0.1.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/README.md +370 -0
- package/dist/index.d.mts +346 -0
- package/dist/index.d.ts +346 -0
- package/dist/index.js +345 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +325 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +43 -0
- package/src/__tests__/client-type-check.spec.mts +61 -0
- package/src/__tests__/declare-client.spec.mts +187 -0
- package/src/__tests__/make-mutation.spec.mts +140 -0
- package/src/__tests__/makeDataTag.spec.mts +70 -0
- package/src/__tests__/makeInfiniteQueryOptions.spec.mts +45 -0
- package/src/__tests__/makeQueryOptions.spec.mts +43 -0
- package/src/declare-client.mts +215 -0
- package/src/index.mts +8 -0
- package/src/make-infinite-query-options.mts +115 -0
- package/src/make-mutation.mts +121 -0
- package/src/make-query-options.mts +113 -0
- package/src/types/client-instance.mts +502 -0
- package/src/types/index.mts +6 -0
- package/src/types/mutation-args.mts +10 -0
- package/src/types/mutation-helpers.mts +13 -0
- package/src/types/query-args.mts +8 -0
- package/src/types/query-helpers.mts +34 -0
- package/src/types/query-url-params-args.mts +6 -0
- package/src/types.mts +118 -0
- package/src/utils/mutation-key.creator.mts +61 -0
- package/src/utils/query-key-creator.mts +125 -0
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BaseEndpointConfig,
|
|
3
|
+
UrlHasParams,
|
|
4
|
+
UrlParams,
|
|
5
|
+
Util_FlatObject,
|
|
6
|
+
} from '@navios/common'
|
|
7
|
+
import type {
|
|
8
|
+
DataTag,
|
|
9
|
+
InfiniteData,
|
|
10
|
+
QueryClient,
|
|
11
|
+
UseMutationResult,
|
|
12
|
+
UseSuspenseInfiniteQueryOptions,
|
|
13
|
+
UseSuspenseQueryOptions,
|
|
14
|
+
} from '@tanstack/react-query'
|
|
15
|
+
import type { AnyZodObject, z, ZodType } from 'zod'
|
|
16
|
+
|
|
17
|
+
import type { ProcessResponseFunction, Split } from '../types.mjs'
|
|
18
|
+
import type { ClientMutationArgs } from './mutation-args.mjs'
|
|
19
|
+
import type { MutationHelpers } from './mutation-helpers.mjs'
|
|
20
|
+
import type { ClientQueryArgs } from './query-args.mjs'
|
|
21
|
+
import type { QueryHelpers } from './query-helpers.mjs'
|
|
22
|
+
|
|
23
|
+
export interface ClientInstance {
|
|
24
|
+
query<
|
|
25
|
+
Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET',
|
|
26
|
+
Url extends string = string,
|
|
27
|
+
Response extends ZodType = ZodType,
|
|
28
|
+
Result = z.output<Response>,
|
|
29
|
+
>(config: {
|
|
30
|
+
method: Method
|
|
31
|
+
url: Url
|
|
32
|
+
responseSchema: Response
|
|
33
|
+
processResponse?: (data: z.output<Response>) => Result
|
|
34
|
+
}): ((
|
|
35
|
+
params: Util_FlatObject<ClientQueryArgs<Url, undefined>>,
|
|
36
|
+
) => UseSuspenseQueryOptions<
|
|
37
|
+
Result,
|
|
38
|
+
Error,
|
|
39
|
+
Result,
|
|
40
|
+
DataTag<Split<Url, '/'>, Result, Error>
|
|
41
|
+
>) &
|
|
42
|
+
QueryHelpers<Url, undefined, Result>
|
|
43
|
+
|
|
44
|
+
query<
|
|
45
|
+
Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET',
|
|
46
|
+
Url extends string = string,
|
|
47
|
+
QuerySchema extends AnyZodObject = AnyZodObject,
|
|
48
|
+
Response extends ZodType = ZodType,
|
|
49
|
+
Result = z.output<Response>,
|
|
50
|
+
>(config: {
|
|
51
|
+
method: Method
|
|
52
|
+
url: Url
|
|
53
|
+
querySchema: QuerySchema
|
|
54
|
+
responseSchema: Response
|
|
55
|
+
processResponse?: (data: z.output<Response>) => Result
|
|
56
|
+
}): ((
|
|
57
|
+
params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>,
|
|
58
|
+
) => UseSuspenseQueryOptions<
|
|
59
|
+
Result,
|
|
60
|
+
Error,
|
|
61
|
+
Result,
|
|
62
|
+
DataTag<Split<Url, '/'>, Result, Error>
|
|
63
|
+
>) &
|
|
64
|
+
QueryHelpers<Url, QuerySchema, Result>
|
|
65
|
+
|
|
66
|
+
infiniteQuery<
|
|
67
|
+
Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET',
|
|
68
|
+
Url extends string = string,
|
|
69
|
+
QuerySchema extends AnyZodObject = AnyZodObject,
|
|
70
|
+
Response extends ZodType = ZodType,
|
|
71
|
+
PageResult = z.output<Response>,
|
|
72
|
+
Result = InfiniteData<PageResult>,
|
|
73
|
+
>(config: {
|
|
74
|
+
method: Method
|
|
75
|
+
url: Url
|
|
76
|
+
querySchema: QuerySchema
|
|
77
|
+
responseSchema: Response
|
|
78
|
+
processResponse?: (data: z.output<Response>) => PageResult
|
|
79
|
+
getNextPageParam: (
|
|
80
|
+
lastPage: PageResult,
|
|
81
|
+
allPages: PageResult[],
|
|
82
|
+
lastPageParam: z.infer<QuerySchema> | undefined,
|
|
83
|
+
allPageParams: z.infer<QuerySchema>[] | undefined,
|
|
84
|
+
) => z.input<QuerySchema> | undefined
|
|
85
|
+
getPreviousPageParam?: (
|
|
86
|
+
firstPage: PageResult,
|
|
87
|
+
allPages: PageResult[],
|
|
88
|
+
lastPageParam: z.infer<QuerySchema> | undefined,
|
|
89
|
+
allPageParams: z.infer<QuerySchema>[] | undefined,
|
|
90
|
+
) => z.input<QuerySchema>
|
|
91
|
+
}): ((
|
|
92
|
+
params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>,
|
|
93
|
+
) => UseSuspenseInfiniteQueryOptions<
|
|
94
|
+
PageResult,
|
|
95
|
+
Error,
|
|
96
|
+
Result,
|
|
97
|
+
PageResult,
|
|
98
|
+
DataTag<Split<Url, '/'>, PageResult, Error>,
|
|
99
|
+
z.output<QuerySchema>
|
|
100
|
+
>) &
|
|
101
|
+
QueryHelpers<Url, QuerySchema, PageResult, true>
|
|
102
|
+
|
|
103
|
+
mutation<
|
|
104
|
+
Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH',
|
|
105
|
+
Url extends string = string,
|
|
106
|
+
RequestSchema extends ZodType = ZodType,
|
|
107
|
+
QuerySchema extends AnyZodObject = AnyZodObject,
|
|
108
|
+
Response extends ZodType = ZodType,
|
|
109
|
+
ReqResult = z.output<Response>,
|
|
110
|
+
Result = unknown,
|
|
111
|
+
Context = unknown,
|
|
112
|
+
UseKey extends true = true,
|
|
113
|
+
>(config: {
|
|
114
|
+
method: Method
|
|
115
|
+
url: Url
|
|
116
|
+
useKey: UseKey
|
|
117
|
+
requestSchema: RequestSchema
|
|
118
|
+
querySchema: QuerySchema
|
|
119
|
+
responseSchema: Response
|
|
120
|
+
processResponse: ProcessResponseFunction<Result, ReqResult>
|
|
121
|
+
useContext?: () => Context
|
|
122
|
+
onSuccess?: (
|
|
123
|
+
queryClient: QueryClient,
|
|
124
|
+
data: NoInfer<Result>,
|
|
125
|
+
variables: Util_FlatObject<
|
|
126
|
+
ClientMutationArgs<Url, RequestSchema, QuerySchema>
|
|
127
|
+
>,
|
|
128
|
+
context: Context,
|
|
129
|
+
) => void | Promise<void>
|
|
130
|
+
onError?: (
|
|
131
|
+
queryClient: QueryClient,
|
|
132
|
+
error: Error,
|
|
133
|
+
variables: Util_FlatObject<
|
|
134
|
+
ClientMutationArgs<Url, RequestSchema, QuerySchema>
|
|
135
|
+
>,
|
|
136
|
+
context: Context,
|
|
137
|
+
) => void | Promise<void>
|
|
138
|
+
}): ((
|
|
139
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
140
|
+
) => UseMutationResult<
|
|
141
|
+
Result,
|
|
142
|
+
Error,
|
|
143
|
+
ClientMutationArgs<Url, RequestSchema, QuerySchema>
|
|
144
|
+
>) &
|
|
145
|
+
MutationHelpers<Url, Result>
|
|
146
|
+
|
|
147
|
+
mutation<
|
|
148
|
+
Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH',
|
|
149
|
+
Url extends string = string,
|
|
150
|
+
RequestSchema extends ZodType = ZodType,
|
|
151
|
+
QuerySchema extends AnyZodObject = AnyZodObject,
|
|
152
|
+
Response extends ZodType = ZodType,
|
|
153
|
+
ReqResult = z.output<Response>,
|
|
154
|
+
Result = unknown,
|
|
155
|
+
Context = unknown,
|
|
156
|
+
>(config: {
|
|
157
|
+
method: Method
|
|
158
|
+
url: Url
|
|
159
|
+
requestSchema: RequestSchema
|
|
160
|
+
querySchema: QuerySchema
|
|
161
|
+
responseSchema: Response
|
|
162
|
+
processResponse: ProcessResponseFunction<Result, ReqResult>
|
|
163
|
+
useContext?: () => Context
|
|
164
|
+
onSuccess?: (
|
|
165
|
+
queryClient: QueryClient,
|
|
166
|
+
data: NoInfer<Result>,
|
|
167
|
+
variables: Util_FlatObject<
|
|
168
|
+
ClientMutationArgs<Url, RequestSchema, QuerySchema>
|
|
169
|
+
>,
|
|
170
|
+
context: Context,
|
|
171
|
+
) => void | Promise<void>
|
|
172
|
+
onError?: (
|
|
173
|
+
queryClient: QueryClient,
|
|
174
|
+
error: Error,
|
|
175
|
+
variables: Util_FlatObject<
|
|
176
|
+
ClientMutationArgs<Url, RequestSchema, QuerySchema>
|
|
177
|
+
>,
|
|
178
|
+
context: Context,
|
|
179
|
+
) => void | Promise<void>
|
|
180
|
+
}): (
|
|
181
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
182
|
+
) => UseMutationResult<
|
|
183
|
+
Result,
|
|
184
|
+
Error,
|
|
185
|
+
ClientMutationArgs<Url, RequestSchema, QuerySchema>
|
|
186
|
+
>
|
|
187
|
+
|
|
188
|
+
mutation<
|
|
189
|
+
Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH',
|
|
190
|
+
Url extends string = string,
|
|
191
|
+
RequestSchema extends ZodType = ZodType,
|
|
192
|
+
Response extends ZodType = ZodType,
|
|
193
|
+
ReqResult = z.output<Response>,
|
|
194
|
+
Result = unknown,
|
|
195
|
+
Context = unknown,
|
|
196
|
+
>(config: {
|
|
197
|
+
method: Method
|
|
198
|
+
url: Url
|
|
199
|
+
requestSchema: RequestSchema
|
|
200
|
+
responseSchema: Response
|
|
201
|
+
processResponse: ProcessResponseFunction<Result, ReqResult>
|
|
202
|
+
useContext?: () => Context
|
|
203
|
+
onSuccess?: (
|
|
204
|
+
queryClient: QueryClient,
|
|
205
|
+
data: NoInfer<Result>,
|
|
206
|
+
variables: Util_FlatObject<
|
|
207
|
+
ClientMutationArgs<Url, RequestSchema, undefined>
|
|
208
|
+
>,
|
|
209
|
+
context: Context,
|
|
210
|
+
) => void | Promise<void>
|
|
211
|
+
onError?: (
|
|
212
|
+
queryClient: QueryClient,
|
|
213
|
+
error: Error,
|
|
214
|
+
variables: Util_FlatObject<
|
|
215
|
+
ClientMutationArgs<Url, RequestSchema, undefined>
|
|
216
|
+
>,
|
|
217
|
+
context: Context,
|
|
218
|
+
) => void | Promise<void>
|
|
219
|
+
}): ((
|
|
220
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
221
|
+
) => UseMutationResult<
|
|
222
|
+
Result,
|
|
223
|
+
Error,
|
|
224
|
+
ClientMutationArgs<Url, RequestSchema, undefined>
|
|
225
|
+
>) &
|
|
226
|
+
MutationHelpers<Url, Result>
|
|
227
|
+
|
|
228
|
+
mutation<
|
|
229
|
+
Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH',
|
|
230
|
+
Url extends string = string,
|
|
231
|
+
RequestSchema extends ZodType = ZodType,
|
|
232
|
+
Response extends ZodType = ZodType,
|
|
233
|
+
ReqResult = z.output<Response>,
|
|
234
|
+
Result = unknown,
|
|
235
|
+
Context = unknown,
|
|
236
|
+
UseKey extends true = true,
|
|
237
|
+
>(config: {
|
|
238
|
+
method: Method
|
|
239
|
+
url: Url
|
|
240
|
+
useKey: UseKey
|
|
241
|
+
requestSchema: RequestSchema
|
|
242
|
+
responseSchema: Response
|
|
243
|
+
processResponse: ProcessResponseFunction<Result, ReqResult>
|
|
244
|
+
useContext?: () => Context
|
|
245
|
+
onSuccess?: (
|
|
246
|
+
queryClient: QueryClient,
|
|
247
|
+
data: NoInfer<Result>,
|
|
248
|
+
variables: Util_FlatObject<
|
|
249
|
+
ClientMutationArgs<Url, RequestSchema, undefined>
|
|
250
|
+
>,
|
|
251
|
+
context: Context,
|
|
252
|
+
) => void | Promise<void>
|
|
253
|
+
onError?: (
|
|
254
|
+
queryClient: QueryClient,
|
|
255
|
+
error: Error,
|
|
256
|
+
variables: Util_FlatObject<
|
|
257
|
+
ClientMutationArgs<Url, RequestSchema, undefined>
|
|
258
|
+
>,
|
|
259
|
+
context: Context,
|
|
260
|
+
) => void | Promise<void>
|
|
261
|
+
}): ((
|
|
262
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
263
|
+
) => UseMutationResult<
|
|
264
|
+
Result,
|
|
265
|
+
Error,
|
|
266
|
+
ClientMutationArgs<Url, RequestSchema, undefined>
|
|
267
|
+
>) &
|
|
268
|
+
MutationHelpers<Url, Result>
|
|
269
|
+
|
|
270
|
+
mutation<
|
|
271
|
+
Method extends 'DELETE' = 'DELETE',
|
|
272
|
+
Url extends string = string,
|
|
273
|
+
QuerySchema extends AnyZodObject = AnyZodObject,
|
|
274
|
+
Response extends ZodType = ZodType,
|
|
275
|
+
ReqResult = z.output<Response>,
|
|
276
|
+
Result = unknown,
|
|
277
|
+
Context = unknown,
|
|
278
|
+
UseKey extends true = true,
|
|
279
|
+
>(config: {
|
|
280
|
+
method: Method
|
|
281
|
+
url: Url
|
|
282
|
+
useKey: UseKey
|
|
283
|
+
querySchema: QuerySchema
|
|
284
|
+
responseSchema: Response
|
|
285
|
+
processResponse: ProcessResponseFunction<Result, ReqResult>
|
|
286
|
+
useContext?: () => Context
|
|
287
|
+
onSuccess?: (
|
|
288
|
+
queryClient: QueryClient,
|
|
289
|
+
data: NoInfer<Result>,
|
|
290
|
+
variables: Util_FlatObject<
|
|
291
|
+
ClientMutationArgs<Url, undefined, QuerySchema>
|
|
292
|
+
>,
|
|
293
|
+
context: Context,
|
|
294
|
+
) => void | Promise<void>
|
|
295
|
+
onError?: (
|
|
296
|
+
queryClient: QueryClient,
|
|
297
|
+
error: Error,
|
|
298
|
+
variables: Util_FlatObject<
|
|
299
|
+
ClientMutationArgs<Url, undefined, QuerySchema>
|
|
300
|
+
>,
|
|
301
|
+
context: Context,
|
|
302
|
+
) => void | Promise<void>
|
|
303
|
+
}): ((
|
|
304
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
305
|
+
) => UseMutationResult<
|
|
306
|
+
Result,
|
|
307
|
+
Error,
|
|
308
|
+
ClientMutationArgs<Url, undefined, QuerySchema>
|
|
309
|
+
>) &
|
|
310
|
+
MutationHelpers<Url, Result>
|
|
311
|
+
|
|
312
|
+
mutation<
|
|
313
|
+
Method extends 'DELETE' = 'DELETE',
|
|
314
|
+
Url extends string = string,
|
|
315
|
+
QuerySchema extends AnyZodObject = AnyZodObject,
|
|
316
|
+
Response extends ZodType = ZodType,
|
|
317
|
+
ReqResult = z.output<Response>,
|
|
318
|
+
Result = unknown,
|
|
319
|
+
Context = unknown,
|
|
320
|
+
>(config: {
|
|
321
|
+
method: Method
|
|
322
|
+
url: Url
|
|
323
|
+
querySchema: QuerySchema
|
|
324
|
+
responseSchema: Response
|
|
325
|
+
processResponse: ProcessResponseFunction<Result, ReqResult>
|
|
326
|
+
useContext?: () => Context
|
|
327
|
+
onSuccess?: (
|
|
328
|
+
queryClient: QueryClient,
|
|
329
|
+
data: NoInfer<Result>,
|
|
330
|
+
variables: Util_FlatObject<
|
|
331
|
+
ClientMutationArgs<Url, undefined, QuerySchema>
|
|
332
|
+
>,
|
|
333
|
+
context: Context,
|
|
334
|
+
) => void | Promise<void>
|
|
335
|
+
onError?: (
|
|
336
|
+
queryClient: QueryClient,
|
|
337
|
+
error: Error,
|
|
338
|
+
variables: Util_FlatObject<
|
|
339
|
+
ClientMutationArgs<Url, undefined, QuerySchema>
|
|
340
|
+
>,
|
|
341
|
+
context: Context,
|
|
342
|
+
) => void | Promise<void>
|
|
343
|
+
}): (
|
|
344
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
345
|
+
) => UseMutationResult<
|
|
346
|
+
Result,
|
|
347
|
+
Error,
|
|
348
|
+
ClientMutationArgs<Url, undefined, QuerySchema>
|
|
349
|
+
>
|
|
350
|
+
|
|
351
|
+
mutation<
|
|
352
|
+
Method extends 'DELETE' = 'DELETE',
|
|
353
|
+
Url extends string = string,
|
|
354
|
+
Response extends ZodType = ZodType,
|
|
355
|
+
ReqResult = z.output<Response>,
|
|
356
|
+
Result = unknown,
|
|
357
|
+
Context = unknown,
|
|
358
|
+
UseKey extends true = true,
|
|
359
|
+
>(config: {
|
|
360
|
+
method: Method
|
|
361
|
+
url: Url
|
|
362
|
+
useKey: UseKey
|
|
363
|
+
responseSchema: Response
|
|
364
|
+
processResponse: ProcessResponseFunction<Result, ReqResult>
|
|
365
|
+
useContext?: () => Context
|
|
366
|
+
onSuccess?: (
|
|
367
|
+
queryClient: QueryClient,
|
|
368
|
+
data: NoInfer<Result>,
|
|
369
|
+
variables: Util_FlatObject<ClientMutationArgs<Url, undefined, undefined>>,
|
|
370
|
+
context: Context,
|
|
371
|
+
) => void | Promise<void>
|
|
372
|
+
onError?: (
|
|
373
|
+
queryClient: QueryClient,
|
|
374
|
+
error: Error,
|
|
375
|
+
variables: Util_FlatObject<ClientMutationArgs<Url, undefined, undefined>>,
|
|
376
|
+
context: Context,
|
|
377
|
+
) => void | Promise<void>
|
|
378
|
+
}): ((
|
|
379
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
380
|
+
) => UseMutationResult<
|
|
381
|
+
Result,
|
|
382
|
+
Error,
|
|
383
|
+
ClientMutationArgs<Url, undefined, undefined>
|
|
384
|
+
>) &
|
|
385
|
+
MutationHelpers<Url, Result>
|
|
386
|
+
|
|
387
|
+
mutation<
|
|
388
|
+
Method extends 'DELETE' = 'DELETE',
|
|
389
|
+
Url extends string = string,
|
|
390
|
+
Response extends ZodType = ZodType,
|
|
391
|
+
ReqResult = z.output<Response>,
|
|
392
|
+
Result = unknown,
|
|
393
|
+
Context = unknown,
|
|
394
|
+
>(config: {
|
|
395
|
+
method: Method
|
|
396
|
+
url: Url
|
|
397
|
+
responseSchema: Response
|
|
398
|
+
processResponse: ProcessResponseFunction<Result, ReqResult>
|
|
399
|
+
useContext?: () => Context
|
|
400
|
+
onSuccess?: (
|
|
401
|
+
queryClient: QueryClient,
|
|
402
|
+
data: NoInfer<Result>,
|
|
403
|
+
variables: Util_FlatObject<ClientMutationArgs<Url, undefined, undefined>>,
|
|
404
|
+
context: Context,
|
|
405
|
+
) => void | Promise<void>
|
|
406
|
+
onError?: (
|
|
407
|
+
queryClient: QueryClient,
|
|
408
|
+
error: Error,
|
|
409
|
+
variables: Util_FlatObject<ClientMutationArgs<Url, undefined, undefined>>,
|
|
410
|
+
context: Context,
|
|
411
|
+
) => void | Promise<void>
|
|
412
|
+
}): (
|
|
413
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
414
|
+
) => UseMutationResult<
|
|
415
|
+
Result,
|
|
416
|
+
Error,
|
|
417
|
+
ClientMutationArgs<Url, undefined, undefined>
|
|
418
|
+
>
|
|
419
|
+
|
|
420
|
+
queryFromEndpoint<
|
|
421
|
+
Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET',
|
|
422
|
+
Url extends string = string,
|
|
423
|
+
QuerySchema extends AnyZodObject = AnyZodObject,
|
|
424
|
+
Response extends ZodType = ZodType,
|
|
425
|
+
Result = z.output<Response>,
|
|
426
|
+
>(
|
|
427
|
+
endpoint: any & {
|
|
428
|
+
config: BaseEndpointConfig<Method, Url, QuerySchema, Response>
|
|
429
|
+
},
|
|
430
|
+
options?: {
|
|
431
|
+
processResponse?: (data: z.output<Response>) => Result
|
|
432
|
+
},
|
|
433
|
+
): (
|
|
434
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
435
|
+
) => UseSuspenseQueryOptions<
|
|
436
|
+
Result,
|
|
437
|
+
Error,
|
|
438
|
+
Result,
|
|
439
|
+
DataTag<Split<Url, '/'>, Result, Error>
|
|
440
|
+
> &
|
|
441
|
+
QueryHelpers<Url, QuerySchema, Result>
|
|
442
|
+
|
|
443
|
+
queryFromEndpoint<
|
|
444
|
+
Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET',
|
|
445
|
+
Url extends string = string,
|
|
446
|
+
Response extends ZodType = ZodType,
|
|
447
|
+
Result = z.output<Response>,
|
|
448
|
+
>(
|
|
449
|
+
endpoint: any & {
|
|
450
|
+
config: BaseEndpointConfig<Method, Url, undefined, Response>
|
|
451
|
+
},
|
|
452
|
+
options?: {
|
|
453
|
+
processResponse?: (data: z.output<Response>) => Result
|
|
454
|
+
},
|
|
455
|
+
): (
|
|
456
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
457
|
+
) => UseSuspenseQueryOptions<
|
|
458
|
+
Result,
|
|
459
|
+
Error,
|
|
460
|
+
Result,
|
|
461
|
+
DataTag<Split<Url, '/'>, Result, Error>
|
|
462
|
+
> &
|
|
463
|
+
QueryHelpers<Url, undefined, Result>
|
|
464
|
+
|
|
465
|
+
infiniteQueryFromEndpoint<
|
|
466
|
+
Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET',
|
|
467
|
+
Url extends string = string,
|
|
468
|
+
QuerySchema extends AnyZodObject = AnyZodObject,
|
|
469
|
+
Response extends ZodType = ZodType,
|
|
470
|
+
PageResult = z.output<Response>,
|
|
471
|
+
Result = InfiniteData<PageResult>,
|
|
472
|
+
>(
|
|
473
|
+
endpoint: any & {
|
|
474
|
+
config: BaseEndpointConfig<Method, Url, QuerySchema, Response>
|
|
475
|
+
},
|
|
476
|
+
options: {
|
|
477
|
+
processResponse?: (data: z.output<Response>) => PageResult
|
|
478
|
+
getNextPageParam: (
|
|
479
|
+
lastPage: PageResult,
|
|
480
|
+
allPages: PageResult[],
|
|
481
|
+
lastPageParam: z.infer<QuerySchema> | undefined,
|
|
482
|
+
allPageParams: z.infer<QuerySchema>[] | undefined,
|
|
483
|
+
) => z.input<QuerySchema> | undefined
|
|
484
|
+
getPreviousPageParam?: (
|
|
485
|
+
firstPage: PageResult,
|
|
486
|
+
allPages: PageResult[],
|
|
487
|
+
lastPageParam: z.infer<QuerySchema> | undefined,
|
|
488
|
+
allPageParams: z.infer<QuerySchema>[] | undefined,
|
|
489
|
+
) => z.input<QuerySchema>
|
|
490
|
+
},
|
|
491
|
+
): (
|
|
492
|
+
params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
|
|
493
|
+
) => UseSuspenseInfiniteQueryOptions<
|
|
494
|
+
PageResult,
|
|
495
|
+
Error,
|
|
496
|
+
Result,
|
|
497
|
+
PageResult,
|
|
498
|
+
DataTag<Split<Url, '/'>, PageResult, Error>,
|
|
499
|
+
z.output<QuerySchema>
|
|
500
|
+
> &
|
|
501
|
+
QueryHelpers<Url, QuerySchema, PageResult, true>
|
|
502
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { UrlHasParams, UrlParams } from '@navios/common'
|
|
2
|
+
import type { AnyZodObject, z } from 'zod'
|
|
3
|
+
|
|
4
|
+
export type ClientMutationArgs<
|
|
5
|
+
Url extends string = string,
|
|
6
|
+
RequestSchema = unknown,
|
|
7
|
+
QuerySchema = unknown,
|
|
8
|
+
> = (UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {}) &
|
|
9
|
+
(RequestSchema extends AnyZodObject ? { data: z.input<RequestSchema> } : {}) &
|
|
10
|
+
(QuerySchema extends AnyZodObject ? { params: z.input<QuerySchema> } : {})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { UrlHasParams, UrlParams } from '@navios/common'
|
|
2
|
+
import type { DataTag } from '@tanstack/react-query'
|
|
3
|
+
|
|
4
|
+
export type MutationHelpers<Url extends string, Result = unknown> =
|
|
5
|
+
UrlHasParams<Url> extends true
|
|
6
|
+
? {
|
|
7
|
+
mutationKey: (params: UrlParams<Url>) => DataTag<[Url], Result, Error>
|
|
8
|
+
useIsMutating: (keyParams: UrlParams<Url>) => boolean
|
|
9
|
+
}
|
|
10
|
+
: {
|
|
11
|
+
mutationKey: () => DataTag<[Url], Result, Error>
|
|
12
|
+
useIsMutating: () => boolean
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { UrlHasParams, UrlParams } from '@navios/common'
|
|
2
|
+
import type { AnyZodObject, z } from 'zod'
|
|
3
|
+
|
|
4
|
+
export type ClientQueryArgs<
|
|
5
|
+
Url extends string = string,
|
|
6
|
+
QuerySchema = AnyZodObject,
|
|
7
|
+
> = (UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {}) &
|
|
8
|
+
(QuerySchema extends AnyZodObject ? { params: z.input<QuerySchema> } : {})
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Util_FlatObject } from '@navios/common'
|
|
2
|
+
import type {
|
|
3
|
+
QueryClient,
|
|
4
|
+
UseQueryResult,
|
|
5
|
+
UseSuspenseQueryResult,
|
|
6
|
+
} from '@tanstack/react-query'
|
|
7
|
+
import type { AnyZodObject } from 'zod'
|
|
8
|
+
|
|
9
|
+
import type { QueryKeyCreatorResult } from '../utils/query-key-creator.mjs'
|
|
10
|
+
import type { ClientQueryArgs } from './query-args.mjs'
|
|
11
|
+
import type { ClientQueryUrlParamsArgs } from './query-url-params-args.mjs'
|
|
12
|
+
|
|
13
|
+
export type QueryHelpers<
|
|
14
|
+
Url extends string,
|
|
15
|
+
QuerySchema extends AnyZodObject | undefined = undefined,
|
|
16
|
+
Result = undefined,
|
|
17
|
+
IsInfinite extends boolean = false,
|
|
18
|
+
> = {
|
|
19
|
+
queryKey: QueryKeyCreatorResult<QuerySchema, Url, Result, IsInfinite>
|
|
20
|
+
use: (
|
|
21
|
+
params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>,
|
|
22
|
+
) => UseQueryResult<Result, Error>
|
|
23
|
+
useSuspense: (
|
|
24
|
+
params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>,
|
|
25
|
+
) => UseSuspenseQueryResult<Result, Error>
|
|
26
|
+
invalidate: (
|
|
27
|
+
queryClient: QueryClient,
|
|
28
|
+
params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>,
|
|
29
|
+
) => () => Promise<void>
|
|
30
|
+
invalidateAll: (
|
|
31
|
+
queryClient: QueryClient,
|
|
32
|
+
params: Util_FlatObject<ClientQueryUrlParamsArgs<Url>>,
|
|
33
|
+
) => () => Promise<void>
|
|
34
|
+
}
|
package/src/types.mts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AnyEndpointConfig,
|
|
3
|
+
BaseEndpointConfig,
|
|
4
|
+
BuilderInstance,
|
|
5
|
+
HttpMethod,
|
|
6
|
+
NaviosZodRequest,
|
|
7
|
+
UrlHasParams,
|
|
8
|
+
UrlParams,
|
|
9
|
+
} from '@navios/common'
|
|
10
|
+
import type { QueryClient, UseMutationOptions } from '@tanstack/react-query'
|
|
11
|
+
import type { AnyZodObject, z } from 'zod'
|
|
12
|
+
|
|
13
|
+
export type Split<S extends string, D extends string> = string extends S
|
|
14
|
+
? string[]
|
|
15
|
+
: S extends ''
|
|
16
|
+
? []
|
|
17
|
+
: S extends `${infer T}${D}${infer U}`
|
|
18
|
+
? [T, ...Split<U, D>]
|
|
19
|
+
: [S]
|
|
20
|
+
|
|
21
|
+
export type ProcessResponseFunction<TData = unknown, TVariables = unknown> = (
|
|
22
|
+
variables: TVariables,
|
|
23
|
+
) => Promise<TData> | TData
|
|
24
|
+
|
|
25
|
+
export type ClientOptions<ProcessResponse = unknown> = {
|
|
26
|
+
api: BuilderInstance
|
|
27
|
+
defaults?: {
|
|
28
|
+
keyPrefix?: string[]
|
|
29
|
+
keySuffix?: string[]
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type BaseQueryParams<Config extends AnyEndpointConfig, Res = any> = {
|
|
34
|
+
keyPrefix?: string[]
|
|
35
|
+
keySuffix?: string[]
|
|
36
|
+
onFail?: (err: unknown) => void
|
|
37
|
+
processResponse: (data: z.output<Config['responseSchema']>) => Res
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface BaseMutationParams<
|
|
41
|
+
Config extends AnyEndpointConfig,
|
|
42
|
+
TData = unknown,
|
|
43
|
+
TVariables = BaseMutationArgs<Config>,
|
|
44
|
+
TResponse = z.output<Config['responseSchema']>,
|
|
45
|
+
TContext = unknown,
|
|
46
|
+
UseKey extends boolean = false,
|
|
47
|
+
> extends Omit<
|
|
48
|
+
UseMutationOptions<TData, Error, TVariables>,
|
|
49
|
+
'mutationKey' | 'mutationFn' | 'onSuccess' | 'onError' | 'scope'
|
|
50
|
+
> {
|
|
51
|
+
processResponse: ProcessResponseFunction<TData, TResponse>
|
|
52
|
+
/**
|
|
53
|
+
* React hooks that will prepare the context for the mutation onSuccess and onError
|
|
54
|
+
* callbacks. This is useful for when you want to use the context in the callbacks
|
|
55
|
+
*/
|
|
56
|
+
useContext?: () => TContext
|
|
57
|
+
onSuccess?: (
|
|
58
|
+
queryClient: QueryClient,
|
|
59
|
+
data: TData,
|
|
60
|
+
variables: TVariables,
|
|
61
|
+
context: TContext,
|
|
62
|
+
) => void | Promise<void>
|
|
63
|
+
onError?: (
|
|
64
|
+
queryClient: QueryClient,
|
|
65
|
+
err: unknown,
|
|
66
|
+
variables: TVariables,
|
|
67
|
+
context: TContext,
|
|
68
|
+
) => void | Promise<void>
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* If true, we will create a mutation key that can be shared across the project.
|
|
72
|
+
*/
|
|
73
|
+
useKey?: UseKey
|
|
74
|
+
keyPrefix?: UseKey extends true
|
|
75
|
+
? UrlHasParams<Config['url']> extends true
|
|
76
|
+
? string[]
|
|
77
|
+
: never
|
|
78
|
+
: never
|
|
79
|
+
keySuffix?: UseKey extends true
|
|
80
|
+
? UrlHasParams<Config['url']> extends true
|
|
81
|
+
? string[]
|
|
82
|
+
: never
|
|
83
|
+
: never
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type BaseQueryArgs<Config extends AnyEndpointConfig> = (UrlHasParams<
|
|
87
|
+
Config['url']
|
|
88
|
+
> extends true
|
|
89
|
+
? { urlParams: UrlParams<Config['url']> }
|
|
90
|
+
: {}) &
|
|
91
|
+
(Config['querySchema'] extends AnyZodObject
|
|
92
|
+
? { params: z.input<Config['querySchema']> }
|
|
93
|
+
: {})
|
|
94
|
+
|
|
95
|
+
export type BaseMutationArgs<Config extends AnyEndpointConfig> =
|
|
96
|
+
NaviosZodRequest<Config>
|
|
97
|
+
|
|
98
|
+
export type InfiniteQueryOptions<
|
|
99
|
+
Config extends BaseEndpointConfig<HttpMethod, string, AnyZodObject>,
|
|
100
|
+
Res = any,
|
|
101
|
+
> = {
|
|
102
|
+
keyPrefix?: string[]
|
|
103
|
+
keySuffix?: string[]
|
|
104
|
+
processResponse: (data: z.infer<Config['responseSchema']>) => Res
|
|
105
|
+
onFail?: (err: unknown) => void
|
|
106
|
+
getNextPageParam: (
|
|
107
|
+
lastPage: z.infer<Config['responseSchema']>,
|
|
108
|
+
allPages: z.infer<Config['responseSchema']>[],
|
|
109
|
+
lastPageParam: z.infer<Config['querySchema']> | undefined,
|
|
110
|
+
allPageParams: z.infer<Config['querySchema']>[] | undefined,
|
|
111
|
+
) =>
|
|
112
|
+
| z.input<Config['querySchema']>
|
|
113
|
+
| z.infer<Config['querySchema']>
|
|
114
|
+
| undefined
|
|
115
|
+
initialPageParam?:
|
|
116
|
+
| z.input<Config['querySchema']>
|
|
117
|
+
| z.infer<Config['querySchema']>
|
|
118
|
+
}
|