@navios/react-query 0.6.1 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.md +142 -41
  3. package/dist/src/client/declare-client.d.mts.map +1 -1
  4. package/dist/src/mutation/make-hook.d.mts +3 -1
  5. package/dist/src/mutation/make-hook.d.mts.map +1 -1
  6. package/dist/tsconfig.tsbuildinfo +1 -1
  7. package/lib/index.cjs +412 -0
  8. package/lib/index.cjs.map +1 -0
  9. package/lib/index.d.cts +961 -0
  10. package/lib/index.d.cts.map +1 -0
  11. package/lib/index.d.mts +961 -34
  12. package/lib/index.d.mts.map +1 -0
  13. package/lib/index.mjs +389 -350
  14. package/lib/index.mjs.map +1 -1
  15. package/package.json +8 -8
  16. package/project.json +2 -2
  17. package/src/__tests__/declare-client.spec.mts +2 -2
  18. package/src/__tests__/make-mutation.spec.mts +10 -9
  19. package/src/client/declare-client.mts +28 -6
  20. package/src/mutation/make-hook.mts +3 -11
  21. package/{tsup.config.mts → tsdown.config.mts} +4 -3
  22. package/dist/src/declare-client.d.mts +0 -31
  23. package/dist/src/declare-client.d.mts.map +0 -1
  24. package/dist/src/make-infinite-query-options.d.mts +0 -13
  25. package/dist/src/make-infinite-query-options.d.mts.map +0 -1
  26. package/dist/src/make-mutation.d.mts +0 -15
  27. package/dist/src/make-mutation.d.mts.map +0 -1
  28. package/dist/src/make-query-options.d.mts +0 -15
  29. package/dist/src/make-query-options.d.mts.map +0 -1
  30. package/dist/src/types/client-endpoint-helper.d.mts +0 -8
  31. package/dist/src/types/client-endpoint-helper.d.mts.map +0 -1
  32. package/dist/src/types/client-instance.d.mts +0 -211
  33. package/dist/src/types/client-instance.d.mts.map +0 -1
  34. package/dist/src/types/index.d.mts +0 -8
  35. package/dist/src/types/index.d.mts.map +0 -1
  36. package/dist/src/types/mutation-args.d.mts +0 -10
  37. package/dist/src/types/mutation-args.d.mts.map +0 -1
  38. package/dist/src/types/mutation-helpers.d.mts +0 -10
  39. package/dist/src/types/mutation-helpers.d.mts.map +0 -1
  40. package/dist/src/types/query-args.d.mts +0 -8
  41. package/dist/src/types/query-args.d.mts.map +0 -1
  42. package/dist/src/types/query-helpers.d.mts +0 -14
  43. package/dist/src/types/query-helpers.d.mts.map +0 -1
  44. package/dist/src/types/query-url-params-args.d.mts +0 -5
  45. package/dist/src/types/query-url-params-args.d.mts.map +0 -1
  46. package/dist/src/types.d.mts +0 -49
  47. package/dist/src/types.d.mts.map +0 -1
  48. package/dist/src/utils/mutation-key.creator.d.mts +0 -39
  49. package/dist/src/utils/mutation-key.creator.d.mts.map +0 -1
  50. package/dist/src/utils/query-key-creator.d.mts +0 -24
  51. package/dist/src/utils/query-key-creator.d.mts.map +0 -1
  52. package/lib/_tsup-dts-rollup.d.mts +0 -1097
  53. package/lib/_tsup-dts-rollup.d.ts +0 -1097
  54. package/lib/index.d.ts +0 -34
  55. package/lib/index.js +0 -375
  56. package/lib/index.js.map +0 -1
package/lib/index.mjs CHANGED
@@ -1,366 +1,405 @@
1
- import { bindUrlParams } from '@navios/builder';
2
- import { useQuery, useSuspenseQuery, useInfiniteQuery, useSuspenseInfiniteQuery, useIsMutating, infiniteQueryOptions, useMutation, queryOptions } from '@tanstack/react-query';
1
+ import { bindUrlParams } from "@navios/builder";
2
+ import { infiniteQueryOptions, queryOptions, useInfiniteQuery, useIsMutating, useMutation, useQuery, useSuspenseInfiniteQuery, useSuspenseQuery } from "@tanstack/react-query";
3
3
 
4
- // src/query/key-creator.mts
4
+ //#region src/query/key-creator.mts
5
+ /**
6
+ * Creates a query key generator for a given endpoint configuration.
7
+ *
8
+ * The returned object provides methods to generate query keys that can be used
9
+ * with TanStack Query for caching, invalidation, and data tagging.
10
+ *
11
+ * @param config - The endpoint configuration
12
+ * @param options - Query parameters including processResponse and key prefix/suffix
13
+ * @param isInfinite - Whether this is for an infinite query
14
+ * @returns An object with methods to generate query keys
15
+ */
5
16
  function createQueryKey(config, options, _isInfinite) {
6
- const url = config.url;
7
- const urlParts = url.split("/").filter(Boolean);
8
- return {
9
- template: urlParts,
10
- // @ts-expect-error We have correct types in return type
11
- dataTag: (params) => {
12
- const queryParams = params && "querySchema" in config && "params" in params ? config.querySchema?.parse(params.params) : [];
13
- return [
14
- ...options.keyPrefix ?? [],
15
- ...urlParts.map(
16
- (part) => part.startsWith("$") ? (
17
- // @ts-expect-error TS2339 We know that the urlParams are defined only if the url has params
18
- params.urlParams[part.slice(1)].toString()
19
- ) : part
20
- ),
21
- ...options.keySuffix ?? [],
22
- queryParams ?? []
23
- ];
24
- },
25
- // @ts-expect-error We have correct types in return type
26
- filterKey: (params) => {
27
- return [
28
- ...options.keyPrefix ?? [],
29
- ...urlParts.map(
30
- (part) => part.startsWith("$") ? (
31
- // @ts-expect-error TS2339 We know that the urlParams are defined only if the url has params
32
- params.urlParams[part.slice(1)].toString()
33
- ) : part
34
- ),
35
- ...options.keySuffix ?? []
36
- ];
37
- },
38
- bindToUrl: (params) => {
39
- return bindUrlParams(url, params ?? {});
40
- }
41
- };
17
+ const url = config.url;
18
+ const urlParts = url.split("/").filter(Boolean);
19
+ return {
20
+ template: urlParts,
21
+ dataTag: (params) => {
22
+ const queryParams = params && "querySchema" in config && "params" in params ? config.querySchema?.parse(params.params) : [];
23
+ return [
24
+ ...options.keyPrefix ?? [],
25
+ ...urlParts.map((part) => part.startsWith("$") ? params.urlParams[part.slice(1)].toString() : part),
26
+ ...options.keySuffix ?? [],
27
+ queryParams ?? []
28
+ ];
29
+ },
30
+ filterKey: (params) => {
31
+ return [
32
+ ...options.keyPrefix ?? [],
33
+ ...urlParts.map((part) => part.startsWith("$") ? params.urlParams[part.slice(1)].toString() : part),
34
+ ...options.keySuffix ?? []
35
+ ];
36
+ },
37
+ bindToUrl: (params) => {
38
+ return bindUrlParams(url, params ?? {});
39
+ }
40
+ };
42
41
  }
43
- var queryKeyCreator = createQueryKey;
42
+ /** @deprecated Use createQueryKey instead */
43
+ const queryKeyCreator = createQueryKey;
44
+
45
+ //#endregion
46
+ //#region src/query/make-options.mts
47
+ /**
48
+ * Creates query options for a given endpoint.
49
+ *
50
+ * Returns a function that generates TanStack Query options when called with params.
51
+ * The returned function also has helper methods attached (use, useSuspense, invalidate, etc.)
52
+ *
53
+ * @param endpoint - The navios endpoint to create query options for
54
+ * @param options - Query configuration including processResponse
55
+ * @param baseQuery - Optional base query options to merge
56
+ * @returns A function that generates query options with attached helpers
57
+ */
44
58
  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
- });
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;
59
+ const config = endpoint.config;
60
+ const queryKey = createQueryKey(config, options, false);
61
+ const processResponse = options.processResponse;
62
+ const result = (params) => {
63
+ return queryOptions({
64
+ queryKey: queryKey.dataTag(params),
65
+ queryFn: async ({ signal }) => {
66
+ let result$1;
67
+ try {
68
+ result$1 = await endpoint({
69
+ signal,
70
+ ...params
71
+ });
72
+ } catch (err) {
73
+ if (options.onFail) options.onFail(err);
74
+ throw err;
75
+ }
76
+ return processResponse(result$1);
77
+ },
78
+ ...baseQuery
79
+ });
80
+ };
81
+ result.queryKey = queryKey;
82
+ result.use = (params) => {
83
+ return useQuery(result(params));
84
+ };
85
+ result.useSuspense = (params) => {
86
+ return useSuspenseQuery(result(params));
87
+ };
88
+ result.invalidate = (queryClient, params) => {
89
+ return queryClient.invalidateQueries({ queryKey: result.queryKey.dataTag(params) });
90
+ };
91
+ result.invalidateAll = (queryClient, params) => {
92
+ return queryClient.invalidateQueries({
93
+ queryKey: result.queryKey.filterKey(params),
94
+ exact: false
95
+ });
96
+ };
97
+ return result;
88
98
  }
99
+
100
+ //#endregion
101
+ //#region src/query/make-infinite-options.mts
102
+ /**
103
+ * Creates infinite query options for a given endpoint.
104
+ *
105
+ * Returns a function that generates TanStack Query infinite options when called with params.
106
+ * The returned function also has helper methods attached (use, useSuspense, invalidate, etc.)
107
+ *
108
+ * @param endpoint - The navios endpoint to create infinite query options for
109
+ * @param options - Infinite query configuration including processResponse and pagination params
110
+ * @param baseQuery - Optional base query options to merge
111
+ * @returns A function that generates infinite query options with attached helpers
112
+ */
89
113
  function makeInfiniteQueryOptions(endpoint, options, baseQuery = {}) {
90
- const config = endpoint.config;
91
- const queryKey = createQueryKey(config, options);
92
- const processResponse = options.processResponse;
93
- const res = (params) => {
94
- return infiniteQueryOptions({
95
- queryKey: queryKey.dataTag(params),
96
- queryFn: async ({ signal, pageParam }) => {
97
- let result;
98
- try {
99
- result = await endpoint({
100
- signal,
101
- // @ts-expect-error TS2345 We bind the url params only if the url has params
102
- urlParams: params.urlParams,
103
- params: {
104
- ..."params" in params ? params.params : {},
105
- ...pageParam
106
- }
107
- });
108
- } catch (err) {
109
- if (options.onFail) {
110
- options.onFail(err);
111
- }
112
- throw err;
113
- }
114
- return processResponse(result);
115
- },
116
- getNextPageParam: options.getNextPageParam,
117
- getPreviousPageParam: options.getPreviousPageParam,
118
- initialPageParam: options.initialPageParam ?? config.querySchema.parse("params" in params ? params.params : {}),
119
- ...baseQuery
120
- });
121
- };
122
- res.queryKey = queryKey;
123
- res.use = (params) => {
124
- return useInfiniteQuery(res(params));
125
- };
126
- res.useSuspense = (params) => {
127
- return useSuspenseInfiniteQuery(res(params));
128
- };
129
- res.invalidate = (queryClient, params) => {
130
- return queryClient.invalidateQueries({
131
- queryKey: res.queryKey.dataTag(params)
132
- });
133
- };
134
- res.invalidateAll = (queryClient, params) => {
135
- return queryClient.invalidateQueries({
136
- queryKey: res.queryKey.filterKey(params),
137
- exact: false
138
- });
139
- };
140
- return res;
114
+ const config = endpoint.config;
115
+ const queryKey = createQueryKey(config, options, true);
116
+ const processResponse = options.processResponse;
117
+ const res = (params) => {
118
+ return infiniteQueryOptions({
119
+ queryKey: queryKey.dataTag(params),
120
+ queryFn: async ({ signal, pageParam }) => {
121
+ let result;
122
+ try {
123
+ result = await endpoint({
124
+ signal,
125
+ urlParams: params.urlParams,
126
+ params: {
127
+ ..."params" in params ? params.params : {},
128
+ ...pageParam
129
+ }
130
+ });
131
+ } catch (err) {
132
+ if (options.onFail) options.onFail(err);
133
+ throw err;
134
+ }
135
+ return processResponse(result);
136
+ },
137
+ getNextPageParam: options.getNextPageParam,
138
+ getPreviousPageParam: options.getPreviousPageParam,
139
+ initialPageParam: options.initialPageParam ?? config.querySchema.parse("params" in params ? params.params : {}),
140
+ ...baseQuery
141
+ });
142
+ };
143
+ res.queryKey = queryKey;
144
+ res.use = (params) => {
145
+ return useInfiniteQuery(res(params));
146
+ };
147
+ res.useSuspense = (params) => {
148
+ return useSuspenseInfiniteQuery(res(params));
149
+ };
150
+ res.invalidate = (queryClient, params) => {
151
+ return queryClient.invalidateQueries({ queryKey: res.queryKey.dataTag(params) });
152
+ };
153
+ res.invalidateAll = (queryClient, params) => {
154
+ return queryClient.invalidateQueries({
155
+ queryKey: res.queryKey.filterKey(params),
156
+ exact: false
157
+ });
158
+ };
159
+ return res;
141
160
  }
142
161
 
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
- };
162
+ //#endregion
163
+ //#region src/mutation/key-creator.mts
164
+ /**
165
+ * Creates a mutation key generator for a given endpoint configuration.
166
+ *
167
+ * @param config - The endpoint configuration
168
+ * @param options - Optional query parameters with a default `processResponse` function
169
+ * @returns A function that generates mutation keys
170
+ *
171
+ * @example Basic usage:
172
+ * ```typescript
173
+ * const createMutationKey = createMutationKey(endpoint.config);
174
+ * const mutationKey = createMutationKey({ urlParams: { id: 123 } });
175
+ * ```
176
+ *
177
+ * @example Advanced usage with processResponse:
178
+ * ```ts
179
+ * const createMutationKey = createMutationKey(endpoint.config, {
180
+ * processResponse: (data) => {
181
+ * if (!data.success) {
182
+ * throw new Error(data.message);
183
+ * }
184
+ * return data.data;
185
+ * },
186
+ * });
187
+ * // We create a mutation that will be shared across the project for all passed userId
188
+ * const mutationKey = createMutationKey({ urlParams: { projectId: 123, userId: 'wildcard' } });
189
+ * ```
190
+ */
191
+ function createMutationKey(config, options = { processResponse: (data) => data }) {
192
+ const queryKey = createQueryKey(config, options, false);
193
+ return (params) => {
194
+ return queryKey.filterKey(params);
195
+ };
151
196
  }
152
- var mutationKeyCreator = createMutationKey;
197
+ /** @deprecated Use createMutationKey instead */
198
+ const mutationKeyCreator = createMutationKey;
199
+
200
+ //#endregion
201
+ //#region src/mutation/make-hook.mts
202
+ /**
203
+ * Creates a mutation hook for a given endpoint.
204
+ *
205
+ * Returns a function that when called returns a TanStack Query mutation result.
206
+ * The returned function also has helper methods attached (mutationKey, useIsMutating).
207
+ *
208
+ * @param endpoint - The navios endpoint to create a mutation hook for
209
+ * @param options - Mutation configuration including processResponse and callbacks
210
+ * @returns A hook function that returns mutation result with attached helpers
211
+ */
153
212
  function makeMutation(endpoint, options) {
154
- const config = endpoint.config;
155
- const mutationKey = createMutationKey(config, {
156
- ...options});
157
- const result = (keyParams) => {
158
- const {
159
- useKey,
160
- useContext,
161
- onMutate,
162
- onError,
163
- onSuccess,
164
- onSettled,
165
- keyPrefix: _keyPrefix,
166
- keySuffix: _keySuffix,
167
- processResponse,
168
- ...rest
169
- } = options;
170
- const ownContext = useContext?.() ?? {};
171
- return useMutation({
172
- ...rest,
173
- mutationKey: useKey ? mutationKey({
174
- urlParams: keyParams
175
- }) : void 0,
176
- scope: useKey ? {
177
- id: JSON.stringify(
178
- mutationKey({
179
- urlParams: keyParams
180
- })
181
- )
182
- } : void 0,
183
- async mutationFn(params) {
184
- const response = await endpoint(params);
185
- return processResponse ? processResponse(response) : response;
186
- },
187
- onSuccess: onSuccess ? (data, variables, onMutateResult, context) => {
188
- return onSuccess?.(data, variables, {
189
- ...ownContext,
190
- ...context,
191
- onMutateResult
192
- });
193
- } : void 0,
194
- onError: onError ? (err, variables, onMutateResult, context) => {
195
- return onError?.(err, variables, {
196
- onMutateResult,
197
- ...ownContext,
198
- ...context
199
- });
200
- } : void 0,
201
- onMutate: onMutate ? (variables, context) => {
202
- return onMutate(variables, {
203
- ...ownContext,
204
- ...context
205
- });
206
- } : void 0,
207
- onSettled: onSettled ? (data, error, variables, onMutateResult, context) => {
208
- return onSettled(data, error, variables, {
209
- ...ownContext,
210
- ...context,
211
- onMutateResult
212
- });
213
- } : void 0
214
- });
215
- };
216
- result.useIsMutating = (keyParams) => {
217
- if (!options.useKey) {
218
- throw new Error(
219
- "useIsMutating can only be used when useKey is set to true"
220
- );
221
- }
222
- const isMutating = useIsMutating({
223
- mutationKey: mutationKey({
224
- urlParams: keyParams
225
- })
226
- });
227
- return isMutating > 0;
228
- };
229
- result.mutationKey = mutationKey;
230
- return result;
213
+ const config = endpoint.config;
214
+ const mutationKey = createMutationKey(config, {
215
+ ...options,
216
+ processResponse: options.processResponse ?? ((data) => data)
217
+ });
218
+ const result = (keyParams) => {
219
+ const { useKey, useContext, onMutate, onError, onSuccess, onSettled, keyPrefix: _keyPrefix, keySuffix: _keySuffix, processResponse, ...rest } = options;
220
+ const ownContext = useContext?.() ?? {};
221
+ return useMutation({
222
+ ...rest,
223
+ mutationKey: useKey ? mutationKey(keyParams) : void 0,
224
+ scope: useKey ? { id: JSON.stringify(mutationKey(keyParams)) } : void 0,
225
+ async mutationFn(params) {
226
+ const response = await endpoint(params);
227
+ return processResponse ? processResponse(response) : response;
228
+ },
229
+ onSuccess: onSuccess ? (data, variables, onMutateResult, context) => {
230
+ return onSuccess?.(data, variables, {
231
+ ...ownContext,
232
+ ...context,
233
+ onMutateResult
234
+ });
235
+ } : void 0,
236
+ onError: onError ? (err, variables, onMutateResult, context) => {
237
+ return onError?.(err, variables, {
238
+ onMutateResult,
239
+ ...ownContext,
240
+ ...context
241
+ });
242
+ } : void 0,
243
+ onMutate: onMutate ? (variables, context) => {
244
+ return onMutate(variables, {
245
+ ...ownContext,
246
+ ...context
247
+ });
248
+ } : void 0,
249
+ onSettled: onSettled ? (data, error, variables, onMutateResult, context) => {
250
+ return onSettled(data, error, variables, {
251
+ ...ownContext,
252
+ ...context,
253
+ onMutateResult
254
+ });
255
+ } : void 0
256
+ });
257
+ };
258
+ result.useIsMutating = (keyParams) => {
259
+ if (!options.useKey) throw new Error("useIsMutating can only be used when useKey is set to true");
260
+ return useIsMutating({ mutationKey: mutationKey({ urlParams: keyParams }) }) > 0;
261
+ };
262
+ result.mutationKey = mutationKey;
263
+ return result;
231
264
  }
232
265
 
233
- // src/client/declare-client.mts
234
- function declareClient({
235
- api,
236
- defaults = {}
237
- }) {
238
- function query(config) {
239
- const endpoint = api.declareEndpoint({
240
- // @ts-expect-error we accept only specific methods
241
- method: config.method,
242
- url: config.url,
243
- querySchema: config.querySchema,
244
- requestSchema: config.requestSchema,
245
- responseSchema: config.responseSchema
246
- });
247
- const queryOptions2 = makeQueryOptions(endpoint, {
248
- ...defaults,
249
- processResponse: config.processResponse ?? ((data) => data)
250
- });
251
- queryOptions2.endpoint = endpoint;
252
- return queryOptions2;
253
- }
254
- function queryFromEndpoint(endpoint, options) {
255
- return makeQueryOptions(endpoint, {
256
- ...defaults,
257
- processResponse: options?.processResponse ?? ((data) => data)
258
- });
259
- }
260
- function infiniteQuery(config) {
261
- const endpoint = api.declareEndpoint({
262
- // @ts-expect-error we accept only specific methods
263
- method: config.method,
264
- url: config.url,
265
- querySchema: config.querySchema,
266
- requestSchema: config.requestSchema,
267
- responseSchema: config.responseSchema
268
- });
269
- const infiniteQueryOptions2 = makeInfiniteQueryOptions(endpoint, {
270
- ...defaults,
271
- processResponse: config.processResponse ?? ((data) => data),
272
- getNextPageParam: config.getNextPageParam,
273
- getPreviousPageParam: config.getPreviousPageParam,
274
- initialPageParam: config.initialPageParam
275
- });
276
- infiniteQueryOptions2.endpoint = endpoint;
277
- return infiniteQueryOptions2;
278
- }
279
- function infiniteQueryFromEndpoint(endpoint, options) {
280
- return makeInfiniteQueryOptions(endpoint, {
281
- ...defaults,
282
- processResponse: options?.processResponse ?? ((data) => data),
283
- getNextPageParam: options.getNextPageParam,
284
- getPreviousPageParam: options?.getPreviousPageParam,
285
- initialPageParam: options?.initialPageParam
286
- });
287
- }
288
- function mutation(config) {
289
- const endpoint = api.declareEndpoint({
290
- // @ts-expect-error We forgot about the DELETE method in original makeMutation
291
- method: config.method,
292
- url: config.url,
293
- querySchema: config.querySchema,
294
- requestSchema: config.requestSchema,
295
- responseSchema: config.responseSchema
296
- });
297
- const useMutation2 = makeMutation(endpoint, {
298
- processResponse: config.processResponse ?? ((data) => data),
299
- useContext: config.useContext,
300
- // @ts-expect-error We forgot about the DELETE method in original makeMutation
301
- onSuccess: config.onSuccess,
302
- // @ts-expect-error We forgot about the DELETE method in original makeMutation
303
- onError: config.onError,
304
- useKey: config.useKey,
305
- meta: config.meta,
306
- ...defaults
307
- });
308
- useMutation2.endpoint = endpoint;
309
- return useMutation2;
310
- }
311
- function mutationFromEndpoint(endpoint, options) {
312
- return makeMutation(endpoint, {
313
- processResponse: options?.processResponse,
314
- useContext: options?.useContext,
315
- onSuccess: options?.onSuccess,
316
- onError: options?.onError,
317
- ...defaults
318
- });
319
- }
320
- function multipartMutation(config) {
321
- const endpoint = api.declareMultipart({
322
- // @ts-expect-error we accept only specific methods
323
- method: config.method,
324
- url: config.url,
325
- querySchema: config.querySchema,
326
- requestSchema: config.requestSchema,
327
- responseSchema: config.responseSchema
328
- });
329
- const useMutation2 = makeMutation(endpoint, {
330
- processResponse: config.processResponse ?? ((data) => data),
331
- useContext: config.useContext,
332
- // @ts-expect-error We forgot about the DELETE method in original makeMutation
333
- onSuccess: config.onSuccess,
334
- // @ts-expect-error We forgot about the DELETE method in original makeMutation
335
- onError: config.onError,
336
- // @ts-expect-error We forgot about the DELETE method in original makeMutation
337
- onMutate: config.onMutate,
338
- // @ts-expect-error We forgot about the DELETE method in original makeMutation
339
- onSettled: config.onSettled,
340
- useKey: config.useKey,
341
- ...defaults
342
- });
343
- useMutation2.endpoint = endpoint;
344
- return useMutation2;
345
- }
346
- return {
347
- // @ts-expect-error We simplified types here
348
- query,
349
- // @ts-expect-error We simplified types here
350
- queryFromEndpoint,
351
- // @ts-expect-error We simplified types here
352
- infiniteQuery,
353
- // @ts-expect-error We simplified types here
354
- infiniteQueryFromEndpoint,
355
- // @ts-expect-error We simplified types here
356
- mutation,
357
- // @ts-expect-error We simplified types here
358
- mutationFromEndpoint,
359
- // @ts-expect-error We simplified types here
360
- multipartMutation
361
- };
266
+ //#endregion
267
+ //#region src/client/declare-client.mts
268
+ /**
269
+ * Creates a client instance for making type-safe queries and mutations.
270
+ *
271
+ * @param options - Client configuration including the API builder and defaults
272
+ * @returns A client instance with query, infiniteQuery, and mutation methods
273
+ *
274
+ * @example
275
+ * ```typescript
276
+ * const api = createBuilder({ baseUrl: '/api' });
277
+ * const client = declareClient({ api });
278
+ *
279
+ * const getUser = client.query({
280
+ * method: 'GET',
281
+ * url: '/users/$id',
282
+ * responseSchema: UserSchema,
283
+ * });
284
+ *
285
+ * // In a component
286
+ * const { data } = useSuspenseQuery(getUser({ urlParams: { id: '123' } }));
287
+ * ```
288
+ */
289
+ function declareClient({ api, defaults = {} }) {
290
+ function query(config) {
291
+ const endpoint = api.declareEndpoint({
292
+ method: config.method,
293
+ url: config.url,
294
+ querySchema: config.querySchema,
295
+ requestSchema: config.requestSchema,
296
+ responseSchema: config.responseSchema
297
+ });
298
+ const queryOptions$1 = makeQueryOptions(endpoint, {
299
+ ...defaults,
300
+ processResponse: config.processResponse ?? ((data) => data)
301
+ });
302
+ queryOptions$1.endpoint = endpoint;
303
+ return queryOptions$1;
304
+ }
305
+ function queryFromEndpoint(endpoint, options) {
306
+ return makeQueryOptions(endpoint, {
307
+ ...defaults,
308
+ processResponse: options?.processResponse ?? ((data) => data)
309
+ });
310
+ }
311
+ function infiniteQuery(config) {
312
+ const endpoint = api.declareEndpoint({
313
+ method: config.method,
314
+ url: config.url,
315
+ querySchema: config.querySchema,
316
+ requestSchema: config.requestSchema,
317
+ responseSchema: config.responseSchema
318
+ });
319
+ const infiniteQueryOptions$1 = makeInfiniteQueryOptions(endpoint, {
320
+ ...defaults,
321
+ processResponse: config.processResponse ?? ((data) => data),
322
+ getNextPageParam: config.getNextPageParam,
323
+ getPreviousPageParam: config.getPreviousPageParam,
324
+ initialPageParam: config.initialPageParam
325
+ });
326
+ infiniteQueryOptions$1.endpoint = endpoint;
327
+ return infiniteQueryOptions$1;
328
+ }
329
+ function infiniteQueryFromEndpoint(endpoint, options) {
330
+ return makeInfiniteQueryOptions(endpoint, {
331
+ ...defaults,
332
+ processResponse: options?.processResponse ?? ((data) => data),
333
+ getNextPageParam: options.getNextPageParam,
334
+ getPreviousPageParam: options?.getPreviousPageParam,
335
+ initialPageParam: options?.initialPageParam
336
+ });
337
+ }
338
+ function mutation(config) {
339
+ const endpoint = api.declareEndpoint({
340
+ method: config.method,
341
+ url: config.url,
342
+ querySchema: config.querySchema,
343
+ requestSchema: config.requestSchema,
344
+ responseSchema: config.responseSchema
345
+ });
346
+ const useMutation$1 = makeMutation(endpoint, {
347
+ processResponse: config.processResponse ?? ((data) => data),
348
+ useContext: config.useContext,
349
+ onSuccess: config.onSuccess,
350
+ onError: config.onError,
351
+ useKey: config.useKey,
352
+ meta: config.meta,
353
+ ...defaults
354
+ });
355
+ useMutation$1.endpoint = endpoint;
356
+ return useMutation$1;
357
+ }
358
+ function mutationFromEndpoint(endpoint, options) {
359
+ return makeMutation(endpoint, {
360
+ processResponse: options?.processResponse,
361
+ useContext: options?.useContext,
362
+ onMutate: options?.onMutate,
363
+ onSuccess: options?.onSuccess,
364
+ onError: options?.onError,
365
+ onSettled: options?.onSettled,
366
+ useKey: options?.useKey,
367
+ meta: options?.meta,
368
+ ...defaults
369
+ });
370
+ }
371
+ function multipartMutation(config) {
372
+ const endpoint = api.declareMultipart({
373
+ method: config.method,
374
+ url: config.url,
375
+ querySchema: config.querySchema,
376
+ requestSchema: config.requestSchema,
377
+ responseSchema: config.responseSchema
378
+ });
379
+ const useMutation$1 = makeMutation(endpoint, {
380
+ processResponse: config.processResponse ?? ((data) => data),
381
+ useContext: config.useContext,
382
+ onSuccess: config.onSuccess,
383
+ onError: config.onError,
384
+ onMutate: config.onMutate,
385
+ onSettled: config.onSettled,
386
+ useKey: config.useKey,
387
+ ...defaults
388
+ });
389
+ useMutation$1.endpoint = endpoint;
390
+ return useMutation$1;
391
+ }
392
+ return {
393
+ query,
394
+ queryFromEndpoint,
395
+ infiniteQuery,
396
+ infiniteQueryFromEndpoint,
397
+ mutation,
398
+ mutationFromEndpoint,
399
+ multipartMutation
400
+ };
362
401
  }
363
402
 
403
+ //#endregion
364
404
  export { createMutationKey, createQueryKey, declareClient, makeInfiniteQueryOptions, makeMutation, makeQueryOptions, mutationKeyCreator, queryKeyCreator };
365
- //# sourceMappingURL=index.mjs.map
366
405
  //# sourceMappingURL=index.mjs.map