@navios/react-query 0.1.0 → 0.1.2

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/dist/index.d.ts CHANGED
@@ -1,346 +1,26 @@
1
- import * as _navios_common from '@navios/common';
2
- import { AnyEndpointConfig, BuilderInstance, NaviosZodRequest, UrlHasParams, BaseEndpointConfig, HttpMethod, UrlParams, Util_FlatObject, AbstractEndpoint } from '@navios/common';
3
- import * as _tanstack_react_query from '@tanstack/react-query';
4
- import { UseMutationOptions, QueryClient, DataTag, InfiniteData, UseQueryResult, UseSuspenseQueryResult, UseSuspenseQueryOptions, UseSuspenseInfiniteQueryOptions, UseMutationResult, UseInfiniteQueryOptions, UseQueryOptions } from '@tanstack/react-query';
5
- import { z, AnyZodObject, ZodType } from 'zod';
6
-
7
- type Split$2<S extends string, D extends string> = string extends S ? string[] : S extends '' ? [] : S extends `${infer T}${D}${infer U}` ? [T, ...Split$2<U, D>] : [S];
8
- type ProcessResponseFunction<TData = unknown, TVariables = unknown> = (variables: TVariables) => Promise<TData> | TData;
9
- type ClientOptions<ProcessResponse = unknown> = {
10
- api: BuilderInstance;
11
- defaults?: {
12
- keyPrefix?: string[];
13
- keySuffix?: string[];
14
- };
15
- };
16
- type BaseQueryParams<Config extends AnyEndpointConfig, Res = any> = {
17
- keyPrefix?: string[];
18
- keySuffix?: string[];
19
- onFail?: (err: unknown) => void;
20
- processResponse: (data: z.output<Config['responseSchema']>) => Res;
21
- };
22
- interface BaseMutationParams<Config extends AnyEndpointConfig, TData = unknown, TVariables = BaseMutationArgs<Config>, TResponse = z.output<Config['responseSchema']>, TContext = unknown, UseKey extends boolean = false> extends Omit<UseMutationOptions<TData, Error, TVariables>, 'mutationKey' | 'mutationFn' | 'onSuccess' | 'onError' | 'scope'> {
23
- processResponse: ProcessResponseFunction<TData, TResponse>;
24
- /**
25
- * React hooks that will prepare the context for the mutation onSuccess and onError
26
- * callbacks. This is useful for when you want to use the context in the callbacks
27
- */
28
- useContext?: () => TContext;
29
- onSuccess?: (queryClient: QueryClient, data: TData, variables: TVariables, context: TContext) => void | Promise<void>;
30
- onError?: (queryClient: QueryClient, err: unknown, variables: TVariables, context: TContext) => void | Promise<void>;
31
- /**
32
- * If true, we will create a mutation key that can be shared across the project.
33
- */
34
- useKey?: UseKey;
35
- keyPrefix?: UseKey extends true ? UrlHasParams<Config['url']> extends true ? string[] : never : never;
36
- keySuffix?: UseKey extends true ? UrlHasParams<Config['url']> extends true ? string[] : never : never;
37
- }
38
- type BaseQueryArgs<Config extends AnyEndpointConfig> = (UrlHasParams<Config['url']> extends true ? {
39
- urlParams: UrlParams<Config['url']>;
40
- } : {}) & (Config['querySchema'] extends AnyZodObject ? {
41
- params: z.input<Config['querySchema']>;
42
- } : {});
43
- type BaseMutationArgs<Config extends AnyEndpointConfig> = NaviosZodRequest<Config>;
44
- type InfiniteQueryOptions<Config extends BaseEndpointConfig<HttpMethod, string, AnyZodObject>, Res = any> = {
45
- keyPrefix?: string[];
46
- keySuffix?: string[];
47
- processResponse: (data: z.infer<Config['responseSchema']>) => Res;
48
- onFail?: (err: unknown) => void;
49
- getNextPageParam: (lastPage: z.infer<Config['responseSchema']>, allPages: z.infer<Config['responseSchema']>[], lastPageParam: z.infer<Config['querySchema']> | undefined, allPageParams: z.infer<Config['querySchema']>[] | undefined) => z.input<Config['querySchema']> | z.infer<Config['querySchema']> | undefined;
50
- initialPageParam?: z.input<Config['querySchema']> | z.infer<Config['querySchema']>;
51
- };
52
-
53
- type ClientMutationArgs<Url extends string = string, RequestSchema = unknown, QuerySchema = unknown> = (UrlHasParams<Url> extends true ? {
54
- urlParams: UrlParams<Url>;
55
- } : {}) & (RequestSchema extends AnyZodObject ? {
56
- data: z.input<RequestSchema>;
57
- } : {}) & (QuerySchema extends AnyZodObject ? {
58
- params: z.input<QuerySchema>;
59
- } : {});
60
-
61
- type MutationHelpers<Url extends string, Result = unknown> = UrlHasParams<Url> extends true ? {
62
- mutationKey: (params: UrlParams<Url>) => DataTag<[Url], Result, Error>;
63
- useIsMutating: (keyParams: UrlParams<Url>) => boolean;
64
- } : {
65
- mutationKey: () => DataTag<[Url], Result, Error>;
66
- useIsMutating: () => boolean;
67
- };
68
-
69
- type ClientQueryArgs<Url extends string = string, QuerySchema = AnyZodObject> = (UrlHasParams<Url> extends true ? {
70
- urlParams: UrlParams<Url>;
71
- } : {}) & (QuerySchema extends AnyZodObject ? {
72
- params: z.input<QuerySchema>;
73
- } : {});
74
-
75
- type Split$1<S extends string, D extends string> = string extends S ? string[] : S extends '' ? [] : S extends `${infer T}${D}${infer U}` ? [T, ...Split$1<U, D>] : [S];
76
- type QueryKeyCreatorResult<QuerySchema = undefined, Url extends string = string, Result = unknown, IsInfinite extends boolean = false, HasParams extends UrlHasParams<Url> = UrlHasParams<Url>> = {
77
- template: Split$1<Url, '/'>;
78
- dataTag: (params: (HasParams extends true ? {
79
- urlParams: UrlParams<Url>;
80
- } : {}) & (QuerySchema extends AnyZodObject ? {
81
- params: z.input<QuerySchema>;
82
- } : {})) => DataTag<Split$1<Url, '/'>, IsInfinite extends true ? InfiniteData<Result> : Result, Error>;
83
- filterKey: (params: HasParams extends true ? {
84
- urlParams: UrlParams<Url>;
85
- } : {}) => DataTag<Split$1<Url, '/'>, IsInfinite extends true ? InfiniteData<Result> : Result, Error>;
86
- bindToUrl: (params: (HasParams extends true ? {
87
- urlParams: UrlParams<Url>;
88
- } : {}) & (QuerySchema extends AnyZodObject ? {
89
- params: z.infer<QuerySchema>;
90
- } : {})) => string;
91
- };
92
- declare function queryKeyCreator<Config extends AnyEndpointConfig, Options extends BaseQueryParams<Config>, IsInfinite extends boolean, Url extends Config['url'] = Config['url'], HasParams extends UrlHasParams<Url> = UrlHasParams<Url>>(config: Config, options: Options, isInfinite: IsInfinite): QueryKeyCreatorResult<Config['querySchema'], Url, Options['processResponse'] extends (...args: any[]) => infer Result ? Result : never, IsInfinite, HasParams>;
93
-
94
- type ClientQueryUrlParamsArgs<Url extends string = string> = UrlHasParams<Url> extends true ? {
95
- urlParams: UrlParams<Url>;
96
- } : {} | undefined;
97
-
98
- type QueryHelpers<Url extends string, QuerySchema extends AnyZodObject | undefined = undefined, Result = undefined, IsInfinite extends boolean = false> = {
99
- queryKey: QueryKeyCreatorResult<QuerySchema, Url, Result, IsInfinite>;
100
- use: (params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>) => UseQueryResult<Result, Error>;
101
- useSuspense: (params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>) => UseSuspenseQueryResult<Result, Error>;
102
- invalidate: (queryClient: QueryClient, params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>) => () => Promise<void>;
103
- invalidateAll: (queryClient: QueryClient, params: Util_FlatObject<ClientQueryUrlParamsArgs<Url>>) => () => Promise<void>;
104
- };
105
-
106
- interface ClientInstance {
107
- query<Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET', Url extends string = string, Response extends ZodType = ZodType, Result = z.output<Response>>(config: {
108
- method: Method;
109
- url: Url;
110
- responseSchema: Response;
111
- processResponse?: (data: z.output<Response>) => Result;
112
- }): ((params: Util_FlatObject<ClientQueryArgs<Url, undefined>>) => UseSuspenseQueryOptions<Result, Error, Result, DataTag<Split$2<Url, '/'>, Result, Error>>) & QueryHelpers<Url, undefined, Result>;
113
- query<Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET', Url extends string = string, QuerySchema extends AnyZodObject = AnyZodObject, Response extends ZodType = ZodType, Result = z.output<Response>>(config: {
114
- method: Method;
115
- url: Url;
116
- querySchema: QuerySchema;
117
- responseSchema: Response;
118
- processResponse?: (data: z.output<Response>) => Result;
119
- }): ((params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>) => UseSuspenseQueryOptions<Result, Error, Result, DataTag<Split$2<Url, '/'>, Result, Error>>) & QueryHelpers<Url, QuerySchema, Result>;
120
- infiniteQuery<Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET', Url extends string = string, QuerySchema extends AnyZodObject = AnyZodObject, Response extends ZodType = ZodType, PageResult = z.output<Response>, Result = InfiniteData<PageResult>>(config: {
121
- method: Method;
122
- url: Url;
123
- querySchema: QuerySchema;
124
- responseSchema: Response;
125
- processResponse?: (data: z.output<Response>) => PageResult;
126
- getNextPageParam: (lastPage: PageResult, allPages: PageResult[], lastPageParam: z.infer<QuerySchema> | undefined, allPageParams: z.infer<QuerySchema>[] | undefined) => z.input<QuerySchema> | undefined;
127
- getPreviousPageParam?: (firstPage: PageResult, allPages: PageResult[], lastPageParam: z.infer<QuerySchema> | undefined, allPageParams: z.infer<QuerySchema>[] | undefined) => z.input<QuerySchema>;
128
- }): ((params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>) => UseSuspenseInfiniteQueryOptions<PageResult, Error, Result, PageResult, DataTag<Split$2<Url, '/'>, PageResult, Error>, z.output<QuerySchema>>) & QueryHelpers<Url, QuerySchema, PageResult, true>;
129
- mutation<Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH', Url extends string = string, RequestSchema extends ZodType = ZodType, QuerySchema extends AnyZodObject = AnyZodObject, Response extends ZodType = ZodType, ReqResult = z.output<Response>, Result = unknown, Context = unknown, UseKey extends true = true>(config: {
130
- method: Method;
131
- url: Url;
132
- useKey: UseKey;
133
- requestSchema: RequestSchema;
134
- querySchema: QuerySchema;
135
- responseSchema: Response;
136
- processResponse: ProcessResponseFunction<Result, ReqResult>;
137
- useContext?: () => Context;
138
- onSuccess?: (queryClient: QueryClient, data: NoInfer<Result>, variables: Util_FlatObject<ClientMutationArgs<Url, RequestSchema, QuerySchema>>, context: Context) => void | Promise<void>;
139
- onError?: (queryClient: QueryClient, error: Error, variables: Util_FlatObject<ClientMutationArgs<Url, RequestSchema, QuerySchema>>, context: Context) => void | Promise<void>;
140
- }): ((params: UrlHasParams<Url> extends true ? {
141
- urlParams: UrlParams<Url>;
142
- } : {}) => UseMutationResult<Result, Error, ClientMutationArgs<Url, RequestSchema, QuerySchema>>) & MutationHelpers<Url, Result>;
143
- mutation<Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH', Url extends string = string, RequestSchema extends ZodType = ZodType, QuerySchema extends AnyZodObject = AnyZodObject, Response extends ZodType = ZodType, ReqResult = z.output<Response>, Result = unknown, Context = unknown>(config: {
144
- method: Method;
145
- url: Url;
146
- requestSchema: RequestSchema;
147
- querySchema: QuerySchema;
148
- responseSchema: Response;
149
- processResponse: ProcessResponseFunction<Result, ReqResult>;
150
- useContext?: () => Context;
151
- onSuccess?: (queryClient: QueryClient, data: NoInfer<Result>, variables: Util_FlatObject<ClientMutationArgs<Url, RequestSchema, QuerySchema>>, context: Context) => void | Promise<void>;
152
- onError?: (queryClient: QueryClient, error: Error, variables: Util_FlatObject<ClientMutationArgs<Url, RequestSchema, QuerySchema>>, context: Context) => void | Promise<void>;
153
- }): (params: UrlHasParams<Url> extends true ? {
154
- urlParams: UrlParams<Url>;
155
- } : {}) => UseMutationResult<Result, Error, ClientMutationArgs<Url, RequestSchema, QuerySchema>>;
156
- mutation<Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH', Url extends string = string, RequestSchema extends ZodType = ZodType, Response extends ZodType = ZodType, ReqResult = z.output<Response>, Result = unknown, Context = unknown>(config: {
157
- method: Method;
158
- url: Url;
159
- requestSchema: RequestSchema;
160
- responseSchema: Response;
161
- processResponse: ProcessResponseFunction<Result, ReqResult>;
162
- useContext?: () => Context;
163
- onSuccess?: (queryClient: QueryClient, data: NoInfer<Result>, variables: Util_FlatObject<ClientMutationArgs<Url, RequestSchema, undefined>>, context: Context) => void | Promise<void>;
164
- onError?: (queryClient: QueryClient, error: Error, variables: Util_FlatObject<ClientMutationArgs<Url, RequestSchema, undefined>>, context: Context) => void | Promise<void>;
165
- }): ((params: UrlHasParams<Url> extends true ? {
166
- urlParams: UrlParams<Url>;
167
- } : {}) => UseMutationResult<Result, Error, ClientMutationArgs<Url, RequestSchema, undefined>>) & MutationHelpers<Url, Result>;
168
- mutation<Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH', Url extends string = string, RequestSchema extends ZodType = ZodType, Response extends ZodType = ZodType, ReqResult = z.output<Response>, Result = unknown, Context = unknown, UseKey extends true = true>(config: {
169
- method: Method;
170
- url: Url;
171
- useKey: UseKey;
172
- requestSchema: RequestSchema;
173
- responseSchema: Response;
174
- processResponse: ProcessResponseFunction<Result, ReqResult>;
175
- useContext?: () => Context;
176
- onSuccess?: (queryClient: QueryClient, data: NoInfer<Result>, variables: Util_FlatObject<ClientMutationArgs<Url, RequestSchema, undefined>>, context: Context) => void | Promise<void>;
177
- onError?: (queryClient: QueryClient, error: Error, variables: Util_FlatObject<ClientMutationArgs<Url, RequestSchema, undefined>>, context: Context) => void | Promise<void>;
178
- }): ((params: UrlHasParams<Url> extends true ? {
179
- urlParams: UrlParams<Url>;
180
- } : {}) => UseMutationResult<Result, Error, ClientMutationArgs<Url, RequestSchema, undefined>>) & MutationHelpers<Url, Result>;
181
- mutation<Method extends 'DELETE' = 'DELETE', Url extends string = string, QuerySchema extends AnyZodObject = AnyZodObject, Response extends ZodType = ZodType, ReqResult = z.output<Response>, Result = unknown, Context = unknown, UseKey extends true = true>(config: {
182
- method: Method;
183
- url: Url;
184
- useKey: UseKey;
185
- querySchema: QuerySchema;
186
- responseSchema: Response;
187
- processResponse: ProcessResponseFunction<Result, ReqResult>;
188
- useContext?: () => Context;
189
- onSuccess?: (queryClient: QueryClient, data: NoInfer<Result>, variables: Util_FlatObject<ClientMutationArgs<Url, undefined, QuerySchema>>, context: Context) => void | Promise<void>;
190
- onError?: (queryClient: QueryClient, error: Error, variables: Util_FlatObject<ClientMutationArgs<Url, undefined, QuerySchema>>, context: Context) => void | Promise<void>;
191
- }): ((params: UrlHasParams<Url> extends true ? {
192
- urlParams: UrlParams<Url>;
193
- } : {}) => UseMutationResult<Result, Error, ClientMutationArgs<Url, undefined, QuerySchema>>) & MutationHelpers<Url, Result>;
194
- mutation<Method extends 'DELETE' = 'DELETE', Url extends string = string, QuerySchema extends AnyZodObject = AnyZodObject, Response extends ZodType = ZodType, ReqResult = z.output<Response>, Result = unknown, Context = unknown>(config: {
195
- method: Method;
196
- url: Url;
197
- querySchema: QuerySchema;
198
- responseSchema: Response;
199
- processResponse: ProcessResponseFunction<Result, ReqResult>;
200
- useContext?: () => Context;
201
- onSuccess?: (queryClient: QueryClient, data: NoInfer<Result>, variables: Util_FlatObject<ClientMutationArgs<Url, undefined, QuerySchema>>, context: Context) => void | Promise<void>;
202
- onError?: (queryClient: QueryClient, error: Error, variables: Util_FlatObject<ClientMutationArgs<Url, undefined, QuerySchema>>, context: Context) => void | Promise<void>;
203
- }): (params: UrlHasParams<Url> extends true ? {
204
- urlParams: UrlParams<Url>;
205
- } : {}) => UseMutationResult<Result, Error, ClientMutationArgs<Url, undefined, QuerySchema>>;
206
- mutation<Method extends 'DELETE' = 'DELETE', Url extends string = string, Response extends ZodType = ZodType, ReqResult = z.output<Response>, Result = unknown, Context = unknown, UseKey extends true = true>(config: {
207
- method: Method;
208
- url: Url;
209
- useKey: UseKey;
210
- responseSchema: Response;
211
- processResponse: ProcessResponseFunction<Result, ReqResult>;
212
- useContext?: () => Context;
213
- onSuccess?: (queryClient: QueryClient, data: NoInfer<Result>, variables: Util_FlatObject<ClientMutationArgs<Url, undefined, undefined>>, context: Context) => void | Promise<void>;
214
- onError?: (queryClient: QueryClient, error: Error, variables: Util_FlatObject<ClientMutationArgs<Url, undefined, undefined>>, context: Context) => void | Promise<void>;
215
- }): ((params: UrlHasParams<Url> extends true ? {
216
- urlParams: UrlParams<Url>;
217
- } : {}) => UseMutationResult<Result, Error, ClientMutationArgs<Url, undefined, undefined>>) & MutationHelpers<Url, Result>;
218
- mutation<Method extends 'DELETE' = 'DELETE', Url extends string = string, Response extends ZodType = ZodType, ReqResult = z.output<Response>, Result = unknown, Context = unknown>(config: {
219
- method: Method;
220
- url: Url;
221
- responseSchema: Response;
222
- processResponse: ProcessResponseFunction<Result, ReqResult>;
223
- useContext?: () => Context;
224
- onSuccess?: (queryClient: QueryClient, data: NoInfer<Result>, variables: Util_FlatObject<ClientMutationArgs<Url, undefined, undefined>>, context: Context) => void | Promise<void>;
225
- onError?: (queryClient: QueryClient, error: Error, variables: Util_FlatObject<ClientMutationArgs<Url, undefined, undefined>>, context: Context) => void | Promise<void>;
226
- }): (params: UrlHasParams<Url> extends true ? {
227
- urlParams: UrlParams<Url>;
228
- } : {}) => UseMutationResult<Result, Error, ClientMutationArgs<Url, undefined, undefined>>;
229
- queryFromEndpoint<Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET', Url extends string = string, QuerySchema extends AnyZodObject = AnyZodObject, Response extends ZodType = ZodType, Result = z.output<Response>>(endpoint: any & {
230
- config: BaseEndpointConfig<Method, Url, QuerySchema, Response>;
231
- }, options?: {
232
- processResponse?: (data: z.output<Response>) => Result;
233
- }): (params: UrlHasParams<Url> extends true ? {
234
- urlParams: UrlParams<Url>;
235
- } : {}) => UseSuspenseQueryOptions<Result, Error, Result, DataTag<Split$2<Url, '/'>, Result, Error>> & QueryHelpers<Url, QuerySchema, Result>;
236
- queryFromEndpoint<Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET', Url extends string = string, Response extends ZodType = ZodType, Result = z.output<Response>>(endpoint: any & {
237
- config: BaseEndpointConfig<Method, Url, undefined, Response>;
238
- }, options?: {
239
- processResponse?: (data: z.output<Response>) => Result;
240
- }): (params: UrlHasParams<Url> extends true ? {
241
- urlParams: UrlParams<Url>;
242
- } : {}) => UseSuspenseQueryOptions<Result, Error, Result, DataTag<Split$2<Url, '/'>, Result, Error>> & QueryHelpers<Url, undefined, Result>;
243
- infiniteQueryFromEndpoint<Method extends 'GET' | 'HEAD' | 'OPTIONS' = 'GET', Url extends string = string, QuerySchema extends AnyZodObject = AnyZodObject, Response extends ZodType = ZodType, PageResult = z.output<Response>, Result = InfiniteData<PageResult>>(endpoint: any & {
244
- config: BaseEndpointConfig<Method, Url, QuerySchema, Response>;
245
- }, options: {
246
- processResponse?: (data: z.output<Response>) => PageResult;
247
- getNextPageParam: (lastPage: PageResult, allPages: PageResult[], lastPageParam: z.infer<QuerySchema> | undefined, allPageParams: z.infer<QuerySchema>[] | undefined) => z.input<QuerySchema> | undefined;
248
- getPreviousPageParam?: (firstPage: PageResult, allPages: PageResult[], lastPageParam: z.infer<QuerySchema> | undefined, allPageParams: z.infer<QuerySchema>[] | undefined) => z.input<QuerySchema>;
249
- }): (params: UrlHasParams<Url> extends true ? {
250
- urlParams: UrlParams<Url>;
251
- } : {}) => UseSuspenseInfiniteQueryOptions<PageResult, Error, Result, PageResult, DataTag<Split$2<Url, '/'>, PageResult, Error>, z.output<QuerySchema>> & QueryHelpers<Url, QuerySchema, PageResult, true>;
252
- }
253
-
254
- /**
255
- * Creates a mutation key for a given endpoint configuration and options.
256
- *
257
- * @param {config: Config } config - The endpoint object containing the configuration.
258
- * @param {Options} [options] - Optional query parameters with a default `processResponse` function that processes the response data.
259
- *
260
- * @returns {Object} An object containing the `mutationKey` function.
261
- *
262
- * The `mutationKey` function generates a mutation key based on the provided parameters:
263
- * - If the URL has parameters (`HasParams` is `true`), it expects an object with `urlParams`.
264
- * - The return type of the `mutationKey` function depends on the `processResponse` function in `options`.
265
- * If `processResponse` is defined, the return type is a `DataTag` containing the processed result and an error type.
266
- *
267
- * @example Example usage:
268
- * ```typescript
269
- * const createMutationKey = mutationKeyCreator(endpoint.config);
270
- * const mutationKey = createMutationKey({ urlParams: { id: 123 } });
271
- * ```
272
- *
273
- * @example Advanced usage:
274
- * ```ts
275
- * const createMutationKey = mutationKeyCreator(endpoint.config, {
276
- * processResponse: (data) => {
277
- * if (!data.success) {
278
- * throw new Error(data.message);
279
- * }
280
- * return data.data;
281
- * },
282
- * });
283
- * // We create a mutation that will be shared across the project for all passed userId
284
- * const mutationKey = createMutationKey({ urlParams: { projectId: 123, userId: 'wildcard' } });
285
- */
286
- declare function mutationKeyCreator<Config extends AnyEndpointConfig, Options extends BaseQueryParams<Config>, Url extends Config['url'] = Config['url'], HasParams extends UrlHasParams<Url> = UrlHasParams<Url>>(config: Config, options?: Options): (params: HasParams extends true ? {
287
- urlParams: UrlParams<Url>;
288
- } : {}) => Options['processResponse'] extends (...args: any[]) => infer Result ? DataTag<[Config['url']], Result, Error> : never;
289
-
290
- interface ClientEndpoint<Method = HttpMethod, Url = string, QuerySchema = unknown, Response = ZodType> {
291
- method: Method;
292
- url: Url;
293
- querySchema?: QuerySchema;
294
- responseSchema: Response;
295
- }
296
- interface ClientQueryConfig<Method = HttpMethod, Url = string, QuerySchema = AnyZodObject, Response extends ZodType = ZodType, Result = z.output<Response>> extends ClientEndpoint<Method, Url, QuerySchema, Response> {
297
- processResponse?: (data: z.output<Response>) => Result;
298
- }
299
- type ClientInfiniteQueryConfig<Method = HttpMethod, Url = string, QuerySchema extends AnyZodObject = AnyZodObject, Response extends ZodType = ZodType, PageResult = z.output<Response>, Result = InfiniteData<PageResult>> = Required<ClientEndpoint<Method, Url, QuerySchema, Response>> & {
300
- processResponse?: (data: z.output<Response>) => PageResult;
301
- select?: (data: InfiniteData<PageResult>) => Result;
302
- getNextPageParam: (lastPage: PageResult, allPages: PageResult[], lastPageParam: z.infer<QuerySchema> | undefined, allPageParams: z.infer<QuerySchema>[] | undefined) => z.input<QuerySchema> | undefined;
303
- getPreviousPageParam?: (firstPage: PageResult, allPages: PageResult[], lastPageParam: z.infer<QuerySchema> | undefined, allPageParams: z.infer<QuerySchema>[] | undefined) => z.input<QuerySchema>;
304
- initialPageParam?: z.input<QuerySchema>;
305
- };
306
- interface ClientMutationDataConfig<Method extends 'POST' | 'PUT' | 'PATCH' | 'DELETE' = 'POST' | 'PUT' | 'PATCH' | 'DELETE', Url extends string = string, RequestSchema = Method extends 'DELETE' ? never : AnyZodObject, QuerySchema = unknown, Response extends ZodType = ZodType, ReqResult = z.output<Response>, Result = unknown, Context = unknown, UseKey extends boolean = false> extends ClientEndpoint<Method, Url, QuerySchema, Response> {
307
- requestSchema?: RequestSchema;
308
- processResponse: ProcessResponseFunction<Result, ReqResult>;
309
- useContext?: () => Context;
310
- onSuccess?: (queryClient: QueryClient, data: NoInfer<Result>, variables: Util_FlatObject<ClientMutationArgs<Url, RequestSchema, QuerySchema>>, context: Context) => void | Promise<void>;
311
- onError?: (queryClient: QueryClient, error: Error, variables: Util_FlatObject<ClientMutationArgs<Url, RequestSchema, QuerySchema>>, context: Context) => void | Promise<void>;
312
- useKey?: UseKey;
313
- }
314
- declare function declareClient<Options extends ClientOptions>({ api, defaults, }: Options): ClientInstance;
315
-
316
- declare function makeMutation<Config extends AnyEndpointConfig, TData = unknown, TVariables extends BaseMutationArgs<Config> = BaseMutationArgs<Config>, TResponse = z.output<Config['responseSchema']>, TContext = unknown, UseKey extends boolean = false>(endpoint: AbstractEndpoint<Config>, options: BaseMutationParams<Config, TData, TVariables, TResponse, TContext, UseKey>): {
317
- (keyParams: UseKey extends true ? UrlHasParams<Config["url"]> extends true ? UrlParams<Config["url"]> : never : never): UseMutationResult<TData, Error, BaseMutationArgs<Config>>;
318
- useIsMutating(keyParams: UseKey extends true ? UrlHasParams<Config["url"]> extends true ? UrlParams<Config["url"]> : never : never): boolean;
319
- mutationKey: (params: UrlHasParams<Config["url"]> extends infer T ? T extends UrlHasParams<Config["url"]> ? T extends true ? {
320
- urlParams: UrlParams<Config["url"]>;
321
- } : {} : never : never) => [Config["url"]] & {
322
- [dataTagSymbol]: TData | Promise<TData>;
323
- [dataTagErrorSymbol]: Error;
324
- };
325
- };
326
-
327
- declare function makeInfiniteQueryOptions<Config extends AnyEndpointConfig, Options extends InfiniteQueryOptions<Config>, BaseQuery extends Omit<UseInfiniteQueryOptions<ReturnType<Options['processResponse']>, Error, any>, 'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam' | 'placeholderData' | 'throwOnError'>>(endpoint: AbstractEndpoint<Config>, options: Options, baseQuery?: BaseQuery): {
328
- (params: ClientQueryArgs): Options["processResponse"] extends (...args: any[]) => infer Result ? UseSuspenseInfiniteQueryOptions<Result, Error, BaseQuery["select"] extends (...args: any[]) => infer T ? T : InfiniteData<Result>> : never;
329
- queryKey: QueryKeyCreatorResult<Config["querySchema"], Config["url"], Options["processResponse"] extends (...args: any[]) => infer Result ? Result : never, true, _navios_common.UrlHasParams<Config["url"]>>;
330
- use(params: ClientQueryArgs): _tanstack_react_query.UseInfiniteQueryResult<BaseQuery["select"] extends (...args: any[]) => infer T ? T : InfiniteData<unknown, unknown>, Error>;
331
- useSuspense(params: ClientQueryArgs): _tanstack_react_query.UseSuspenseInfiniteQueryResult<BaseQuery["select"] extends (...args: any[]) => infer T ? T : InfiniteData<unknown, unknown>, Error>;
332
- invalidate(queryClient: QueryClient, params: ClientQueryArgs): Promise<void>;
333
- invalidateAll(queryClient: QueryClient, params: ClientQueryArgs): Promise<void>;
334
- };
335
-
336
- type Split<S extends string, D extends string> = string extends S ? string[] : S extends '' ? [] : S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S];
337
- declare function makeQueryOptions<Config extends AnyEndpointConfig, Options extends BaseQueryParams<Config>, BaseQuery extends Omit<UseQueryOptions<ReturnType<Options['processResponse']>, Error, any>, 'queryKey' | 'queryFn' | 'getNextPageParam' | 'initialPageParam' | 'enabled' | 'throwOnError' | 'placeholderData'>>(endpoint: AbstractEndpoint<Config>, options: Options, baseQuery?: BaseQuery): {
338
- (params: BaseQueryArgs<Config>): Options["processResponse"] extends (...args: any[]) => infer Result ? UseSuspenseQueryOptions<Result, Error, BaseQuery["select"] extends (...args: any[]) => infer T ? T : Result, DataTag<Split<Config["url"], "/">, Result, Error>> : never;
339
- queryKey: QueryKeyCreatorResult<Config["querySchema"], Config["url"], Options["processResponse"] extends (...args: any[]) => infer Result ? Result : never, false, _navios_common.UrlHasParams<Config["url"]>>;
340
- use(params: ClientQueryArgs): _tanstack_react_query.DefinedUseInfiniteQueryResult<BaseQuery["select"] extends (...args: any[]) => infer T ? T : unknown, Error>;
341
- useSuspense(params: ClientQueryArgs): _tanstack_react_query.UseSuspenseInfiniteQueryResult<BaseQuery["select"] extends (...args: any[]) => infer T ? T : unknown, Error>;
342
- invalidate(queryClient: QueryClient, params: ClientQueryArgs): Promise<void>;
343
- invalidateAll(queryClient: QueryClient, params: ClientQueryArgs): Promise<void>;
344
- };
345
-
346
- export { type BaseMutationArgs, type BaseMutationParams, type BaseQueryArgs, type BaseQueryParams, type ClientEndpoint, type ClientInfiniteQueryConfig, type ClientInstance, type ClientMutationArgs, type ClientMutationDataConfig, type ClientOptions, type ClientQueryArgs, type ClientQueryConfig, type ClientQueryUrlParamsArgs, type InfiniteQueryOptions, type MutationHelpers, type ProcessResponseFunction, type QueryHelpers, type QueryKeyCreatorResult, type Split$2 as Split, declareClient, makeInfiniteQueryOptions, makeMutation, makeQueryOptions, mutationKeyCreator, queryKeyCreator };
1
+ export { ClientEndpointHelper } from './_tsup-dts-rollup.js';
2
+ export { ClientInstance } from './_tsup-dts-rollup.js';
3
+ export { ClientMutationArgs } from './_tsup-dts-rollup.js';
4
+ export { MutationHelpers } from './_tsup-dts-rollup.js';
5
+ export { ClientQueryArgs } from './_tsup-dts-rollup.js';
6
+ export { QueryHelpers } from './_tsup-dts-rollup.js';
7
+ export { ClientQueryUrlParamsArgs } from './_tsup-dts-rollup.js';
8
+ export { mutationKeyCreator } from './_tsup-dts-rollup.js';
9
+ export { queryKeyCreator } from './_tsup-dts-rollup.js';
10
+ export { QueryKeyCreatorResult } from './_tsup-dts-rollup.js';
11
+ export { declareClient_alias_1 as declareClient } from './_tsup-dts-rollup.js';
12
+ export { ClientEndpointDefinition_alias_1 as ClientEndpointDefinition } from './_tsup-dts-rollup.js';
13
+ export { ClientQueryConfig_alias_1 as ClientQueryConfig } from './_tsup-dts-rollup.js';
14
+ export { ClientInfiniteQueryConfig_alias_1 as ClientInfiniteQueryConfig } from './_tsup-dts-rollup.js';
15
+ export { ClientMutationDataConfig_alias_1 as ClientMutationDataConfig } from './_tsup-dts-rollup.js';
16
+ export { makeMutation } from './_tsup-dts-rollup.js';
17
+ export { makeInfiniteQueryOptions } from './_tsup-dts-rollup.js';
18
+ export { makeQueryOptions } from './_tsup-dts-rollup.js';
19
+ export { Split } from './_tsup-dts-rollup.js';
20
+ export { ProcessResponseFunction } from './_tsup-dts-rollup.js';
21
+ export { ClientOptions } from './_tsup-dts-rollup.js';
22
+ export { BaseQueryParams } from './_tsup-dts-rollup.js';
23
+ export { BaseMutationParams } from './_tsup-dts-rollup.js';
24
+ export { BaseQueryArgs } from './_tsup-dts-rollup.js';
25
+ export { BaseMutationArgs } from './_tsup-dts-rollup.js';
26
+ export { InfiniteQueryOptions } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -230,10 +230,10 @@ function makeQueryOptions(endpoint, options, baseQuery = {}) {
230
230
  };
231
231
  result.queryKey = queryKey;
232
232
  result.use = (params) => {
233
- return (0, import_react_query3.useInfiniteQuery)(result(params));
233
+ return (0, import_react_query3.useQuery)(result(params));
234
234
  };
235
235
  result.useSuspense = (params) => {
236
- return (0, import_react_query3.useSuspenseInfiniteQuery)(result(params));
236
+ return (0, import_react_query3.useSuspenseQuery)(result(params));
237
237
  };
238
238
  result.invalidate = (queryClient, params) => {
239
239
  return queryClient.invalidateQueries({
@@ -264,10 +264,12 @@ function declareClient({
264
264
  querySchema: config.querySchema,
265
265
  responseSchema: config.responseSchema
266
266
  });
267
- return makeQueryOptions(endpoint, {
267
+ const queryOptions2 = makeQueryOptions(endpoint, {
268
268
  ...defaults,
269
269
  processResponse: config.processResponse ?? ((data) => data)
270
270
  });
271
+ queryOptions2.endpoint = endpoint;
272
+ return queryOptions2;
271
273
  }
272
274
  function queryFromEndpoint(endpoint, options) {
273
275
  return makeQueryOptions(endpoint, {
@@ -283,13 +285,15 @@ function declareClient({
283
285
  querySchema: config.querySchema,
284
286
  responseSchema: config.responseSchema
285
287
  });
286
- return makeInfiniteQueryOptions(endpoint, {
288
+ const infiniteQueryOptions2 = makeInfiniteQueryOptions(endpoint, {
287
289
  ...defaults,
288
290
  processResponse: config.processResponse ?? ((data) => data),
289
291
  getNextPageParam: config.getNextPageParam,
290
292
  getPreviousPageParam: config.getPreviousPageParam,
291
293
  initialPageParam: config.initialPageParam
292
294
  });
295
+ infiniteQueryOptions2.endpoint = endpoint;
296
+ return infiniteQueryOptions2;
293
297
  }
294
298
  function infiniteQueryFromEndpoint(endpoint, options) {
295
299
  return makeInfiniteQueryOptions(endpoint, {
@@ -309,7 +313,7 @@ function declareClient({
309
313
  requestSchema: config.requestSchema,
310
314
  responseSchema: config.responseSchema
311
315
  });
312
- return makeMutation(endpoint, {
316
+ const useMutation2 = makeMutation(endpoint, {
313
317
  processResponse: config.processResponse ?? ((data) => data),
314
318
  useContext: config.useContext,
315
319
  // @ts-expect-error We forgot about the DELETE method in original makeMutation
@@ -319,6 +323,8 @@ function declareClient({
319
323
  useKey: config.useKey,
320
324
  ...defaults
321
325
  });
326
+ useMutation2.endpoint = endpoint;
327
+ return useMutation2;
322
328
  }
323
329
  return {
324
330
  // @ts-expect-error We simplified types here
@@ -342,4 +348,3 @@ function declareClient({
342
348
  mutationKeyCreator,
343
349
  queryKeyCreator
344
350
  });
345
- //# sourceMappingURL=index.js.map
package/dist/index.mjs CHANGED
@@ -179,11 +179,7 @@ function makeMutation(endpoint, options) {
179
179
  }
180
180
 
181
181
  // packages/react-query/src/make-query-options.mts
182
- import {
183
- queryOptions,
184
- useInfiniteQuery as useInfiniteQuery2,
185
- useSuspenseInfiniteQuery as useSuspenseInfiniteQuery2
186
- } from "@tanstack/react-query";
182
+ import { queryOptions, useQuery, useSuspenseQuery } from "@tanstack/react-query";
187
183
  function makeQueryOptions(endpoint, options, baseQuery = {}) {
188
184
  const config = endpoint.config;
189
185
  const queryKey = queryKeyCreator(config, options, false);
@@ -211,10 +207,10 @@ function makeQueryOptions(endpoint, options, baseQuery = {}) {
211
207
  };
212
208
  result.queryKey = queryKey;
213
209
  result.use = (params) => {
214
- return useInfiniteQuery2(result(params));
210
+ return useQuery(result(params));
215
211
  };
216
212
  result.useSuspense = (params) => {
217
- return useSuspenseInfiniteQuery2(result(params));
213
+ return useSuspenseQuery(result(params));
218
214
  };
219
215
  result.invalidate = (queryClient, params) => {
220
216
  return queryClient.invalidateQueries({
@@ -245,10 +241,12 @@ function declareClient({
245
241
  querySchema: config.querySchema,
246
242
  responseSchema: config.responseSchema
247
243
  });
248
- return makeQueryOptions(endpoint, {
244
+ const queryOptions2 = makeQueryOptions(endpoint, {
249
245
  ...defaults,
250
246
  processResponse: config.processResponse ?? ((data) => data)
251
247
  });
248
+ queryOptions2.endpoint = endpoint;
249
+ return queryOptions2;
252
250
  }
253
251
  function queryFromEndpoint(endpoint, options) {
254
252
  return makeQueryOptions(endpoint, {
@@ -264,13 +262,15 @@ function declareClient({
264
262
  querySchema: config.querySchema,
265
263
  responseSchema: config.responseSchema
266
264
  });
267
- return makeInfiniteQueryOptions(endpoint, {
265
+ const infiniteQueryOptions2 = makeInfiniteQueryOptions(endpoint, {
268
266
  ...defaults,
269
267
  processResponse: config.processResponse ?? ((data) => data),
270
268
  getNextPageParam: config.getNextPageParam,
271
269
  getPreviousPageParam: config.getPreviousPageParam,
272
270
  initialPageParam: config.initialPageParam
273
271
  });
272
+ infiniteQueryOptions2.endpoint = endpoint;
273
+ return infiniteQueryOptions2;
274
274
  }
275
275
  function infiniteQueryFromEndpoint(endpoint, options) {
276
276
  return makeInfiniteQueryOptions(endpoint, {
@@ -290,7 +290,7 @@ function declareClient({
290
290
  requestSchema: config.requestSchema,
291
291
  responseSchema: config.responseSchema
292
292
  });
293
- return makeMutation(endpoint, {
293
+ const useMutation2 = makeMutation(endpoint, {
294
294
  processResponse: config.processResponse ?? ((data) => data),
295
295
  useContext: config.useContext,
296
296
  // @ts-expect-error We forgot about the DELETE method in original makeMutation
@@ -300,6 +300,8 @@ function declareClient({
300
300
  useKey: config.useKey,
301
301
  ...defaults
302
302
  });
303
+ useMutation2.endpoint = endpoint;
304
+ return useMutation2;
303
305
  }
304
306
  return {
305
307
  // @ts-expect-error We simplified types here
@@ -322,4 +324,3 @@ export {
322
324
  mutationKeyCreator,
323
325
  queryKeyCreator
324
326
  };
325
- //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navios/react-query",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "author": {
5
5
  "name": "Oleksandr Hanzha",
6
6
  "email": "alex@granted.name"
@@ -15,7 +15,7 @@
15
15
  "main": "./dist/index.js",
16
16
  "module": "./dist/index.mjs",
17
17
  "peerDependencies": {
18
- "@navios/common": "^0.1.0",
18
+ "@navios/common": "^0.1.3",
19
19
  "@tanstack/react-query": "^5.51.21",
20
20
  "zod": "^3.23.8"
21
21
  },
@@ -33,11 +33,11 @@
33
33
  "./package.json": "./package.json"
34
34
  },
35
35
  "devDependencies": {
36
- "@navios/common": "^0.1.0",
37
- "@tanstack/react-query": "^5.75.1",
36
+ "@navios/common": "^0.1.3",
37
+ "@tanstack/react-query": "^5.75.7",
38
38
  "navios": "^0.4.0",
39
39
  "react": "^19.1.0",
40
40
  "typescript": "^5.8.3",
41
- "zod": "^3.24.3"
41
+ "zod": "^3.24.4"
42
42
  }
43
43
  }