@navios/react-query 0.4.0 → 0.5.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 +74 -0
- package/dist/src/client/declare-client.d.mts +70 -0
- package/dist/src/client/declare-client.d.mts.map +1 -0
- package/dist/src/client/index.d.mts +3 -0
- package/dist/src/client/index.d.mts.map +1 -0
- package/dist/src/client/types.d.mts +287 -0
- package/dist/src/client/types.d.mts.map +1 -0
- package/dist/src/common/index.d.mts +2 -0
- package/dist/src/common/index.d.mts.map +1 -0
- package/dist/src/common/types.d.mts +21 -0
- package/dist/src/common/types.d.mts.map +1 -0
- package/dist/src/index.d.mts +13 -8
- package/dist/src/index.d.mts.map +1 -1
- package/dist/src/mutation/index.d.mts +4 -0
- package/dist/src/mutation/index.d.mts.map +1 -0
- package/dist/src/mutation/key-creator.d.mts +36 -0
- package/dist/src/mutation/key-creator.d.mts.map +1 -0
- package/dist/src/mutation/make-hook.d.mts +25 -0
- package/dist/src/mutation/make-hook.d.mts.map +1 -0
- package/dist/src/mutation/types.d.mts +50 -0
- package/dist/src/mutation/types.d.mts.map +1 -0
- package/dist/src/query/index.d.mts +5 -0
- package/dist/src/query/index.d.mts.map +1 -0
- package/dist/src/query/key-creator.d.mts +17 -0
- package/dist/src/query/key-creator.d.mts.map +1 -0
- package/dist/src/query/make-infinite-options.d.mts +23 -0
- package/dist/src/query/make-infinite-options.d.mts.map +1 -0
- package/dist/src/query/make-options.d.mts +24 -0
- package/dist/src/query/make-options.d.mts.map +1 -0
- package/dist/src/query/types.d.mts +83 -0
- package/dist/src/query/types.d.mts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/_tsup-dts-rollup.d.mts +500 -250
- package/lib/_tsup-dts-rollup.d.ts +500 -250
- package/lib/index.d.mts +26 -19
- package/lib/index.d.ts +26 -19
- package/lib/index.js +92 -66
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +92 -68
- package/lib/index.mjs.map +1 -1
- package/package.json +7 -7
- package/project.json +9 -1
- package/src/__tests__/client-type-check.spec.mts +1 -1
- package/src/__tests__/declare-client.spec.mts +1 -1
- package/src/__tests__/make-mutation.spec.mts +1 -1
- package/src/__tests__/makeDataTag.spec.mts +1 -1
- package/src/__tests__/makeInfiniteQueryOptions.spec.mts +1 -2
- package/src/__tests__/makeQueryOptions.spec.mts +1 -1
- package/src/{declare-client.mts → client/declare-client.mts} +93 -33
- package/src/client/index.mts +2 -0
- package/src/{types/client-instance.mts → client/types.mts} +429 -145
- package/src/common/index.mts +1 -0
- package/src/common/types.mts +31 -0
- package/src/index.mts +28 -8
- package/src/mutation/index.mts +3 -0
- package/src/mutation/key-creator.mts +64 -0
- package/src/{make-mutation.mts → mutation/make-hook.mts} +19 -8
- package/src/mutation/types.mts +106 -0
- package/src/query/index.mts +4 -0
- package/src/{utils/query-key-creator.mts → query/key-creator.mts} +22 -48
- package/src/{make-infinite-query-options.mts → query/make-infinite-options.mts} +28 -14
- package/src/{make-query-options.mts → query/make-options.mts} +26 -25
- package/src/query/types.mts +163 -0
- package/dist/tsdown.config.d.mts +0 -3
- package/dist/tsdown.config.d.mts.map +0 -1
- package/dist/tsup.config.d.mts +0 -3
- package/dist/tsup.config.d.mts.map +0 -1
- package/dist/vitest.config.d.mts +0 -3
- package/dist/vitest.config.d.mts.map +0 -1
- package/src/types/client-endpoint-helper.mts +0 -29
- package/src/types/index.mts +0 -7
- package/src/types/mutation-args.mts +0 -10
- package/src/types/mutation-helpers.mts +0 -13
- package/src/types/query-args.mts +0 -8
- package/src/types/query-helpers.mts +0 -34
- package/src/types/query-url-params-args.mts +0 -6
- package/src/types.mts +0 -118
- package/src/utils/mutation-key.creator.mts +0 -65
|
@@ -2,48 +2,54 @@ import type {
|
|
|
2
2
|
AbstractEndpoint,
|
|
3
3
|
AnyEndpointConfig,
|
|
4
4
|
HttpMethod,
|
|
5
|
-
Util_FlatObject,
|
|
6
5
|
} from '@navios/builder'
|
|
7
6
|
import type { InfiniteData, QueryClient } from '@tanstack/react-query'
|
|
8
7
|
import type { z, ZodObject, ZodType } from 'zod/v4'
|
|
9
8
|
|
|
10
|
-
import type { ClientOptions, ProcessResponseFunction } from '
|
|
11
|
-
import type {
|
|
9
|
+
import type { ClientOptions, ProcessResponseFunction } from '../common/types.mjs'
|
|
10
|
+
import type { MutationArgs } from '../mutation/types.mjs'
|
|
11
|
+
import type { ClientInstance } from './types.mjs'
|
|
12
12
|
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import { makeQueryOptions } from '
|
|
13
|
+
import { makeMutation } from '../mutation/make-hook.mjs'
|
|
14
|
+
import { makeInfiniteQueryOptions } from '../query/make-infinite-options.mjs'
|
|
15
|
+
import { makeQueryOptions } from '../query/make-options.mjs'
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Configuration for declaring a query endpoint.
|
|
19
|
+
*/
|
|
20
|
+
export interface QueryConfig<
|
|
18
21
|
Method = HttpMethod,
|
|
19
22
|
Url = string,
|
|
20
|
-
QuerySchema =
|
|
21
|
-
Response = ZodType,
|
|
23
|
+
QuerySchema = ZodObject,
|
|
24
|
+
Response extends ZodType = ZodType,
|
|
25
|
+
Result = z.output<Response>,
|
|
26
|
+
RequestSchema = unknown,
|
|
22
27
|
> {
|
|
23
28
|
method: Method
|
|
24
29
|
url: Url
|
|
25
30
|
querySchema?: QuerySchema
|
|
26
31
|
responseSchema: Response
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
export interface ClientQueryConfig<
|
|
30
|
-
Method = HttpMethod,
|
|
31
|
-
Url = string,
|
|
32
|
-
QuerySchema = ZodObject,
|
|
33
|
-
Response extends ZodType = ZodType,
|
|
34
|
-
Result = z.output<Response>,
|
|
35
|
-
> extends ClientEndpointDefinition<Method, Url, QuerySchema, Response> {
|
|
32
|
+
requestSchema?: RequestSchema
|
|
36
33
|
processResponse?: (data: z.output<Response>) => Result
|
|
37
34
|
}
|
|
38
35
|
|
|
39
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Configuration for declaring an infinite query endpoint.
|
|
38
|
+
*/
|
|
39
|
+
export type InfiniteQueryConfig<
|
|
40
40
|
Method = HttpMethod,
|
|
41
41
|
Url = string,
|
|
42
42
|
QuerySchema extends ZodObject = ZodObject,
|
|
43
43
|
Response extends ZodType = ZodType,
|
|
44
44
|
PageResult = z.output<Response>,
|
|
45
45
|
Result = InfiniteData<PageResult>,
|
|
46
|
-
|
|
46
|
+
RequestSchema = unknown,
|
|
47
|
+
> = {
|
|
48
|
+
method: Method
|
|
49
|
+
url: Url
|
|
50
|
+
querySchema: QuerySchema
|
|
51
|
+
responseSchema: Response
|
|
52
|
+
requestSchema?: RequestSchema
|
|
47
53
|
processResponse?: (data: z.output<Response>) => PageResult
|
|
48
54
|
select?: (data: InfiniteData<PageResult>) => Result
|
|
49
55
|
getNextPageParam: (
|
|
@@ -61,7 +67,10 @@ export type ClientInfiniteQueryConfig<
|
|
|
61
67
|
initialPageParam?: z.input<QuerySchema>
|
|
62
68
|
}
|
|
63
69
|
|
|
64
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Configuration for declaring a mutation endpoint.
|
|
72
|
+
*/
|
|
73
|
+
export interface MutationConfig<
|
|
65
74
|
Method extends 'POST' | 'PUT' | 'PATCH' | 'DELETE' =
|
|
66
75
|
| 'POST'
|
|
67
76
|
| 'PUT'
|
|
@@ -75,39 +84,61 @@ export interface ClientMutationDataConfig<
|
|
|
75
84
|
Result = unknown,
|
|
76
85
|
Context = unknown,
|
|
77
86
|
UseKey extends boolean = false,
|
|
78
|
-
>
|
|
87
|
+
> {
|
|
88
|
+
method: Method
|
|
89
|
+
url: Url
|
|
90
|
+
querySchema?: QuerySchema
|
|
91
|
+
responseSchema: Response
|
|
79
92
|
requestSchema?: RequestSchema
|
|
80
93
|
processResponse: ProcessResponseFunction<Result, ReqResult>
|
|
81
94
|
useContext?: () => Context
|
|
82
95
|
onSuccess?: (
|
|
83
96
|
queryClient: QueryClient,
|
|
84
97
|
data: NoInfer<Result>,
|
|
85
|
-
variables:
|
|
86
|
-
ClientMutationArgs<Url, RequestSchema, QuerySchema>
|
|
87
|
-
>,
|
|
98
|
+
variables: MutationArgs<Url, RequestSchema, QuerySchema>,
|
|
88
99
|
context: Context,
|
|
89
100
|
) => void | Promise<void>
|
|
90
101
|
onError?: (
|
|
91
102
|
queryClient: QueryClient,
|
|
92
103
|
error: Error,
|
|
93
|
-
variables:
|
|
94
|
-
ClientMutationArgs<Url, RequestSchema, QuerySchema>
|
|
95
|
-
>,
|
|
104
|
+
variables: MutationArgs<Url, RequestSchema, QuerySchema>,
|
|
96
105
|
context: Context,
|
|
97
106
|
) => void | Promise<void>
|
|
98
107
|
useKey?: UseKey
|
|
99
108
|
}
|
|
100
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Creates a client instance for making type-safe queries and mutations.
|
|
112
|
+
*
|
|
113
|
+
* @param options - Client configuration including the API builder and defaults
|
|
114
|
+
* @returns A client instance with query, infiniteQuery, and mutation methods
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* const api = createBuilder({ baseUrl: '/api' });
|
|
119
|
+
* const client = declareClient({ api });
|
|
120
|
+
*
|
|
121
|
+
* const getUser = client.query({
|
|
122
|
+
* method: 'GET',
|
|
123
|
+
* url: '/users/$id',
|
|
124
|
+
* responseSchema: UserSchema,
|
|
125
|
+
* });
|
|
126
|
+
*
|
|
127
|
+
* // In a component
|
|
128
|
+
* const { data } = useSuspenseQuery(getUser({ urlParams: { id: '123' } }));
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
101
131
|
export function declareClient<Options extends ClientOptions>({
|
|
102
132
|
api,
|
|
103
133
|
defaults = {},
|
|
104
134
|
}: Options): ClientInstance {
|
|
105
|
-
function query(config:
|
|
135
|
+
function query(config: QueryConfig) {
|
|
106
136
|
const endpoint = api.declareEndpoint({
|
|
107
137
|
// @ts-expect-error we accept only specific methods
|
|
108
138
|
method: config.method,
|
|
109
139
|
url: config.url,
|
|
110
140
|
querySchema: config.querySchema,
|
|
141
|
+
requestSchema: config.requestSchema,
|
|
111
142
|
responseSchema: config.responseSchema,
|
|
112
143
|
})
|
|
113
144
|
|
|
@@ -134,12 +165,13 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
134
165
|
})
|
|
135
166
|
}
|
|
136
167
|
|
|
137
|
-
function infiniteQuery(config:
|
|
168
|
+
function infiniteQuery(config: InfiniteQueryConfig) {
|
|
138
169
|
const endpoint = api.declareEndpoint({
|
|
139
170
|
// @ts-expect-error we accept only specific methods
|
|
140
171
|
method: config.method,
|
|
141
172
|
url: config.url,
|
|
142
173
|
querySchema: config.querySchema,
|
|
174
|
+
requestSchema: config.requestSchema,
|
|
143
175
|
responseSchema: config.responseSchema,
|
|
144
176
|
})
|
|
145
177
|
const infiniteQueryOptions = makeInfiniteQueryOptions(endpoint, {
|
|
@@ -185,7 +217,7 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
185
217
|
})
|
|
186
218
|
}
|
|
187
219
|
|
|
188
|
-
function mutation(config:
|
|
220
|
+
function mutation(config: MutationConfig) {
|
|
189
221
|
const endpoint = api.declareEndpoint({
|
|
190
222
|
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
191
223
|
method: config.method,
|
|
@@ -219,13 +251,13 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
219
251
|
onSuccess?: (
|
|
220
252
|
queryClient: QueryClient,
|
|
221
253
|
data: unknown,
|
|
222
|
-
variables:
|
|
254
|
+
variables: MutationArgs,
|
|
223
255
|
context: unknown,
|
|
224
256
|
) => void | Promise<void>
|
|
225
257
|
onError?: (
|
|
226
258
|
queryClient: QueryClient,
|
|
227
259
|
error: Error,
|
|
228
|
-
variables:
|
|
260
|
+
variables: MutationArgs,
|
|
229
261
|
context: unknown,
|
|
230
262
|
) => void | Promise<void>
|
|
231
263
|
},
|
|
@@ -240,6 +272,32 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
240
272
|
})
|
|
241
273
|
}
|
|
242
274
|
|
|
275
|
+
function multipartMutation(config: MutationConfig) {
|
|
276
|
+
const endpoint = api.declareMultipart({
|
|
277
|
+
// @ts-expect-error we accept only specific methods
|
|
278
|
+
method: config.method,
|
|
279
|
+
url: config.url,
|
|
280
|
+
querySchema: config.querySchema,
|
|
281
|
+
requestSchema: config.requestSchema,
|
|
282
|
+
responseSchema: config.responseSchema,
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
const useMutation = makeMutation(endpoint, {
|
|
286
|
+
processResponse: config.processResponse ?? ((data) => data),
|
|
287
|
+
useContext: config.useContext,
|
|
288
|
+
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
289
|
+
onSuccess: config.onSuccess,
|
|
290
|
+
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
291
|
+
onError: config.onError,
|
|
292
|
+
useKey: config.useKey,
|
|
293
|
+
...defaults,
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
// @ts-expect-error We attach the endpoint to the useMutation
|
|
297
|
+
useMutation.endpoint = endpoint
|
|
298
|
+
return useMutation
|
|
299
|
+
}
|
|
300
|
+
|
|
243
301
|
return {
|
|
244
302
|
// @ts-expect-error We simplified types here
|
|
245
303
|
query,
|
|
@@ -253,5 +311,7 @@ export function declareClient<Options extends ClientOptions>({
|
|
|
253
311
|
mutation,
|
|
254
312
|
// @ts-expect-error We simplified types here
|
|
255
313
|
mutationFromEndpoint,
|
|
314
|
+
// @ts-expect-error We simplified types here
|
|
315
|
+
multipartMutation,
|
|
256
316
|
}
|
|
257
317
|
}
|