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