@navios/react-query 0.3.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/README.md +13 -7
- 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/declare-client.d.mts +4 -4
- package/dist/src/declare-client.d.mts.map +1 -1
- package/dist/src/index.d.mts +13 -8
- package/dist/src/index.d.mts.map +1 -1
- package/dist/src/make-mutation.d.mts +1 -1
- package/dist/src/make-mutation.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/src/types/client-endpoint-helper.d.mts +1 -1
- package/dist/src/types/client-endpoint-helper.d.mts.map +1 -1
- package/dist/src/types/client-instance.d.mts +15 -15
- package/dist/src/types/client-instance.d.mts.map +1 -1
- package/dist/src/types/mutation-args.d.mts +3 -3
- package/dist/src/types/mutation-args.d.mts.map +1 -1
- package/dist/src/types/query-args.d.mts +3 -3
- package/dist/src/types/query-args.d.mts.map +1 -1
- package/dist/src/types/query-helpers.d.mts +2 -2
- package/dist/src/types/query-helpers.d.mts.map +1 -1
- package/dist/src/types.d.mts +4 -4
- package/dist/src/types.d.mts.map +1 -1
- package/dist/src/utils/query-key-creator.d.mts +3 -3
- package/dist/src/utils/query-key-creator.d.mts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/_tsup-dts-rollup.d.mts +512 -262
- package/lib/_tsup-dts-rollup.d.ts +512 -262
- 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 +8 -8
- package/project.json +9 -1
- package/src/__tests__/client-type-check.spec.mts +2 -2
- package/src/__tests__/declare-client.spec.mts +2 -2
- package/src/__tests__/make-mutation.spec.mts +2 -2
- package/src/__tests__/makeDataTag.spec.mts +2 -2
- package/src/__tests__/makeInfiniteQueryOptions.spec.mts +2 -3
- package/src/__tests__/makeQueryOptions.spec.mts +2 -2
- package/src/{declare-client.mts → client/declare-client.mts} +96 -36
- package/src/client/index.mts +2 -0
- package/src/{types/client-instance.mts → client/types.mts} +442 -160
- 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} +20 -9
- 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 -52
- package/src/{make-infinite-query-options.mts → query/make-infinite-options.mts} +29 -15
- 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
package/lib/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { bindUrlParams } from '@navios/builder';
|
|
2
|
-
import {
|
|
2
|
+
import { useQuery, useSuspenseQuery, useInfiniteQuery, useSuspenseInfiniteQuery, useIsMutating, infiniteQueryOptions, useQueryClient, useMutation, queryOptions } from '@tanstack/react-query';
|
|
3
3
|
|
|
4
|
-
// src/
|
|
5
|
-
function
|
|
4
|
+
// src/query/key-creator.mts
|
|
5
|
+
function createQueryKey(config, options, _isInfinite) {
|
|
6
6
|
const url = config.url;
|
|
7
7
|
const urlParts = url.split("/").filter(Boolean);
|
|
8
8
|
return {
|
|
@@ -40,23 +40,58 @@ function queryKeyCreator(config, options, isInfinite) {
|
|
|
40
40
|
}
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
var queryKeyCreator = createQueryKey;
|
|
44
|
+
function makeQueryOptions(endpoint, options, baseQuery = {}) {
|
|
45
|
+
const config = endpoint.config;
|
|
46
|
+
const queryKey = createQueryKey(config, options);
|
|
47
|
+
const processResponse = options.processResponse;
|
|
48
|
+
const result = (params) => {
|
|
49
|
+
return queryOptions({
|
|
50
|
+
queryKey: queryKey.dataTag(params),
|
|
51
|
+
queryFn: async ({ signal }) => {
|
|
52
|
+
let result2;
|
|
53
|
+
try {
|
|
54
|
+
result2 = await endpoint({
|
|
55
|
+
signal,
|
|
56
|
+
...params
|
|
57
|
+
});
|
|
58
|
+
} catch (err) {
|
|
59
|
+
if (options.onFail) {
|
|
60
|
+
options.onFail(err);
|
|
61
|
+
}
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
return processResponse(result2);
|
|
65
|
+
},
|
|
66
|
+
...baseQuery
|
|
67
|
+
});
|
|
51
68
|
};
|
|
69
|
+
result.queryKey = queryKey;
|
|
70
|
+
result.use = (params) => {
|
|
71
|
+
return useQuery(result(params));
|
|
72
|
+
};
|
|
73
|
+
result.useSuspense = (params) => {
|
|
74
|
+
return useSuspenseQuery(result(params));
|
|
75
|
+
};
|
|
76
|
+
result.invalidate = (queryClient, params) => {
|
|
77
|
+
return queryClient.invalidateQueries({
|
|
78
|
+
queryKey: result.queryKey.dataTag(params)
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
result.invalidateAll = (queryClient, params) => {
|
|
82
|
+
return queryClient.invalidateQueries({
|
|
83
|
+
queryKey: result.queryKey.filterKey(params),
|
|
84
|
+
exact: false
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
return result;
|
|
52
88
|
}
|
|
53
89
|
function makeInfiniteQueryOptions(endpoint, options, baseQuery = {}) {
|
|
54
90
|
const config = endpoint.config;
|
|
55
|
-
const queryKey =
|
|
91
|
+
const queryKey = createQueryKey(config, options);
|
|
56
92
|
const processResponse = options.processResponse;
|
|
57
93
|
const res = (params) => {
|
|
58
94
|
return infiniteQueryOptions({
|
|
59
|
-
// @ts-expect-error TS2322 We know the type
|
|
60
95
|
queryKey: queryKey.dataTag(params),
|
|
61
96
|
queryFn: async ({ signal, pageParam }) => {
|
|
62
97
|
let result;
|
|
@@ -79,6 +114,7 @@ function makeInfiniteQueryOptions(endpoint, options, baseQuery = {}) {
|
|
|
79
114
|
return processResponse(result);
|
|
80
115
|
},
|
|
81
116
|
getNextPageParam: options.getNextPageParam,
|
|
117
|
+
getPreviousPageParam: options.getPreviousPageParam,
|
|
82
118
|
initialPageParam: options.initialPageParam ?? config.querySchema.parse("params" in params ? params.params : {}),
|
|
83
119
|
...baseQuery
|
|
84
120
|
});
|
|
@@ -92,22 +128,31 @@ function makeInfiniteQueryOptions(endpoint, options, baseQuery = {}) {
|
|
|
92
128
|
};
|
|
93
129
|
res.invalidate = (queryClient, params) => {
|
|
94
130
|
return queryClient.invalidateQueries({
|
|
95
|
-
// @ts-expect-error We add additional function to the result
|
|
96
131
|
queryKey: res.queryKey.dataTag(params)
|
|
97
132
|
});
|
|
98
133
|
};
|
|
99
134
|
res.invalidateAll = (queryClient, params) => {
|
|
100
135
|
return queryClient.invalidateQueries({
|
|
101
|
-
// @ts-expect-error We add additional function to the result
|
|
102
136
|
queryKey: res.queryKey.filterKey(params),
|
|
103
137
|
exact: false
|
|
104
138
|
});
|
|
105
139
|
};
|
|
106
140
|
return res;
|
|
107
141
|
}
|
|
142
|
+
|
|
143
|
+
// src/mutation/key-creator.mts
|
|
144
|
+
function createMutationKey(config, options = {
|
|
145
|
+
processResponse: (data) => data
|
|
146
|
+
}) {
|
|
147
|
+
const queryKey = createQueryKey(config, options);
|
|
148
|
+
return (params) => {
|
|
149
|
+
return queryKey.filterKey(params);
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
var mutationKeyCreator = createMutationKey;
|
|
108
153
|
function makeMutation(endpoint, options) {
|
|
109
154
|
const config = endpoint.config;
|
|
110
|
-
const mutationKey =
|
|
155
|
+
const mutationKey = createMutationKey(config, options);
|
|
111
156
|
const result = (keyParams) => {
|
|
112
157
|
const queryClient = useQueryClient();
|
|
113
158
|
const {
|
|
@@ -115,8 +160,8 @@ function makeMutation(endpoint, options) {
|
|
|
115
160
|
useContext,
|
|
116
161
|
onError,
|
|
117
162
|
onSuccess,
|
|
118
|
-
keyPrefix,
|
|
119
|
-
keySuffix,
|
|
163
|
+
keyPrefix: _keyPrefix,
|
|
164
|
+
keySuffix: _keySuffix,
|
|
120
165
|
processResponse,
|
|
121
166
|
...rest
|
|
122
167
|
} = options;
|
|
@@ -164,55 +209,8 @@ function makeMutation(endpoint, options) {
|
|
|
164
209
|
result.mutationKey = mutationKey;
|
|
165
210
|
return result;
|
|
166
211
|
}
|
|
167
|
-
function makeQueryOptions(endpoint, options, baseQuery = {}) {
|
|
168
|
-
const config = endpoint.config;
|
|
169
|
-
const queryKey = queryKeyCreator(config, options);
|
|
170
|
-
const processResponse = options.processResponse;
|
|
171
|
-
const result = (params) => {
|
|
172
|
-
return queryOptions({
|
|
173
|
-
queryKey: queryKey.dataTag(params),
|
|
174
|
-
queryFn: async ({ signal }) => {
|
|
175
|
-
let result2;
|
|
176
|
-
try {
|
|
177
|
-
result2 = await endpoint({
|
|
178
|
-
signal,
|
|
179
|
-
...params
|
|
180
|
-
});
|
|
181
|
-
} catch (err) {
|
|
182
|
-
if (options.onFail) {
|
|
183
|
-
options.onFail(err);
|
|
184
|
-
}
|
|
185
|
-
throw err;
|
|
186
|
-
}
|
|
187
|
-
return processResponse(result2);
|
|
188
|
-
},
|
|
189
|
-
...baseQuery
|
|
190
|
-
});
|
|
191
|
-
};
|
|
192
|
-
result.queryKey = queryKey;
|
|
193
|
-
result.use = (params) => {
|
|
194
|
-
return useQuery(result(params));
|
|
195
|
-
};
|
|
196
|
-
result.useSuspense = (params) => {
|
|
197
|
-
return useSuspenseQuery(result(params));
|
|
198
|
-
};
|
|
199
|
-
result.invalidate = (queryClient, params) => {
|
|
200
|
-
return queryClient.invalidateQueries({
|
|
201
|
-
// @ts-expect-error We add additional function to the result
|
|
202
|
-
queryKey: result.queryKey.dataTag(params)
|
|
203
|
-
});
|
|
204
|
-
};
|
|
205
|
-
result.invalidateAll = (queryClient, params) => {
|
|
206
|
-
return queryClient.invalidateQueries({
|
|
207
|
-
// @ts-expect-error We add additional function to the result
|
|
208
|
-
queryKey: result.queryKey.filterKey(params),
|
|
209
|
-
exact: false
|
|
210
|
-
});
|
|
211
|
-
};
|
|
212
|
-
return result;
|
|
213
|
-
}
|
|
214
212
|
|
|
215
|
-
// src/declare-client.mts
|
|
213
|
+
// src/client/declare-client.mts
|
|
216
214
|
function declareClient({
|
|
217
215
|
api,
|
|
218
216
|
defaults = {}
|
|
@@ -223,6 +221,7 @@ function declareClient({
|
|
|
223
221
|
method: config.method,
|
|
224
222
|
url: config.url,
|
|
225
223
|
querySchema: config.querySchema,
|
|
224
|
+
requestSchema: config.requestSchema,
|
|
226
225
|
responseSchema: config.responseSchema
|
|
227
226
|
});
|
|
228
227
|
const queryOptions2 = makeQueryOptions(endpoint, {
|
|
@@ -244,6 +243,7 @@ function declareClient({
|
|
|
244
243
|
method: config.method,
|
|
245
244
|
url: config.url,
|
|
246
245
|
querySchema: config.querySchema,
|
|
246
|
+
requestSchema: config.requestSchema,
|
|
247
247
|
responseSchema: config.responseSchema
|
|
248
248
|
});
|
|
249
249
|
const infiniteQueryOptions2 = makeInfiniteQueryOptions(endpoint, {
|
|
@@ -297,6 +297,28 @@ function declareClient({
|
|
|
297
297
|
...defaults
|
|
298
298
|
});
|
|
299
299
|
}
|
|
300
|
+
function multipartMutation(config) {
|
|
301
|
+
const endpoint = api.declareMultipart({
|
|
302
|
+
// @ts-expect-error we accept only specific methods
|
|
303
|
+
method: config.method,
|
|
304
|
+
url: config.url,
|
|
305
|
+
querySchema: config.querySchema,
|
|
306
|
+
requestSchema: config.requestSchema,
|
|
307
|
+
responseSchema: config.responseSchema
|
|
308
|
+
});
|
|
309
|
+
const useMutation2 = makeMutation(endpoint, {
|
|
310
|
+
processResponse: config.processResponse ?? ((data) => data),
|
|
311
|
+
useContext: config.useContext,
|
|
312
|
+
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
313
|
+
onSuccess: config.onSuccess,
|
|
314
|
+
// @ts-expect-error We forgot about the DELETE method in original makeMutation
|
|
315
|
+
onError: config.onError,
|
|
316
|
+
useKey: config.useKey,
|
|
317
|
+
...defaults
|
|
318
|
+
});
|
|
319
|
+
useMutation2.endpoint = endpoint;
|
|
320
|
+
return useMutation2;
|
|
321
|
+
}
|
|
300
322
|
return {
|
|
301
323
|
// @ts-expect-error We simplified types here
|
|
302
324
|
query,
|
|
@@ -309,10 +331,12 @@ function declareClient({
|
|
|
309
331
|
// @ts-expect-error We simplified types here
|
|
310
332
|
mutation,
|
|
311
333
|
// @ts-expect-error We simplified types here
|
|
312
|
-
mutationFromEndpoint
|
|
334
|
+
mutationFromEndpoint,
|
|
335
|
+
// @ts-expect-error We simplified types here
|
|
336
|
+
multipartMutation
|
|
313
337
|
};
|
|
314
338
|
}
|
|
315
339
|
|
|
316
|
-
export { declareClient, makeInfiniteQueryOptions, makeMutation, makeQueryOptions, mutationKeyCreator, queryKeyCreator };
|
|
340
|
+
export { createMutationKey, createQueryKey, declareClient, makeInfiniteQueryOptions, makeMutation, makeQueryOptions, mutationKeyCreator, queryKeyCreator };
|
|
317
341
|
//# sourceMappingURL=index.mjs.map
|
|
318
342
|
//# sourceMappingURL=index.mjs.map
|
package/lib/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils/query-key-creator.mts","../src/utils/mutation-key.creator.mts","../src/make-infinite-query-options.mts","../src/make-mutation.mts","../src/make-query-options.mts","../src/declare-client.mts"],"names":["result","queryOptions","infiniteQueryOptions","useMutation"],"mappings":";;;;AAqDO,SAAS,eAAA,CAOd,MACA,EAAA,OAAA,EACA,UASA,EAAA;AACA,EAAA,MAAM,MAAM,MAAO,CAAA,GAAA;AACnB,EAAA,MAAM,WAAW,GAAI,CAAA,KAAA,CAAM,GAAG,CAAA,CAAE,OAAO,OAAO,CAAA;AAC9C,EAAO,OAAA;AAAA,IACL,QAAU,EAAA,QAAA;AAAA;AAAA,IAEV,OAAA,EAAS,CAAC,MAAW,KAAA;AACnB,MAAA,MAA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AAA;AAAA,MACtD,YAAY,MAAO,CAAA,UAAA;AAAA;AAAA,MAEnB,WAAW,MAAO,CAAA,SAAA;AAAA;AAAA,MAElB,SAAS,MAAO,CAAA,OAAA;AAAA,MAChB,QAAQ,MAAO,CAAA,MAAA;AAAA,MACf,GAAG;AAAA,KACJ,CAAA;AAGD,IAAAA,aAAY,QAAW,GAAA,QAAA;AACvB,IAAOA,OAAAA,YAAAA;AAAA;AAGT,EAAS,SAAA,oBAAA,CACP,UACA,OAgBA,EAAA;AACA,IAAA,OAAO,aAAa,QAAU,EAAA;AAAA,MAC5B,iBAAiB,OAAQ,CAAA,eAAA;AAAA,MACzB,YAAY,OAAQ,CAAA,UAAA;AAAA,MACpB,WAAW,OAAQ,CAAA,SAAA;AAAA;AAAA,MAEnB,SAAS,OAAQ,CAAA,OAAA;AAAA,MACjB,GAAG;AAAA,KACJ,CAAA;AAAA;AAGH,EAAO,OAAA;AAAA;AAAA,IAEL,KAAA;AAAA;AAAA,IAEA,iBAAA;AAAA;AAAA,IAEA,aAAA;AAAA;AAAA,IAEA,yBAAA;AAAA;AAAA,IAEA,QAAA;AAAA;AAAA,IAEA;AAAA,GACF;AACF","file":"index.mjs","sourcesContent":["import type {\n AnyEndpointConfig,\n UrlHasParams,\n UrlParams,\n} from '@navios/builder'\nimport type { DataTag, InfiniteData } from '@tanstack/react-query'\nimport type { AnyZodObject, z } from 'zod'\n\nimport { bindUrlParams } from '@navios/builder'\n\nimport type { BaseQueryParams } from '../types.mjs'\n\ntype Split<S extends string, D extends string> = string extends S\n ? string[]\n : S extends ''\n ? []\n : S extends `${infer T}${D}${infer U}`\n ? [T, ...Split<U, D>]\n : [S]\n\nexport type QueryKeyCreatorResult<\n QuerySchema = undefined,\n Url extends string = string,\n Result = unknown,\n IsInfinite extends boolean = false,\n HasParams extends UrlHasParams<Url> = UrlHasParams<Url>,\n> = {\n template: Split<Url, '/'>\n dataTag: (\n params: (HasParams extends true ? { urlParams: UrlParams<Url> } : {}) &\n (QuerySchema extends AnyZodObject\n ? { params: z.input<QuerySchema> }\n : {}),\n ) => DataTag<\n Split<Url, '/'>,\n IsInfinite extends true ? InfiniteData<Result> : Result,\n Error\n >\n filterKey: (\n params: HasParams extends true ? { urlParams: UrlParams<Url> } : {},\n ) => DataTag<\n Split<Url, '/'>,\n IsInfinite extends true ? InfiniteData<Result> : Result,\n Error\n >\n bindToUrl: (\n params: (HasParams extends true ? { urlParams: UrlParams<Url> } : {}) &\n (QuerySchema extends AnyZodObject\n ? { params: z.infer<QuerySchema> }\n : {}),\n ) => string\n}\n\nexport function queryKeyCreator<\n Config extends AnyEndpointConfig,\n Options extends BaseQueryParams<Config>,\n IsInfinite extends boolean,\n Url extends Config['url'] = Config['url'],\n HasParams extends UrlHasParams<Url> = UrlHasParams<Url>,\n>(\n config: Config,\n options: Options,\n isInfinite: IsInfinite,\n): QueryKeyCreatorResult<\n Config['querySchema'],\n Url,\n Options['processResponse'] extends (...args: any[]) => infer Result\n ? Result\n : never,\n IsInfinite,\n HasParams\n> {\n const url = config.url as Url\n const urlParts = url.split('/').filter(Boolean) as Split<Url, '/'>\n return {\n template: urlParts,\n // @ts-expect-error We have correct types in return type\n dataTag: (params) => {\n const queryParams =\n params && 'querySchema' in config && 'params' in params\n ? config.querySchema?.parse(params.params)\n : []\n return [\n ...(options.keyPrefix ?? []),\n ...urlParts.map((part) =>\n part.startsWith('$')\n ? // @ts-expect-error TS2339 We know that the urlParams are defined only if the url has params\n params.urlParams[part.slice(1)].toString()\n : part,\n ),\n ...(options.keySuffix ?? []),\n queryParams ?? [],\n ] as unknown as DataTag<\n Split<Url, '/'>,\n Options['processResponse'] extends (...args: any[]) => infer Result\n ? IsInfinite extends true\n ? InfiniteData<Result>\n : Result\n : never,\n Error\n >\n },\n // @ts-expect-error We have correct types in return type\n filterKey: (params) => {\n return [\n ...(options.keyPrefix ?? []),\n ...urlParts.map((part) =>\n part.startsWith('$')\n ? // @ts-expect-error TS2339 We know that the urlParams are defined only if the url has params\n params.urlParams[part.slice(1)].toString()\n : part,\n ),\n ...(options.keySuffix ?? []),\n ] as unknown as DataTag<\n Split<Url, '/'>,\n Options['processResponse'] extends (...args: any[]) => infer Result\n ? IsInfinite extends true\n ? InfiniteData<Result>\n : Result\n : never,\n Error\n >\n },\n\n bindToUrl: (params) => {\n return bindUrlParams<Url>(url, params ?? ({} as any))\n },\n }\n}\n","import type {\n AnyEndpointConfig,\n UrlHasParams,\n UrlParams,\n} from '@navios/builder'\nimport type { DataTag } from '@tanstack/react-query'\n\nimport type { BaseQueryParams } from '../types.mjs'\n\nimport { queryKeyCreator } from './query-key-creator.mjs'\n\n/**\n * Creates a mutation key for a given endpoint configuration and options.\n *\n * @param {config: Config } config - The endpoint object containing the configuration.\n * @param {Options} [options] - Optional query parameters with a default `processResponse` function that processes the response data.\n *\n * @returns {Object} An object containing the `mutationKey` function.\n *\n * The `mutationKey` function generates a mutation key based on the provided parameters:\n * - If the URL has parameters (`HasParams` is `true`), it expects an object with `urlParams`.\n * - The return type of the `mutationKey` function depends on the `processResponse` function in `options`.\n * If `processResponse` is defined, the return type is a `DataTag` containing the processed result and an error type.\n *\n * @example Example usage:\n * ```typescript\n * const createMutationKey = mutationKeyCreator(endpoint.config);\n * const mutationKey = createMutationKey({ urlParams: { id: 123 } });\n * ```\n *\n * @example Advanced usage:\n * ```ts\n * const createMutationKey = mutationKeyCreator(endpoint.config, {\n * processResponse: (data) => {\n * if (!data.success) {\n * throw new Error(data.message);\n * }\n * return data.data;\n * },\n * });\n * // We create a mutation that will be shared across the project for all passed userId\n * const mutationKey = createMutationKey({ urlParams: { projectId: 123, userId: 'wildcard' } });\n */\nexport function mutationKeyCreator<\n Config extends AnyEndpointConfig,\n Options extends BaseQueryParams<Config>,\n Url extends Config['url'] = Config['url'],\n HasParams extends UrlHasParams<Url> = UrlHasParams<Url>,\n>(\n config: Config,\n options: Options = {\n processResponse: (data) => data,\n } as Options,\n): (\n params: HasParams extends true ? { urlParams: UrlParams<Url> } : {},\n) => Options['processResponse'] extends (...args: any[]) => infer Result\n ? DataTag<[Config['url']], Result, Error>\n : never {\n const queryKey = queryKeyCreator(config, options, false)\n\n // @ts-expect-error We have correct types in return type\n return (params) => {\n return queryKey.filterKey(params)\n }\n}\n","import type {\n AbstractEndpoint,\n AnyEndpointConfig,\n UrlParams,\n} from '@navios/builder'\nimport type {\n InfiniteData,\n QueryClient,\n UseInfiniteQueryOptions,\n UseSuspenseInfiniteQueryOptions,\n} from '@tanstack/react-query'\nimport type { z } from 'zod'\n\nimport {\n infiniteQueryOptions,\n useInfiniteQuery,\n useSuspenseInfiniteQuery,\n} from '@tanstack/react-query'\n\nimport type { InfiniteQueryOptions } from './types.mjs'\nimport type { ClientQueryArgs } from './types/index.mjs'\n\nimport { queryKeyCreator } from './utils/query-key-creator.mjs'\n\nexport function makeInfiniteQueryOptions<\n Config extends AnyEndpointConfig,\n Options extends InfiniteQueryOptions<Config>,\n BaseQuery extends Omit<\n UseInfiniteQueryOptions<ReturnType<Options['processResponse']>, Error, any>,\n | 'queryKey'\n | 'queryFn'\n | 'getNextPageParam'\n | 'initialPageParam'\n | 'placeholderData'\n | 'throwOnError'\n >,\n>(\n endpoint: AbstractEndpoint<Config>,\n options: Options,\n baseQuery: BaseQuery = {} as BaseQuery,\n) {\n const config = endpoint.config\n const queryKey = queryKeyCreator(config, options, true)\n\n const processResponse = options.processResponse\n const res = (\n params: ClientQueryArgs,\n ): Options['processResponse'] extends (...args: any[]) => infer Result\n ? UseSuspenseInfiniteQueryOptions<\n Result,\n Error,\n BaseQuery['select'] extends (...args: any[]) => infer T\n ? T\n : InfiniteData<Result>\n >\n : never => {\n // @ts-expect-error TS2322 We know that the processResponse is defined\n return infiniteQueryOptions({\n // @ts-expect-error TS2322 We know the type\n queryKey: queryKey.dataTag(params),\n queryFn: async ({ signal, pageParam }) => {\n let result\n try {\n result = await endpoint({\n signal,\n // @ts-expect-error TS2345 We bind the url params only if the url has params\n urlParams: params.urlParams as z.infer<UrlParams<Config['url']>>,\n params: {\n ...('params' in params ? params.params : {}),\n ...(pageParam as z.infer<Config['querySchema']>),\n },\n })\n } catch (err) {\n if (options.onFail) {\n options.onFail(err)\n }\n throw err\n }\n\n return processResponse(result)\n },\n getNextPageParam: options.getNextPageParam,\n initialPageParam:\n options.initialPageParam ??\n config.querySchema.parse('params' in params ? params.params : {}),\n ...baseQuery,\n })\n }\n res.queryKey = queryKey\n\n res.use = (params: ClientQueryArgs) => {\n return useInfiniteQuery(res(params))\n }\n\n res.useSuspense = (params: ClientQueryArgs) => {\n return useSuspenseInfiniteQuery(res(params))\n }\n\n res.invalidate = (queryClient: QueryClient, params: ClientQueryArgs) => {\n return queryClient.invalidateQueries({\n // @ts-expect-error We add additional function to the result\n queryKey: res.queryKey.dataTag(params),\n })\n }\n\n res.invalidateAll = (queryClient: QueryClient, params: ClientQueryArgs) => {\n return queryClient.invalidateQueries({\n // @ts-expect-error We add additional function to the result\n queryKey: res.queryKey.filterKey(params),\n exact: false,\n })\n }\n\n return res\n}\n","import type {\n AbstractEndpoint,\n AnyEndpointConfig,\n UrlHasParams,\n UrlParams,\n} from '@navios/builder'\nimport type { UseMutationResult } from '@tanstack/react-query'\nimport type { z } from 'zod'\n\nimport {\n useIsMutating,\n useMutation,\n useQueryClient,\n} from '@tanstack/react-query'\n\nimport type { BaseMutationArgs, BaseMutationParams } from './types.mjs'\n\nimport { mutationKeyCreator } from './index.mjs'\n\nexport function makeMutation<\n Config extends AnyEndpointConfig,\n TData = unknown,\n TVariables extends BaseMutationArgs<Config> = BaseMutationArgs<Config>,\n TResponse = z.output<Config['responseSchema']>,\n TContext = unknown,\n UseKey extends boolean = false,\n>(\n endpoint: AbstractEndpoint<Config>,\n options: BaseMutationParams<\n Config,\n TData,\n TVariables,\n TResponse,\n TContext,\n UseKey\n >,\n) {\n const config = endpoint.config\n\n const mutationKey = mutationKeyCreator(config, options)\n const result = (\n keyParams: UseKey extends true\n ? UrlHasParams<Config['url']> extends true\n ? UrlParams<Config['url']>\n : never\n : never,\n ): UseMutationResult<TData, Error, BaseMutationArgs<Config>> => {\n const queryClient = useQueryClient()\n const {\n useKey,\n useContext,\n onError,\n onSuccess,\n keyPrefix,\n keySuffix,\n processResponse,\n ...rest\n } = options\n\n const context = useContext?.() as TContext\n\n // @ts-expect-error The types match\n return useMutation(\n {\n ...rest,\n mutationKey: useKey\n ? mutationKey({\n urlParams: keyParams,\n })\n : undefined,\n scope: useKey\n ? {\n id: JSON.stringify(\n mutationKey({\n urlParams: keyParams,\n }),\n ),\n }\n : undefined,\n async mutationFn(params: TVariables) {\n const response = await endpoint(params)\n\n return processResponse(response) as TData\n },\n onSuccess: onSuccess\n ? (data: TData, variables: TVariables) => {\n return onSuccess?.(queryClient, data, variables, context)\n }\n : undefined,\n onError: onError\n ? (err: Error, variables: TVariables) => {\n return onError?.(queryClient, err, variables, context)\n }\n : undefined,\n },\n queryClient,\n )\n }\n result.useIsMutating = (\n keyParams: UseKey extends true\n ? UrlHasParams<Config['url']> extends true\n ? UrlParams<Config['url']>\n : never\n : never,\n ): boolean => {\n if (!options.useKey) {\n throw new Error(\n 'useIsMutating can only be used when useKey is set to true',\n )\n }\n const isMutating = useIsMutating({\n mutationKey: mutationKey({\n urlParams: keyParams,\n }),\n })\n return isMutating > 0\n }\n result.mutationKey = mutationKey\n\n return result\n}\n","import type { AbstractEndpoint, AnyEndpointConfig } from '@navios/builder'\nimport type {\n DataTag,\n QueryClient,\n UseQueryOptions,\n UseSuspenseQueryOptions,\n} from '@tanstack/react-query'\n\nimport { queryOptions, useQuery, useSuspenseQuery } from '@tanstack/react-query'\n\nimport type { BaseQueryArgs, BaseQueryParams } from './types.mjs'\nimport type { ClientQueryArgs } from './types/index.mjs'\n\nimport { queryKeyCreator } from './utils/query-key-creator.mjs'\n\ntype Split<S extends string, D extends string> = string extends S\n ? string[]\n : S extends ''\n ? []\n : S extends `${infer T}${D}${infer U}`\n ? [T, ...Split<U, D>]\n : [S]\n\nexport function makeQueryOptions<\n Config extends AnyEndpointConfig,\n Options extends BaseQueryParams<Config>,\n BaseQuery extends Omit<\n UseQueryOptions<ReturnType<Options['processResponse']>, Error, any>,\n | 'queryKey'\n | 'queryFn'\n | 'getNextPageParam'\n | 'initialPageParam'\n | 'enabled'\n | 'throwOnError'\n | 'placeholderData'\n >,\n>(\n endpoint: AbstractEndpoint<Config>,\n options: Options,\n baseQuery: BaseQuery = {} as BaseQuery,\n) {\n const config = endpoint.config\n // Let's hack the url to be a string for now\n const queryKey = queryKeyCreator(config, options, false)\n const processResponse = options.processResponse\n\n const result = (\n params: BaseQueryArgs<Config>,\n ): Options['processResponse'] extends (...args: any[]) => infer Result\n ? UseSuspenseQueryOptions<\n Result,\n Error,\n BaseQuery['select'] extends (...args: any[]) => infer T ? T : Result,\n DataTag<Split<Config['url'], '/'>, Result, Error>\n >\n : never => {\n // @ts-expect-error TS2322 We know that the processResponse is defined\n return queryOptions({\n queryKey: queryKey.dataTag(params),\n queryFn: async ({ signal }) => {\n let result\n try {\n result = await endpoint({\n signal,\n ...params,\n })\n } catch (err) {\n if (options.onFail) {\n options.onFail(err)\n }\n throw err\n }\n\n return processResponse(result)\n },\n ...baseQuery,\n })\n }\n result.queryKey = queryKey\n result.use = (params: ClientQueryArgs) => {\n // @ts-expect-error We add additional function to the result\n return useQuery(result(params))\n }\n\n result.useSuspense = (params: ClientQueryArgs) => {\n // @ts-expect-error We add additional function to the result\n return useSuspenseQuery(result(params))\n }\n\n result.invalidate = (queryClient: QueryClient, params: ClientQueryArgs) => {\n return queryClient.invalidateQueries({\n // @ts-expect-error We add additional function to the result\n queryKey: result.queryKey.dataTag(params),\n })\n }\n\n result.invalidateAll = (\n queryClient: QueryClient,\n params: ClientQueryArgs,\n ) => {\n return queryClient.invalidateQueries({\n // @ts-expect-error We add additional function to the result\n queryKey: result.queryKey.filterKey(params),\n exact: false,\n })\n }\n\n return result\n}\n","import type {\n AbstractEndpoint,\n AnyEndpointConfig,\n HttpMethod,\n Util_FlatObject,\n} from '@navios/builder'\nimport type { InfiniteData, QueryClient } from '@tanstack/react-query'\nimport type { AnyZodObject, z, ZodType } from 'zod'\n\nimport type { ClientOptions, ProcessResponseFunction } from './types.mjs'\nimport type { ClientInstance, ClientMutationArgs } from './types/index.mjs'\n\nimport { makeInfiniteQueryOptions } from './make-infinite-query-options.mjs'\nimport { makeMutation } from './make-mutation.mjs'\nimport { makeQueryOptions } from './make-query-options.mjs'\n\nexport interface ClientEndpointDefinition<\n Method = HttpMethod,\n Url = string,\n QuerySchema = unknown,\n Response = ZodType,\n> {\n method: Method\n url: Url\n querySchema?: QuerySchema\n responseSchema: Response\n}\n\nexport interface ClientQueryConfig<\n Method = HttpMethod,\n Url = string,\n QuerySchema = AnyZodObject,\n Response extends ZodType = ZodType,\n Result = z.output<Response>,\n> extends ClientEndpointDefinition<Method, Url, QuerySchema, Response> {\n processResponse?: (data: z.output<Response>) => Result\n}\n\nexport type ClientInfiniteQueryConfig<\n Method = HttpMethod,\n Url = string,\n QuerySchema extends AnyZodObject = AnyZodObject,\n Response extends ZodType = ZodType,\n PageResult = z.output<Response>,\n Result = InfiniteData<PageResult>,\n> = Required<ClientEndpointDefinition<Method, Url, QuerySchema, Response>> & {\n processResponse?: (data: z.output<Response>) => PageResult\n select?: (data: InfiniteData<PageResult>) => Result\n getNextPageParam: (\n lastPage: PageResult,\n allPages: PageResult[],\n lastPageParam: z.infer<QuerySchema> | undefined,\n allPageParams: z.infer<QuerySchema>[] | undefined,\n ) => z.input<QuerySchema> | undefined\n getPreviousPageParam?: (\n firstPage: PageResult,\n allPages: PageResult[],\n lastPageParam: z.infer<QuerySchema> | undefined,\n allPageParams: z.infer<QuerySchema>[] | undefined,\n ) => z.input<QuerySchema>\n initialPageParam?: z.input<QuerySchema>\n}\n\nexport interface ClientMutationDataConfig<\n Method extends 'POST' | 'PUT' | 'PATCH' | 'DELETE' =\n | 'POST'\n | 'PUT'\n | 'PATCH'\n | 'DELETE',\n Url extends string = string,\n RequestSchema = Method extends 'DELETE' ? never : AnyZodObject,\n QuerySchema = unknown,\n Response extends ZodType = ZodType,\n ReqResult = z.output<Response>,\n Result = unknown,\n Context = unknown,\n UseKey extends boolean = false,\n> extends ClientEndpointDefinition<Method, Url, QuerySchema, Response> {\n requestSchema?: RequestSchema\n processResponse: ProcessResponseFunction<Result, ReqResult>\n useContext?: () => Context\n onSuccess?: (\n queryClient: QueryClient,\n data: NoInfer<Result>,\n variables: Util_FlatObject<\n ClientMutationArgs<Url, RequestSchema, QuerySchema>\n >,\n context: Context,\n ) => void | Promise<void>\n onError?: (\n queryClient: QueryClient,\n error: Error,\n variables: Util_FlatObject<\n ClientMutationArgs<Url, RequestSchema, QuerySchema>\n >,\n context: Context,\n ) => void | Promise<void>\n useKey?: UseKey\n}\n\nexport function declareClient<Options extends ClientOptions>({\n api,\n defaults = {},\n}: Options): ClientInstance {\n function query(config: ClientQueryConfig) {\n const endpoint = api.declareEndpoint({\n // @ts-expect-error we accept only specific methods\n method: config.method,\n url: config.url,\n querySchema: config.querySchema,\n responseSchema: config.responseSchema,\n })\n\n const queryOptions = makeQueryOptions(endpoint, {\n ...defaults,\n processResponse: config.processResponse ?? ((data) => data),\n })\n // @ts-expect-error We attach the endpoint to the queryOptions\n queryOptions.endpoint = endpoint\n return queryOptions\n }\n\n function queryFromEndpoint(\n endpoint: AbstractEndpoint<AnyEndpointConfig>,\n options?: {\n processResponse?: (\n data: z.output<AnyEndpointConfig['responseSchema']>,\n ) => unknown\n },\n ) {\n return makeQueryOptions(endpoint, {\n ...defaults,\n processResponse: options?.processResponse ?? ((data) => data),\n })\n }\n\n function infiniteQuery(config: ClientInfiniteQueryConfig) {\n const endpoint = api.declareEndpoint({\n // @ts-expect-error we accept only specific methods\n method: config.method,\n url: config.url,\n querySchema: config.querySchema,\n responseSchema: config.responseSchema,\n })\n const infiniteQueryOptions = makeInfiniteQueryOptions(endpoint, {\n ...defaults,\n processResponse: config.processResponse ?? ((data) => data),\n getNextPageParam: config.getNextPageParam,\n getPreviousPageParam: config.getPreviousPageParam,\n initialPageParam: config.initialPageParam,\n })\n\n // @ts-expect-error We attach the endpoint to the infiniteQueryOptions\n infiniteQueryOptions.endpoint = endpoint\n return infiniteQueryOptions\n }\n\n function infiniteQueryFromEndpoint(\n endpoint: AbstractEndpoint<AnyEndpointConfig>,\n options: {\n processResponse?: (\n data: z.output<AnyEndpointConfig['responseSchema']>,\n ) => unknown\n getNextPageParam: (\n lastPage: z.infer<AnyEndpointConfig['responseSchema']>,\n allPages: z.infer<AnyEndpointConfig['responseSchema']>[],\n lastPageParam: z.infer<AnyEndpointConfig['querySchema']> | undefined,\n allPageParams: z.infer<AnyEndpointConfig['querySchema']>[] | undefined,\n ) => z.input<AnyEndpointConfig['querySchema']> | undefined\n getPreviousPageParam?: (\n firstPage: z.infer<AnyEndpointConfig['responseSchema']>,\n allPages: z.infer<AnyEndpointConfig['responseSchema']>[],\n lastPageParam: z.infer<AnyEndpointConfig['querySchema']> | undefined,\n allPageParams: z.infer<AnyEndpointConfig['querySchema']>[] | undefined,\n ) => z.input<AnyEndpointConfig['querySchema']>\n initialPageParam?: z.input<AnyEndpointConfig['querySchema']>\n },\n ) {\n return makeInfiniteQueryOptions(endpoint, {\n ...defaults,\n processResponse: options?.processResponse ?? ((data) => data),\n getNextPageParam: options.getNextPageParam,\n getPreviousPageParam: options?.getPreviousPageParam,\n initialPageParam: options?.initialPageParam,\n })\n }\n\n function mutation(config: ClientMutationDataConfig) {\n const endpoint = api.declareEndpoint({\n // @ts-expect-error We forgot about the DELETE method in original makeMutation\n method: config.method,\n url: config.url,\n querySchema: config.querySchema,\n requestSchema: config.requestSchema,\n responseSchema: config.responseSchema,\n })\n\n const useMutation = makeMutation(endpoint, {\n processResponse: config.processResponse ?? ((data) => data),\n useContext: config.useContext,\n // @ts-expect-error We forgot about the DELETE method in original makeMutation\n onSuccess: config.onSuccess,\n // @ts-expect-error We forgot about the DELETE method in original makeMutation\n onError: config.onError,\n useKey: config.useKey,\n ...defaults,\n })\n\n // @ts-expect-error We attach the endpoint to the useMutation\n useMutation.endpoint = endpoint\n return useMutation\n }\n\n function mutationFromEndpoint(\n endpoint: AbstractEndpoint<AnyEndpointConfig>,\n options: {\n processResponse: ProcessResponseFunction\n useContext?: () => unknown\n onSuccess?: (\n queryClient: QueryClient,\n data: unknown,\n variables: Util_FlatObject<ClientMutationArgs>,\n context: unknown,\n ) => void | Promise<void>\n onError?: (\n queryClient: QueryClient,\n error: Error,\n variables: Util_FlatObject<ClientMutationArgs>,\n context: unknown,\n ) => void | Promise<void>\n },\n ) {\n return makeMutation(endpoint, {\n processResponse: options.processResponse,\n useContext: options.useContext,\n onSuccess: options.onSuccess,\n // @ts-expect-error simplify types here\n onError: options.onError,\n ...defaults,\n })\n }\n\n return {\n // @ts-expect-error We simplified types here\n query,\n // @ts-expect-error We simplified types here\n queryFromEndpoint,\n // @ts-expect-error We simplified types here\n infiniteQuery,\n // @ts-expect-error We simplified types here\n infiniteQueryFromEndpoint,\n // @ts-expect-error We simplified types here\n mutation,\n // @ts-expect-error We simplified types here\n mutationFromEndpoint,\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/query/key-creator.mts","../src/query/make-options.mts","../src/query/make-infinite-options.mts","../src/mutation/key-creator.mts","../src/mutation/make-hook.mts","../src/client/declare-client.mts"],"names":["result","queryOptions","infiniteQueryOptions","useMutation"],"mappings":";;;;AAmBO,SAAS,cAAA,CAOd,MAAA,EACA,OAAA,EACA,WAAA,EASA;AACA,EAAA,MAAM,MAAM,MAAA,CAAO,GAAA;AACnB,EAAA,MAAM,WAAW,GAAA,CAAI,KAAA,CAAM,GAAG,CAAA,CAAE,OAAO,OAAO,CAAA;AAC9C,EAAA,OAAO;AAAA,IACL,QAAA,EAAU,QAAA;AAAA;AAAA,IAEV,OAAA,EAAS,CAAC,MAAA,KAAW;AACnB,MAAA,MAAM,WAAA,GACJ,MAAA,IAAU,aAAA,IAAiB,MAAA,IAAU,QAAA,IAAY,MAAA,GAC7C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O,UAAA;AAAA;AAAA,MAEnB,WAAW,MAAA,CAAO,SAAA;AAAA;AAAA,MAElB,SAAS,MAAA,CAAO,OAAA;AAAA,MAChB,QAAQ,MAAA,CAAO,MAAA;AAAA,MACf,GAAG;AAAA,KACJ,CAAA;AAGD,IAAAA,aAAY,QAAA,GAAW,QAAA;AACvB,IAAA,OAAOA,YAAAA;AAAA,EACT;AAEA,EAAA,OAAO;AAAA;AAAA,IAEL,KAAA;AAAA;AAAA,IAEA,iBAAA;AAAA;AAAA,IAEA,aAAA;AAAA;AAAA,IAEA,yBAAA;AAAA;AAAA,IAEA,QAAA;AAAA;AAAA,IAEA,oBAAA;AAAA;AAAA,IAEA;AAAA,GACF;AACF","file":"index.mjs","sourcesContent":["import type { AnyEndpointConfig, UrlHasParams } from '@navios/builder'\nimport type { DataTag, InfiniteData } from '@tanstack/react-query'\n\nimport { bindUrlParams } from '@navios/builder'\n\nimport type { Split } from '../common/types.mjs'\nimport type { QueryKeyCreatorResult, QueryParams } from './types.mjs'\n\n/**\n * Creates a query key generator for a given endpoint configuration.\n *\n * The returned object provides methods to generate query keys that can be used\n * with TanStack Query for caching, invalidation, and data tagging.\n *\n * @param config - The endpoint configuration\n * @param options - Query parameters including processResponse and key prefix/suffix\n * @param isInfinite - Whether this is for an infinite query\n * @returns An object with methods to generate query keys\n */\nexport function createQueryKey<\n Config extends AnyEndpointConfig,\n Options extends QueryParams<Config>,\n IsInfinite extends boolean,\n Url extends Config['url'] = Config['url'],\n HasParams extends UrlHasParams<Url> = UrlHasParams<Url>,\n>(\n config: Config,\n options: Options,\n _isInfinite: IsInfinite,\n): QueryKeyCreatorResult<\n Config['querySchema'],\n Url,\n Options['processResponse'] extends (...args: any[]) => infer Result\n ? Result\n : never,\n IsInfinite,\n HasParams\n> {\n const url = config.url as Url\n const urlParts = url.split('/').filter(Boolean) as Split<Url, '/'>\n return {\n template: urlParts,\n // @ts-expect-error We have correct types in return type\n dataTag: (params) => {\n const queryParams =\n params && 'querySchema' in config && 'params' in params\n ? config.querySchema?.parse(params.params)\n : []\n return [\n ...(options.keyPrefix ?? []),\n ...urlParts.map((part) =>\n part.startsWith('$')\n ? // @ts-expect-error TS2339 We know that the urlParams are defined only if the url has params\n params.urlParams[part.slice(1)].toString()\n : part,\n ),\n ...(options.keySuffix ?? []),\n queryParams ?? [],\n ] as unknown as DataTag<\n Split<Url, '/'>,\n Options['processResponse'] extends (...args: any[]) => infer Result\n ? IsInfinite extends true\n ? InfiniteData<Result>\n : Result\n : never,\n Error\n >\n },\n // @ts-expect-error We have correct types in return type\n filterKey: (params) => {\n return [\n ...(options.keyPrefix ?? []),\n ...urlParts.map((part) =>\n part.startsWith('$')\n ? // @ts-expect-error TS2339 We know that the urlParams are defined only if the url has params\n params.urlParams[part.slice(1)].toString()\n : part,\n ),\n ...(options.keySuffix ?? []),\n ] as unknown as DataTag<\n Split<Url, '/'>,\n Options['processResponse'] extends (...args: any[]) => infer Result\n ? IsInfinite extends true\n ? InfiniteData<Result>\n : Result\n : never,\n Error\n >\n },\n\n bindToUrl: (params) => {\n return bindUrlParams<Url>(url, params ?? ({} as never))\n },\n }\n}\n\n// Legacy export for backwards compatibility\n/** @deprecated Use createQueryKey instead */\nexport const queryKeyCreator = createQueryKey\n","import type { AbstractEndpoint, AnyEndpointConfig } from '@navios/builder'\nimport type {\n DataTag,\n QueryClient,\n UseQueryOptions,\n UseSuspenseQueryOptions,\n} from '@tanstack/react-query'\n\nimport { queryOptions, useQuery, useSuspenseQuery } from '@tanstack/react-query'\n\nimport type { Split } from '../common/types.mjs'\nimport type { QueryArgs, QueryParams } from './types.mjs'\n\nimport { createQueryKey } from './key-creator.mjs'\n\n/**\n * Creates query options for a given endpoint.\n *\n * Returns a function that generates TanStack Query options when called with params.\n * The returned function also has helper methods attached (use, useSuspense, invalidate, etc.)\n *\n * @param endpoint - The navios endpoint to create query options for\n * @param options - Query configuration including processResponse\n * @param baseQuery - Optional base query options to merge\n * @returns A function that generates query options with attached helpers\n */\nexport function makeQueryOptions<\n Config extends AnyEndpointConfig,\n Options extends QueryParams<Config>,\n BaseQuery extends Omit<\n UseQueryOptions<ReturnType<Options['processResponse']>, Error, any>,\n | 'queryKey'\n | 'queryFn'\n | 'getNextPageParam'\n | 'initialPageParam'\n | 'enabled'\n | 'throwOnError'\n | 'placeholderData'\n >,\n>(\n endpoint: AbstractEndpoint<Config>,\n options: Options,\n baseQuery: BaseQuery = {} as BaseQuery,\n) {\n const config = endpoint.config\n const queryKey = createQueryKey(config, options, false)\n const processResponse = options.processResponse\n\n const result = (\n params: QueryArgs<Config['url'], Config['querySchema']>,\n ): Options['processResponse'] extends (...args: any[]) => infer Result\n ? UseSuspenseQueryOptions<\n Result,\n Error,\n BaseQuery['select'] extends (...args: any[]) => infer T ? T : Result,\n DataTag<Split<Config['url'], '/'>, Result, Error>\n >\n : never => {\n // @ts-expect-error TS2322 We know that the processResponse is defined\n return queryOptions({\n queryKey: queryKey.dataTag(params),\n queryFn: async ({ signal }): Promise<ReturnType<Options['processResponse']>> => {\n let result\n try {\n result = await endpoint({\n signal,\n ...params,\n })\n } catch (err) {\n if (options.onFail) {\n options.onFail(err)\n }\n throw err\n }\n\n return processResponse(result) as ReturnType<Options['processResponse']>\n },\n ...baseQuery,\n })\n }\n result.queryKey = queryKey\n result.use = (params: QueryArgs<Config['url'], Config['querySchema']>) => {\n return useQuery(result(params))\n }\n\n result.useSuspense = (params: QueryArgs<Config['url'], Config['querySchema']>) => {\n return useSuspenseQuery(result(params))\n }\n\n result.invalidate = (\n queryClient: QueryClient,\n params: QueryArgs<Config['url'], Config['querySchema']>,\n ) => {\n return queryClient.invalidateQueries({\n queryKey: result.queryKey.dataTag(params),\n })\n }\n\n result.invalidateAll = (\n queryClient: QueryClient,\n params: QueryArgs<Config['url'], Config['querySchema']>,\n ) => {\n return queryClient.invalidateQueries({\n queryKey: result.queryKey.filterKey(params),\n exact: false,\n })\n }\n\n return result\n}\n","import type {\n AbstractEndpoint,\n AnyEndpointConfig,\n UrlParams,\n} from '@navios/builder'\nimport type {\n InfiniteData,\n QueryClient,\n UseInfiniteQueryOptions,\n UseSuspenseInfiniteQueryOptions,\n} from '@tanstack/react-query'\nimport type { z } from 'zod/v4'\n\nimport {\n infiniteQueryOptions,\n useInfiniteQuery,\n useSuspenseInfiniteQuery,\n} from '@tanstack/react-query'\n\nimport type { InfiniteQueryOptions, QueryArgs } from './types.mjs'\n\nimport { createQueryKey } from './key-creator.mjs'\n\n/**\n * Creates infinite query options for a given endpoint.\n *\n * Returns a function that generates TanStack Query infinite options when called with params.\n * The returned function also has helper methods attached (use, useSuspense, invalidate, etc.)\n *\n * @param endpoint - The navios endpoint to create infinite query options for\n * @param options - Infinite query configuration including processResponse and pagination params\n * @param baseQuery - Optional base query options to merge\n * @returns A function that generates infinite query options with attached helpers\n */\nexport function makeInfiniteQueryOptions<\n Config extends AnyEndpointConfig,\n Options extends InfiniteQueryOptions<Config>,\n BaseQuery extends Omit<\n UseInfiniteQueryOptions<ReturnType<Options['processResponse']>, Error, any>,\n | 'queryKey'\n | 'queryFn'\n | 'getNextPageParam'\n | 'initialPageParam'\n | 'placeholderData'\n | 'throwOnError'\n >,\n>(\n endpoint: AbstractEndpoint<Config>,\n options: Options,\n baseQuery: BaseQuery = {} as BaseQuery,\n) {\n const config = endpoint.config\n const queryKey = createQueryKey(config, options, true)\n\n const processResponse = options.processResponse\n const res = (\n params: QueryArgs<Config['url'], Config['querySchema']>,\n ): Options['processResponse'] extends (...args: any[]) => infer Result\n ? UseSuspenseInfiniteQueryOptions<\n Result,\n Error,\n BaseQuery['select'] extends (...args: any[]) => infer T\n ? T\n : InfiniteData<Result>\n >\n : never => {\n // @ts-expect-error TS2322 We know that the processResponse is defined\n return infiniteQueryOptions({\n queryKey: queryKey.dataTag(params),\n queryFn: async ({ signal, pageParam }): Promise<ReturnType<Options['processResponse']>> => {\n let result\n try {\n result = await endpoint({\n signal,\n // @ts-expect-error TS2345 We bind the url params only if the url has params\n urlParams: params.urlParams as z.infer<UrlParams<Config['url']>>,\n params: {\n ...('params' in params ? params.params : {}),\n ...(pageParam as z.infer<Config['querySchema']>),\n },\n })\n } catch (err) {\n if (options.onFail) {\n options.onFail(err)\n }\n throw err\n }\n\n return processResponse(result) as ReturnType<Options['processResponse']>\n },\n getNextPageParam: options.getNextPageParam,\n getPreviousPageParam: options.getPreviousPageParam,\n initialPageParam:\n options.initialPageParam ??\n config.querySchema.parse('params' in params ? params.params : {}),\n ...baseQuery,\n })\n }\n res.queryKey = queryKey\n\n res.use = (params: QueryArgs<Config['url'], Config['querySchema']>) => {\n return useInfiniteQuery(res(params))\n }\n\n res.useSuspense = (params: QueryArgs<Config['url'], Config['querySchema']>) => {\n return useSuspenseInfiniteQuery(res(params))\n }\n\n res.invalidate = (\n queryClient: QueryClient,\n params: QueryArgs<Config['url'], Config['querySchema']>,\n ) => {\n return queryClient.invalidateQueries({\n queryKey: res.queryKey.dataTag(params),\n })\n }\n\n res.invalidateAll = (\n queryClient: QueryClient,\n params: QueryArgs<Config['url'], Config['querySchema']>,\n ) => {\n return queryClient.invalidateQueries({\n queryKey: res.queryKey.filterKey(params),\n exact: false,\n })\n }\n\n return res\n}\n","import type {\n AnyEndpointConfig,\n UrlHasParams,\n UrlParams,\n} from '@navios/builder'\nimport type { DataTag } from '@tanstack/react-query'\n\nimport type { QueryParams } from '../query/types.mjs'\n\nimport { createQueryKey } from '../query/key-creator.mjs'\n\n/**\n * Creates a mutation key generator for a given endpoint configuration.\n *\n * @param config - The endpoint configuration\n * @param options - Optional query parameters with a default `processResponse` function\n * @returns A function that generates mutation keys\n *\n * @example Basic usage:\n * ```typescript\n * const createMutationKey = createMutationKey(endpoint.config);\n * const mutationKey = createMutationKey({ urlParams: { id: 123 } });\n * ```\n *\n * @example Advanced usage with processResponse:\n * ```ts\n * const createMutationKey = createMutationKey(endpoint.config, {\n * processResponse: (data) => {\n * if (!data.success) {\n * throw new Error(data.message);\n * }\n * return data.data;\n * },\n * });\n * // We create a mutation that will be shared across the project for all passed userId\n * const mutationKey = createMutationKey({ urlParams: { projectId: 123, userId: 'wildcard' } });\n * ```\n */\nexport function createMutationKey<\n Config extends AnyEndpointConfig,\n Options extends QueryParams<Config>,\n Url extends Config['url'] = Config['url'],\n HasParams extends UrlHasParams<Url> = UrlHasParams<Url>,\n>(\n config: Config,\n options: Options = {\n processResponse: (data) => data,\n } as Options,\n): (\n params: HasParams extends true ? { urlParams: UrlParams<Url> } : {},\n) => Options['processResponse'] extends (...args: unknown[]) => infer Result\n ? DataTag<[Config['url']], Result, Error>\n : never {\n const queryKey = createQueryKey(config, options, false)\n\n // @ts-expect-error We have correct types in return type\n return (params) => {\n return queryKey.filterKey(params)\n }\n}\n\n// Legacy export for backwards compatibility\n/** @deprecated Use createMutationKey instead */\nexport const mutationKeyCreator = createMutationKey\n","import type {\n AbstractEndpoint,\n AnyEndpointConfig,\n NaviosZodRequest,\n UrlHasParams,\n UrlParams,\n} from '@navios/builder'\nimport type { UseMutationResult } from '@tanstack/react-query'\nimport type { z } from 'zod/v4'\n\nimport {\n useIsMutating,\n useMutation,\n useQueryClient,\n} from '@tanstack/react-query'\n\nimport type { MutationParams } from './types.mjs'\n\nimport { createMutationKey } from './key-creator.mjs'\n\n/**\n * Creates a mutation hook for a given endpoint.\n *\n * Returns a function that when called returns a TanStack Query mutation result.\n * The returned function also has helper methods attached (mutationKey, useIsMutating).\n *\n * @param endpoint - The navios endpoint to create a mutation hook for\n * @param options - Mutation configuration including processResponse and callbacks\n * @returns A hook function that returns mutation result with attached helpers\n */\nexport function makeMutation<\n Config extends AnyEndpointConfig,\n TData = unknown,\n TVariables extends NaviosZodRequest<Config> = NaviosZodRequest<Config>,\n TResponse = z.output<Config['responseSchema']>,\n TContext = unknown,\n UseKey extends boolean = false,\n>(\n endpoint: AbstractEndpoint<Config>,\n options: MutationParams<\n Config,\n TData,\n TVariables,\n TResponse,\n TContext,\n UseKey\n >,\n) {\n const config = endpoint.config\n\n const mutationKey = createMutationKey(config, options)\n const result = (\n keyParams: UseKey extends true\n ? UrlHasParams<Config['url']> extends true\n ? UrlParams<Config['url']>\n : never\n : never,\n ): UseMutationResult<TData, Error, NaviosZodRequest<Config>> => {\n const queryClient = useQueryClient()\n const {\n useKey,\n useContext,\n onError,\n onSuccess,\n keyPrefix: _keyPrefix,\n keySuffix: _keySuffix,\n processResponse,\n ...rest\n } = options\n\n const context = useContext?.() as TContext\n\n // @ts-expect-error The types match\n return useMutation(\n {\n ...rest,\n mutationKey: useKey\n ? mutationKey({\n urlParams: keyParams,\n })\n : undefined,\n scope: useKey\n ? {\n id: JSON.stringify(\n mutationKey({\n urlParams: keyParams,\n }),\n ),\n }\n : undefined,\n async mutationFn(params: TVariables) {\n const response = await endpoint(params)\n\n return processResponse(response) as TData\n },\n onSuccess: onSuccess\n ? (data: TData, variables: TVariables) => {\n return onSuccess?.(queryClient, data, variables, context)\n }\n : undefined,\n onError: onError\n ? (err: Error, variables: TVariables) => {\n return onError?.(queryClient, err, variables, context)\n }\n : undefined,\n },\n queryClient,\n )\n }\n result.useIsMutating = (\n keyParams: UseKey extends true\n ? UrlHasParams<Config['url']> extends true\n ? UrlParams<Config['url']>\n : never\n : never,\n ): boolean => {\n if (!options.useKey) {\n throw new Error(\n 'useIsMutating can only be used when useKey is set to true',\n )\n }\n const isMutating = useIsMutating({\n mutationKey: mutationKey({\n urlParams: keyParams,\n }),\n })\n return isMutating > 0\n }\n result.mutationKey = mutationKey\n\n return result\n}\n","import type {\n AbstractEndpoint,\n AnyEndpointConfig,\n HttpMethod,\n} from '@navios/builder'\nimport type { InfiniteData, QueryClient } from '@tanstack/react-query'\nimport type { z, ZodObject, ZodType } from 'zod/v4'\n\nimport type { ClientOptions, ProcessResponseFunction } from '../common/types.mjs'\nimport type { MutationArgs } from '../mutation/types.mjs'\nimport type { ClientInstance } from './types.mjs'\n\nimport { makeMutation } from '../mutation/make-hook.mjs'\nimport { makeInfiniteQueryOptions } from '../query/make-infinite-options.mjs'\nimport { makeQueryOptions } from '../query/make-options.mjs'\n\n/**\n * Configuration for declaring a query endpoint.\n */\nexport interface QueryConfig<\n Method = HttpMethod,\n Url = string,\n QuerySchema = ZodObject,\n Response extends ZodType = ZodType,\n Result = z.output<Response>,\n RequestSchema = unknown,\n> {\n method: Method\n url: Url\n querySchema?: QuerySchema\n responseSchema: Response\n requestSchema?: RequestSchema\n processResponse?: (data: z.output<Response>) => Result\n}\n\n/**\n * Configuration for declaring an infinite query endpoint.\n */\nexport type InfiniteQueryConfig<\n Method = HttpMethod,\n Url = string,\n QuerySchema extends ZodObject = ZodObject,\n Response extends ZodType = ZodType,\n PageResult = z.output<Response>,\n Result = InfiniteData<PageResult>,\n RequestSchema = unknown,\n> = {\n method: Method\n url: Url\n querySchema: QuerySchema\n responseSchema: Response\n requestSchema?: RequestSchema\n processResponse?: (data: z.output<Response>) => PageResult\n select?: (data: InfiniteData<PageResult>) => Result\n getNextPageParam: (\n lastPage: PageResult,\n allPages: PageResult[],\n lastPageParam: z.infer<QuerySchema> | undefined,\n allPageParams: z.infer<QuerySchema>[] | undefined,\n ) => z.input<QuerySchema> | undefined\n getPreviousPageParam?: (\n firstPage: PageResult,\n allPages: PageResult[],\n lastPageParam: z.infer<QuerySchema> | undefined,\n allPageParams: z.infer<QuerySchema>[] | undefined,\n ) => z.input<QuerySchema>\n initialPageParam?: z.input<QuerySchema>\n}\n\n/**\n * Configuration for declaring a mutation endpoint.\n */\nexport interface MutationConfig<\n Method extends 'POST' | 'PUT' | 'PATCH' | 'DELETE' =\n | 'POST'\n | 'PUT'\n | 'PATCH'\n | 'DELETE',\n Url extends string = string,\n RequestSchema = Method extends 'DELETE' ? never : ZodObject,\n QuerySchema = unknown,\n Response extends ZodType = ZodType,\n ReqResult = z.output<Response>,\n Result = unknown,\n Context = unknown,\n UseKey extends boolean = false,\n> {\n method: Method\n url: Url\n querySchema?: QuerySchema\n responseSchema: Response\n requestSchema?: RequestSchema\n processResponse: ProcessResponseFunction<Result, ReqResult>\n useContext?: () => Context\n onSuccess?: (\n queryClient: QueryClient,\n data: NoInfer<Result>,\n variables: MutationArgs<Url, RequestSchema, QuerySchema>,\n context: Context,\n ) => void | Promise<void>\n onError?: (\n queryClient: QueryClient,\n error: Error,\n variables: MutationArgs<Url, RequestSchema, QuerySchema>,\n context: Context,\n ) => void | Promise<void>\n useKey?: UseKey\n}\n\n/**\n * Creates a client instance for making type-safe queries and mutations.\n *\n * @param options - Client configuration including the API builder and defaults\n * @returns A client instance with query, infiniteQuery, and mutation methods\n *\n * @example\n * ```typescript\n * const api = createBuilder({ baseUrl: '/api' });\n * const client = declareClient({ api });\n *\n * const getUser = client.query({\n * method: 'GET',\n * url: '/users/$id',\n * responseSchema: UserSchema,\n * });\n *\n * // In a component\n * const { data } = useSuspenseQuery(getUser({ urlParams: { id: '123' } }));\n * ```\n */\nexport function declareClient<Options extends ClientOptions>({\n api,\n defaults = {},\n}: Options): ClientInstance {\n function query(config: QueryConfig) {\n const endpoint = api.declareEndpoint({\n // @ts-expect-error we accept only specific methods\n method: config.method,\n url: config.url,\n querySchema: config.querySchema,\n requestSchema: config.requestSchema,\n responseSchema: config.responseSchema,\n })\n\n const queryOptions = makeQueryOptions(endpoint, {\n ...defaults,\n processResponse: config.processResponse ?? ((data) => data),\n })\n // @ts-expect-error We attach the endpoint to the queryOptions\n queryOptions.endpoint = endpoint\n return queryOptions\n }\n\n function queryFromEndpoint(\n endpoint: AbstractEndpoint<AnyEndpointConfig>,\n options?: {\n processResponse?: (\n data: z.output<AnyEndpointConfig['responseSchema']>,\n ) => unknown\n },\n ) {\n return makeQueryOptions(endpoint, {\n ...defaults,\n processResponse: options?.processResponse ?? ((data) => data),\n })\n }\n\n function infiniteQuery(config: InfiniteQueryConfig) {\n const endpoint = api.declareEndpoint({\n // @ts-expect-error we accept only specific methods\n method: config.method,\n url: config.url,\n querySchema: config.querySchema,\n requestSchema: config.requestSchema,\n responseSchema: config.responseSchema,\n })\n const infiniteQueryOptions = makeInfiniteQueryOptions(endpoint, {\n ...defaults,\n processResponse: config.processResponse ?? ((data) => data),\n getNextPageParam: config.getNextPageParam,\n getPreviousPageParam: config.getPreviousPageParam,\n initialPageParam: config.initialPageParam,\n })\n\n // @ts-expect-error We attach the endpoint to the infiniteQueryOptions\n infiniteQueryOptions.endpoint = endpoint\n return infiniteQueryOptions\n }\n\n function infiniteQueryFromEndpoint(\n endpoint: AbstractEndpoint<AnyEndpointConfig>,\n options: {\n processResponse?: (\n data: z.output<AnyEndpointConfig['responseSchema']>,\n ) => unknown\n getNextPageParam: (\n lastPage: z.infer<AnyEndpointConfig['responseSchema']>,\n allPages: z.infer<AnyEndpointConfig['responseSchema']>[],\n lastPageParam: z.infer<AnyEndpointConfig['querySchema']> | undefined,\n allPageParams: z.infer<AnyEndpointConfig['querySchema']>[] | undefined,\n ) => z.input<AnyEndpointConfig['querySchema']> | undefined\n getPreviousPageParam?: (\n firstPage: z.infer<AnyEndpointConfig['responseSchema']>,\n allPages: z.infer<AnyEndpointConfig['responseSchema']>[],\n lastPageParam: z.infer<AnyEndpointConfig['querySchema']> | undefined,\n allPageParams: z.infer<AnyEndpointConfig['querySchema']>[] | undefined,\n ) => z.input<AnyEndpointConfig['querySchema']>\n initialPageParam?: z.input<AnyEndpointConfig['querySchema']>\n },\n ) {\n return makeInfiniteQueryOptions(endpoint, {\n ...defaults,\n processResponse: options?.processResponse ?? ((data) => data),\n getNextPageParam: options.getNextPageParam,\n getPreviousPageParam: options?.getPreviousPageParam,\n initialPageParam: options?.initialPageParam,\n })\n }\n\n function mutation(config: MutationConfig) {\n const endpoint = api.declareEndpoint({\n // @ts-expect-error We forgot about the DELETE method in original makeMutation\n method: config.method,\n url: config.url,\n querySchema: config.querySchema,\n requestSchema: config.requestSchema,\n responseSchema: config.responseSchema,\n })\n\n const useMutation = makeMutation(endpoint, {\n processResponse: config.processResponse ?? ((data) => data),\n useContext: config.useContext,\n // @ts-expect-error We forgot about the DELETE method in original makeMutation\n onSuccess: config.onSuccess,\n // @ts-expect-error We forgot about the DELETE method in original makeMutation\n onError: config.onError,\n useKey: config.useKey,\n ...defaults,\n })\n\n // @ts-expect-error We attach the endpoint to the useMutation\n useMutation.endpoint = endpoint\n return useMutation\n }\n\n function mutationFromEndpoint(\n endpoint: AbstractEndpoint<AnyEndpointConfig>,\n options: {\n processResponse: ProcessResponseFunction\n useContext?: () => unknown\n onSuccess?: (\n queryClient: QueryClient,\n data: unknown,\n variables: MutationArgs,\n context: unknown,\n ) => void | Promise<void>\n onError?: (\n queryClient: QueryClient,\n error: Error,\n variables: MutationArgs,\n context: unknown,\n ) => void | Promise<void>\n },\n ) {\n return makeMutation(endpoint, {\n processResponse: options.processResponse,\n useContext: options.useContext,\n onSuccess: options.onSuccess,\n // @ts-expect-error simplify types here\n onError: options.onError,\n ...defaults,\n })\n }\n\n function multipartMutation(config: MutationConfig) {\n const endpoint = api.declareMultipart({\n // @ts-expect-error we accept only specific methods\n method: config.method,\n url: config.url,\n querySchema: config.querySchema,\n requestSchema: config.requestSchema,\n responseSchema: config.responseSchema,\n })\n\n const useMutation = makeMutation(endpoint, {\n processResponse: config.processResponse ?? ((data) => data),\n useContext: config.useContext,\n // @ts-expect-error We forgot about the DELETE method in original makeMutation\n onSuccess: config.onSuccess,\n // @ts-expect-error We forgot about the DELETE method in original makeMutation\n onError: config.onError,\n useKey: config.useKey,\n ...defaults,\n })\n\n // @ts-expect-error We attach the endpoint to the useMutation\n useMutation.endpoint = endpoint\n return useMutation\n }\n\n return {\n // @ts-expect-error We simplified types here\n query,\n // @ts-expect-error We simplified types here\n queryFromEndpoint,\n // @ts-expect-error We simplified types here\n infiniteQuery,\n // @ts-expect-error We simplified types here\n infiniteQueryFromEndpoint,\n // @ts-expect-error We simplified types here\n mutation,\n // @ts-expect-error We simplified types here\n mutationFromEndpoint,\n // @ts-expect-error We simplified types here\n multipartMutation,\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@navios/react-query",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Oleksandr Hanzha",
|
|
6
6
|
"email": "alex@granted.name"
|
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
},
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"peerDependencies": {
|
|
15
|
-
"@navios/builder": "^0.
|
|
15
|
+
"@navios/builder": "^0.4.0",
|
|
16
16
|
"@tanstack/react-query": "^5.51.21",
|
|
17
|
-
"zod": "^3.
|
|
17
|
+
"zod": "^3.25.0 || ^4.0.0"
|
|
18
18
|
},
|
|
19
19
|
"typings": "./lib/index.d.mts",
|
|
20
20
|
"main": "./lib/index.js",
|
|
@@ -32,11 +32,11 @@
|
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@navios/builder": "^0.
|
|
36
|
-
"@tanstack/react-query": "^5.
|
|
35
|
+
"@navios/builder": "^0.4.0",
|
|
36
|
+
"@tanstack/react-query": "^5.90.12",
|
|
37
37
|
"navios": "^0.5.0",
|
|
38
|
-
"react": "^19.
|
|
39
|
-
"typescript": "^5.
|
|
40
|
-
"zod": "^
|
|
38
|
+
"react": "^19.2.3",
|
|
39
|
+
"typescript": "^5.9.3",
|
|
40
|
+
"zod": "^4.1.13"
|
|
41
41
|
}
|
|
42
42
|
}
|
package/project.json
CHANGED
|
@@ -15,6 +15,14 @@
|
|
|
15
15
|
"cwd": "packages/react-query"
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
|
+
"lint": {
|
|
19
|
+
"executor": "nx:run-commands",
|
|
20
|
+
"inputs": ["^projectSources", "project"],
|
|
21
|
+
"options": {
|
|
22
|
+
"command": "oxlint --fix",
|
|
23
|
+
"cwd": "packages/react-query"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
18
26
|
"test:ci": {
|
|
19
27
|
"executor": "nx:run-commands",
|
|
20
28
|
"inputs": ["^projectSources", "project"],
|
|
@@ -27,7 +35,7 @@
|
|
|
27
35
|
"executor": "nx:run-commands",
|
|
28
36
|
"inputs": ["projectSources"],
|
|
29
37
|
"outputs": ["{projectRoot}/lib"],
|
|
30
|
-
"dependsOn": ["check", "test:ci"],
|
|
38
|
+
"dependsOn": ["check", "test:ci", "lint"],
|
|
31
39
|
"options": {
|
|
32
40
|
"command": "tsup",
|
|
33
41
|
"cwd": "packages/react-query"
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { builder } from '@navios/builder'
|
|
2
2
|
|
|
3
3
|
import { describe, expect, it } from 'vitest'
|
|
4
|
-
import { z } from 'zod'
|
|
4
|
+
import { z } from 'zod/v4'
|
|
5
5
|
|
|
6
|
-
import { declareClient } from '../declare-client.mjs'
|
|
6
|
+
import { declareClient } from '../client/declare-client.mjs'
|
|
7
7
|
|
|
8
8
|
describe('client type check', () => {
|
|
9
9
|
const api = builder({
|
|
@@ -4,9 +4,9 @@ import { makeNaviosFakeAdapter } from 'navios/testing'
|
|
|
4
4
|
import { builder } from '@navios/builder'
|
|
5
5
|
|
|
6
6
|
import { describe, expect, it, vi } from 'vitest'
|
|
7
|
-
import { z } from 'zod'
|
|
7
|
+
import { z } from 'zod/v4'
|
|
8
8
|
|
|
9
|
-
import { declareClient } from '../declare-client.mjs'
|
|
9
|
+
import { declareClient } from '../client/declare-client.mjs'
|
|
10
10
|
|
|
11
11
|
vi.mock('@tanstack/react-query', async (importReal) => {
|
|
12
12
|
const actual = await importReal<typeof import('@tanstack/react-query')>()
|
|
@@ -4,9 +4,9 @@ import { makeNaviosFakeAdapter } from 'navios/testing'
|
|
|
4
4
|
import { builder } from '@navios/builder'
|
|
5
5
|
|
|
6
6
|
import { describe, expect, it, vi } from 'vitest'
|
|
7
|
-
import { z } from 'zod'
|
|
7
|
+
import { z } from 'zod/v4'
|
|
8
8
|
|
|
9
|
-
import { makeMutation } from '../make-
|
|
9
|
+
import { makeMutation } from '../mutation/make-hook.mjs'
|
|
10
10
|
|
|
11
11
|
vi.mock('@tanstack/react-query', async (importReal) => {
|
|
12
12
|
const actual = await importReal<typeof import('@tanstack/react-query')>()
|
|
@@ -2,9 +2,9 @@ import { builder } from '@navios/builder'
|
|
|
2
2
|
|
|
3
3
|
import { QueryClient } from '@tanstack/react-query'
|
|
4
4
|
import { describe, expect, it } from 'vitest'
|
|
5
|
-
import { z } from 'zod'
|
|
5
|
+
import { z } from 'zod/v4'
|
|
6
6
|
|
|
7
|
-
import { makeQueryOptions } from '../make-
|
|
7
|
+
import { makeQueryOptions } from '../query/make-options.mjs'
|
|
8
8
|
|
|
9
9
|
describe('makeDataTag', () => {
|
|
10
10
|
it('should return a string', () => {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { builder } from '@navios/builder'
|
|
2
2
|
|
|
3
3
|
import { describe, expect, it } from 'vitest'
|
|
4
|
-
import { z } from 'zod'
|
|
4
|
+
import { z } from 'zod/v4'
|
|
5
5
|
|
|
6
|
-
import { makeInfiniteQueryOptions } from '../make-infinite-
|
|
6
|
+
import { makeInfiniteQueryOptions } from '../query/make-infinite-options.mjs'
|
|
7
7
|
|
|
8
8
|
describe('makeInfiniteQueryOptions', () => {
|
|
9
9
|
const api = builder({})
|
|
@@ -36,7 +36,6 @@ describe('makeInfiniteQueryOptions', () => {
|
|
|
36
36
|
},
|
|
37
37
|
)
|
|
38
38
|
const options = makeOptions({
|
|
39
|
-
// @ts-expect-error it's internal type
|
|
40
39
|
urlParams: { testId: '1', fooId: '2' },
|
|
41
40
|
params: {},
|
|
42
41
|
})
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { builder } from '@navios/builder'
|
|
2
2
|
|
|
3
3
|
import { describe, expect, it } from 'vitest'
|
|
4
|
-
import { z } from 'zod'
|
|
4
|
+
import { z } from 'zod/v4'
|
|
5
5
|
|
|
6
|
-
import { makeQueryOptions } from '../make-
|
|
6
|
+
import { makeQueryOptions } from '../query/make-options.mjs'
|
|
7
7
|
|
|
8
8
|
describe('makeQueryOptions', () => {
|
|
9
9
|
const api = builder({})
|