@navios/react-query 0.7.1 → 1.0.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 (117) hide show
  1. package/CHANGELOG.md +171 -1
  2. package/README.md +152 -4
  3. package/coverage/base.css +224 -0
  4. package/coverage/block-navigation.js +87 -0
  5. package/coverage/client/declare-client.mts.html +1264 -0
  6. package/coverage/client/index.html +116 -0
  7. package/coverage/clover.xml +160 -0
  8. package/coverage/coverage-final.json +8 -0
  9. package/coverage/favicon.png +0 -0
  10. package/coverage/index.html +146 -0
  11. package/coverage/mutation/index.html +131 -0
  12. package/coverage/mutation/key-creator.mts.html +277 -0
  13. package/coverage/mutation/make-hook.mts.html +952 -0
  14. package/coverage/prettify.css +1 -0
  15. package/coverage/prettify.js +2 -0
  16. package/coverage/query/index.html +161 -0
  17. package/coverage/query/key-creator.mts.html +415 -0
  18. package/coverage/query/make-infinite-options.mts.html +601 -0
  19. package/coverage/query/make-options.mts.html +838 -0
  20. package/coverage/query/prefetch.mts.html +1063 -0
  21. package/coverage/sort-arrow-sprite.png +0 -0
  22. package/coverage/sorter.js +210 -0
  23. package/dist/src/__tests__/errorSchema.spec.d.mts +2 -0
  24. package/dist/src/__tests__/errorSchema.spec.d.mts.map +1 -0
  25. package/dist/src/__tests__/prefetch.spec.d.mts +2 -0
  26. package/dist/src/__tests__/prefetch.spec.d.mts.map +1 -0
  27. package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts +2 -0
  28. package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts.map +1 -0
  29. package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts +2 -0
  30. package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts.map +1 -0
  31. package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts +2 -0
  32. package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts.map +1 -0
  33. package/dist/src/client/__type-tests__/mutation.spec-d.d.mts +2 -0
  34. package/dist/src/client/__type-tests__/mutation.spec-d.d.mts.map +1 -0
  35. package/dist/src/client/__type-tests__/query.spec-d.d.mts +2 -0
  36. package/dist/src/client/__type-tests__/query.spec-d.d.mts.map +1 -0
  37. package/dist/src/client/declare-client.d.mts +15 -8
  38. package/dist/src/client/declare-client.d.mts.map +1 -1
  39. package/dist/src/client/types/from-endpoint.d.mts +130 -0
  40. package/dist/src/client/types/from-endpoint.d.mts.map +1 -0
  41. package/dist/src/client/types/helpers.d.mts +74 -0
  42. package/dist/src/client/types/helpers.d.mts.map +1 -0
  43. package/dist/src/client/types/index.d.mts +21 -0
  44. package/dist/src/client/types/index.d.mts.map +1 -0
  45. package/dist/src/client/types/infinite-query.d.mts +61 -0
  46. package/dist/src/client/types/infinite-query.d.mts.map +1 -0
  47. package/dist/src/client/types/multipart-mutation.d.mts +98 -0
  48. package/dist/src/client/types/multipart-mutation.d.mts.map +1 -0
  49. package/dist/src/client/types/mutation.d.mts +75 -0
  50. package/dist/src/client/types/mutation.d.mts.map +1 -0
  51. package/dist/src/client/types/query.d.mts +65 -0
  52. package/dist/src/client/types/query.d.mts.map +1 -0
  53. package/dist/src/client/types.d.mts +1 -608
  54. package/dist/src/client/types.d.mts.map +1 -1
  55. package/dist/src/common/types.d.mts +40 -3
  56. package/dist/src/common/types.d.mts.map +1 -1
  57. package/dist/src/mutation/index.d.mts +1 -0
  58. package/dist/src/mutation/index.d.mts.map +1 -1
  59. package/dist/src/mutation/make-hook.d.mts +42 -16
  60. package/dist/src/mutation/make-hook.d.mts.map +1 -1
  61. package/dist/src/mutation/optimistic.d.mts +172 -0
  62. package/dist/src/mutation/optimistic.d.mts.map +1 -0
  63. package/dist/src/mutation/types.d.mts +41 -20
  64. package/dist/src/mutation/types.d.mts.map +1 -1
  65. package/dist/src/query/index.d.mts +1 -0
  66. package/dist/src/query/index.d.mts.map +1 -1
  67. package/dist/src/query/key-creator.d.mts.map +1 -1
  68. package/dist/src/query/make-infinite-options.d.mts +3 -2
  69. package/dist/src/query/make-infinite-options.d.mts.map +1 -1
  70. package/dist/src/query/make-options.d.mts +42 -12
  71. package/dist/src/query/make-options.d.mts.map +1 -1
  72. package/dist/src/query/prefetch.d.mts +245 -0
  73. package/dist/src/query/prefetch.d.mts.map +1 -0
  74. package/dist/src/query/types.d.mts +25 -18
  75. package/dist/src/query/types.d.mts.map +1 -1
  76. package/dist/tsconfig.tsbuildinfo +1 -1
  77. package/lib/index.cjs +451 -28
  78. package/lib/index.cjs.map +1 -1
  79. package/lib/index.d.cts +1019 -600
  80. package/lib/index.d.cts.map +1 -1
  81. package/lib/index.d.mts +1016 -597
  82. package/lib/index.d.mts.map +1 -1
  83. package/lib/index.mjs +447 -29
  84. package/lib/index.mjs.map +1 -1
  85. package/package.json +3 -3
  86. package/src/__tests__/declare-client.spec.mts +229 -2
  87. package/src/__tests__/errorSchema.spec.mts +391 -0
  88. package/src/__tests__/make-mutation.spec.mts +6 -5
  89. package/src/__tests__/makeDataTag.spec.mts +2 -1
  90. package/src/__tests__/makeQueryOptions.spec.mts +2 -1
  91. package/src/__tests__/prefetch.spec.mts +310 -0
  92. package/src/client/__type-tests__/from-endpoint.spec-d.mts +550 -0
  93. package/src/client/__type-tests__/infinite-query.spec-d.mts +648 -0
  94. package/src/client/__type-tests__/multipart-mutation.spec-d.mts +725 -0
  95. package/src/client/__type-tests__/mutation.spec-d.mts +757 -0
  96. package/src/client/__type-tests__/query.spec-d.mts +701 -0
  97. package/src/client/declare-client.mts +59 -34
  98. package/src/client/types/from-endpoint.mts +344 -0
  99. package/src/client/types/helpers.mts +140 -0
  100. package/src/client/types/index.mts +26 -0
  101. package/src/client/types/infinite-query.mts +133 -0
  102. package/src/client/types/multipart-mutation.mts +264 -0
  103. package/src/client/types/mutation.mts +176 -0
  104. package/src/client/types/query.mts +132 -0
  105. package/src/client/types.mts +1 -1935
  106. package/src/common/types.mts +67 -3
  107. package/src/mutation/index.mts +1 -0
  108. package/src/mutation/make-hook.mts +171 -63
  109. package/src/mutation/optimistic.mts +300 -0
  110. package/src/mutation/types.mts +87 -30
  111. package/src/query/index.mts +1 -0
  112. package/src/query/key-creator.mts +24 -13
  113. package/src/query/make-infinite-options.mts +53 -10
  114. package/src/query/make-options.mts +184 -43
  115. package/src/query/prefetch.mts +326 -0
  116. package/src/query/types.mts +56 -17
  117. package/src/client/__type-tests__/client-instance.spec-d.mts +0 -852
@@ -1,4 +1,9 @@
1
- import type { BuilderInstance } from '@navios/builder'
1
+ import type {
2
+ BuilderInstance,
3
+ ErrorSchemaRecord,
4
+ InferErrorSchemaOutput,
5
+ } from '@navios/builder'
6
+ import type { z, ZodType } from 'zod/v4'
2
7
 
3
8
  /**
4
9
  * Splits a string by a delimiter into a tuple type.
@@ -19,13 +24,72 @@ export type ProcessResponseFunction<TData = unknown, TVariables = unknown> = (
19
24
  variables: TVariables,
20
25
  ) => Promise<TData> | TData
21
26
 
27
+ /**
28
+ * Compute the response input type based on discriminator and error schema.
29
+ * When UseDiscriminator=true and errorSchema is present, errors are included as a union.
30
+ * When UseDiscriminator=false, only the success type is returned (errors are thrown).
31
+ *
32
+ * @template UseDiscriminator - Whether to include error types in the response union
33
+ * @template ResponseSchema - The success response schema
34
+ * @template ErrorSchema - The error schema record (optional)
35
+ */
36
+ export type ComputeResponseInput<
37
+ UseDiscriminator extends boolean,
38
+ ResponseSchema extends ZodType,
39
+ ErrorSchema extends ErrorSchemaRecord | undefined,
40
+ > = UseDiscriminator extends true
41
+ ? ErrorSchema extends ErrorSchemaRecord
42
+ ? z.output<ResponseSchema> | InferErrorSchemaOutput<ErrorSchema>
43
+ : z.output<ResponseSchema>
44
+ : z.output<ResponseSchema>
45
+
22
46
  /**
23
47
  * Options for creating a client instance.
48
+ *
49
+ * @template UseDiscriminator - When `true`, errors are returned as union types.
50
+ * When `false` (default), errors are thrown.
24
51
  */
25
- export type ClientOptions = {
26
- api: BuilderInstance
52
+ export type ClientOptions<UseDiscriminator extends boolean = false> = {
53
+ api: BuilderInstance<UseDiscriminator>
27
54
  defaults?: {
28
55
  keyPrefix?: string[]
29
56
  keySuffix?: string[]
30
57
  }
31
58
  }
59
+
60
+ /**
61
+ * Infers the full response type from an endpoint configuration.
62
+ * Returns `ResponseType | ErrorTypes` if errorSchema exists,
63
+ * otherwise just `ResponseType`.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * type Response = InferEndpointResponse<{
68
+ * responseSchema: z.ZodObject<{ data: z.ZodString }>,
69
+ * errorSchema: { 400: z.ZodObject<{ error: z.ZodString }> }
70
+ * }>
71
+ * // Result: { data: string } | { error: string }
72
+ * ```
73
+ */
74
+ export type InferEndpointResponse<
75
+ Config extends {
76
+ responseSchema: ZodType
77
+ errorSchema?: ErrorSchemaRecord
78
+ },
79
+ > = Config['errorSchema'] extends ErrorSchemaRecord
80
+ ? z.output<Config['responseSchema']> | InferErrorSchemaOutput<Config['errorSchema']>
81
+ : z.output<Config['responseSchema']>
82
+
83
+ /**
84
+ * Computes the Result type, applying processResponse transformation
85
+ * to the full response (including error union when present).
86
+ */
87
+ export type ComputeResultType<
88
+ ResponseSchema extends ZodType,
89
+ ErrorSchema extends ErrorSchemaRecord | undefined,
90
+ ProcessedResult,
91
+ > = ProcessedResult extends undefined
92
+ ? ErrorSchema extends ErrorSchemaRecord
93
+ ? z.output<ResponseSchema> | InferErrorSchemaOutput<ErrorSchema>
94
+ : z.output<ResponseSchema>
95
+ : ProcessedResult
@@ -1,3 +1,4 @@
1
1
  export * from './types.mjs'
2
2
  export * from './key-creator.mjs'
3
3
  export * from './make-hook.mjs'
4
+ export * from './optimistic.mjs'
@@ -1,22 +1,105 @@
1
1
  import type {
2
- AbstractEndpoint,
3
2
  AnyEndpointConfig,
4
- NaviosZodRequest,
3
+ BaseEndpointConfig,
4
+ ErrorSchemaRecord,
5
+ HttpMethod,
6
+ InferErrorSchemaOutput,
5
7
  UrlHasParams,
6
8
  UrlParams,
7
9
  } from '@navios/builder'
8
10
  import type {
9
11
  MutationFunctionContext,
12
+ UseMutationOptions,
10
13
  UseMutationResult,
11
14
  } from '@tanstack/react-query'
12
- import type { z } from 'zod/v4'
15
+ import type { z, ZodObject, ZodType } from 'zod/v4'
13
16
 
14
17
  import { useIsMutating, useMutation } from '@tanstack/react-query'
15
18
 
16
- import type { MutationParams } from './types.mjs'
19
+ import type { ProcessResponseFunction } from '../common/types.mjs'
20
+ import type { MutationHelpers } from './types.mjs'
17
21
 
18
22
  import { createMutationKey } from './key-creator.mjs'
19
23
 
24
+ /**
25
+ * Helper type for endpoint with config property
26
+ */
27
+ type EndpointWithConfig<Config extends AnyEndpointConfig> = ((
28
+ params: any,
29
+ ) => Promise<any>) & {
30
+ config: Config
31
+ }
32
+
33
+ /**
34
+ * Helper type for response input when errorSchema is present
35
+ */
36
+ type ResponseInput<
37
+ ResponseSchema extends ZodType,
38
+ ErrorSchema extends ErrorSchemaRecord | undefined,
39
+ > = ErrorSchema extends ErrorSchemaRecord
40
+ ? z.output<ResponseSchema> | InferErrorSchemaOutput<ErrorSchema>
41
+ : z.output<ResponseSchema>
42
+
43
+ /**
44
+ * Options type for makeMutation
45
+ */
46
+ type MakeMutationParams<
47
+ Config extends AnyEndpointConfig,
48
+ ResponseSchema extends ZodType,
49
+ ErrorSchema extends ErrorSchemaRecord | undefined,
50
+ TData,
51
+ TVariables,
52
+ TOnMutateResult,
53
+ TContext,
54
+ UseKey extends boolean,
55
+ > = Omit<
56
+ UseMutationOptions<TData, Error, TVariables>,
57
+ | 'mutationKey'
58
+ | 'mutationFn'
59
+ | 'onMutate'
60
+ | 'onSuccess'
61
+ | 'onError'
62
+ | 'onSettled'
63
+ | 'scope'
64
+ > & {
65
+ processResponse?: ProcessResponseFunction<TData, ResponseInput<ResponseSchema, ErrorSchema>>
66
+ useContext?: () => TContext
67
+ onSuccess?: (
68
+ data: TData,
69
+ variables: TVariables,
70
+ context: TContext &
71
+ MutationFunctionContext & { onMutateResult: TOnMutateResult | undefined },
72
+ ) => void | Promise<void>
73
+ onError?: (
74
+ err: unknown,
75
+ variables: TVariables,
76
+ context: TContext &
77
+ MutationFunctionContext & { onMutateResult: TOnMutateResult | undefined },
78
+ ) => void | Promise<void>
79
+ onMutate?: (
80
+ variables: TVariables,
81
+ context: TContext & MutationFunctionContext,
82
+ ) => TOnMutateResult | Promise<TOnMutateResult>
83
+ onSettled?: (
84
+ data: TData | undefined,
85
+ error: Error | null,
86
+ variables: TVariables,
87
+ context: TContext &
88
+ MutationFunctionContext & { onMutateResult: TOnMutateResult | undefined },
89
+ ) => void | Promise<void>
90
+ useKey?: UseKey
91
+ keyPrefix?: UseKey extends true
92
+ ? UrlHasParams<Config['url']> extends true
93
+ ? string[]
94
+ : never
95
+ : never
96
+ keySuffix?: UseKey extends true
97
+ ? UrlHasParams<Config['url']> extends true
98
+ ? string[]
99
+ : never
100
+ : never
101
+ }
102
+
20
103
  /**
21
104
  * Creates a mutation hook for a given endpoint.
22
105
  *
@@ -27,44 +110,87 @@ import { createMutationKey } from './key-creator.mjs'
27
110
  * @param options - Mutation configuration including processResponse and callbacks
28
111
  * @returns A hook function that returns mutation result with attached helpers
29
112
  */
113
+ // Overload: WITH errorSchema
30
114
  export function makeMutation<
31
- Config extends AnyEndpointConfig,
32
- TData = unknown,
33
- TVariables extends NaviosZodRequest<Config> = NaviosZodRequest<Config>,
34
- TResponse = z.output<Config['responseSchema']>,
115
+ Method extends HttpMethod,
116
+ Url extends string,
117
+ QuerySchema extends ZodObject | undefined,
118
+ ResponseSchema extends ZodType,
119
+ RequestSchema extends ZodType,
120
+ ErrorSchema extends ErrorSchemaRecord,
121
+ TData,
122
+ TOnMutateResult = unknown,
123
+ TContext = unknown,
124
+ UseKey extends boolean = false,
125
+ >(
126
+ endpoint: EndpointWithConfig<
127
+ BaseEndpointConfig<Method, Url, QuerySchema, ResponseSchema, RequestSchema, ErrorSchema>
128
+ >,
129
+ options: MakeMutationParams<
130
+ BaseEndpointConfig<Method, Url, QuerySchema, ResponseSchema, RequestSchema, ErrorSchema>,
131
+ ResponseSchema,
132
+ ErrorSchema,
133
+ TData,
134
+ any,
135
+ TOnMutateResult,
136
+ TContext,
137
+ UseKey
138
+ >,
139
+ ): ((
140
+ keyParams: UseKey extends true
141
+ ? UrlHasParams<Url> extends true
142
+ ? { urlParams: UrlParams<Url> }
143
+ : never
144
+ : never,
145
+ ) => UseMutationResult<TData, Error, any, TOnMutateResult>) &
146
+ MutationHelpers<Url, TData>
147
+
148
+ // Overload: WITHOUT errorSchema
149
+ export function makeMutation<
150
+ Method extends HttpMethod,
151
+ Url extends string,
152
+ QuerySchema extends ZodObject | undefined,
153
+ ResponseSchema extends ZodType,
154
+ RequestSchema extends ZodType | undefined,
155
+ TData,
35
156
  TOnMutateResult = unknown,
36
157
  TContext = unknown,
37
158
  UseKey extends boolean = false,
38
159
  >(
39
- endpoint: AbstractEndpoint<Config>,
40
- options: MutationParams<
41
- Config,
160
+ endpoint: EndpointWithConfig<
161
+ BaseEndpointConfig<Method, Url, QuerySchema, ResponseSchema, RequestSchema, undefined>
162
+ >,
163
+ options: MakeMutationParams<
164
+ BaseEndpointConfig<Method, Url, QuerySchema, ResponseSchema, RequestSchema, undefined>,
165
+ ResponseSchema,
166
+ undefined,
42
167
  TData,
43
- TVariables,
44
- TResponse,
168
+ any,
45
169
  TOnMutateResult,
46
170
  TContext,
47
171
  UseKey
48
172
  >,
49
- ) {
173
+ ): ((
174
+ keyParams: UseKey extends true
175
+ ? UrlHasParams<Url> extends true
176
+ ? { urlParams: UrlParams<Url> }
177
+ : never
178
+ : never,
179
+ ) => UseMutationResult<TData, Error, any, TOnMutateResult>) &
180
+ MutationHelpers<Url, TData>
181
+
182
+ // Implementation
183
+ export function makeMutation(
184
+ endpoint: EndpointWithConfig<AnyEndpointConfig>,
185
+ options: any,
186
+ ): any {
50
187
  const config = endpoint.config
51
188
 
52
189
  const mutationKey = createMutationKey(config, {
53
190
  ...options,
54
- processResponse: options.processResponse ?? ((data) => data),
191
+ processResponse: options.processResponse ?? ((data: any) => data),
55
192
  })
56
- const result = (
57
- keyParams: UseKey extends true
58
- ? UrlHasParams<Config['url']> extends true
59
- ? { urlParams: UrlParams<Config['url']> }
60
- : never
61
- : never,
62
- ): UseMutationResult<
63
- TData,
64
- Error,
65
- NaviosZodRequest<Config>,
66
- TOnMutateResult
67
- > => {
193
+ const result = (keyParams: any): any => {
68
194
  const {
69
195
  useKey,
70
196
  useContext,
@@ -78,9 +204,8 @@ export function makeMutation<
78
204
  ...rest
79
205
  } = options
80
206
 
81
- const ownContext = (useContext?.() as TContext) ?? {}
207
+ const ownContext = useContext?.() ?? {}
82
208
 
83
- // @ts-expect-error The types match
84
209
  return useMutation({
85
210
  ...rest,
86
211
  mutationKey: useKey ? mutationKey(keyParams) : undefined,
@@ -89,89 +214,72 @@ export function makeMutation<
89
214
  id: JSON.stringify(mutationKey(keyParams)),
90
215
  }
91
216
  : undefined,
92
- async mutationFn(params: TVariables) {
217
+ async mutationFn(params: any) {
93
218
  const response = await endpoint(params)
94
219
 
95
- return (processResponse ? processResponse(response) : response) as TData
220
+ return processResponse ? processResponse(response) : response
96
221
  },
97
222
  onSuccess: onSuccess
98
223
  ? (
99
- data: TData,
100
- variables: TVariables,
101
- onMutateResult: TOnMutateResult | undefined,
224
+ data: any,
225
+ variables: any,
226
+ onMutateResult: any,
102
227
  context: MutationFunctionContext,
103
228
  ) => {
104
229
  return onSuccess?.(data, variables, {
105
230
  ...ownContext,
106
231
  ...context,
107
232
  onMutateResult,
108
- } as TContext &
109
- MutationFunctionContext & {
110
- onMutateResult: TOnMutateResult | undefined
111
- })
233
+ })
112
234
  }
113
235
  : undefined,
114
236
  onError: onError
115
237
  ? (
116
238
  err: Error,
117
- variables: TVariables,
118
- onMutateResult: TOnMutateResult | undefined,
239
+ variables: any,
240
+ onMutateResult: any,
119
241
  context: MutationFunctionContext,
120
242
  ) => {
121
243
  return onError?.(err, variables, {
122
244
  onMutateResult,
123
245
  ...ownContext,
124
246
  ...context,
125
- } as TContext &
126
- MutationFunctionContext & {
127
- onMutateResult: TOnMutateResult | undefined
128
- })
247
+ })
129
248
  }
130
249
  : undefined,
131
250
  onMutate: onMutate
132
- ? (variables: TVariables, context: MutationFunctionContext) => {
251
+ ? (variables: any, context: MutationFunctionContext) => {
133
252
  return onMutate(variables, {
134
253
  ...ownContext,
135
254
  ...context,
136
- } as TContext & MutationFunctionContext)
255
+ })
137
256
  }
138
257
  : undefined,
139
258
  onSettled: onSettled
140
259
  ? (
141
- data: TData | undefined,
260
+ data: any,
142
261
  error: Error | null,
143
- variables: TVariables,
144
- onMutateResult: TOnMutateResult | undefined,
262
+ variables: any,
263
+ onMutateResult: any,
145
264
  context: MutationFunctionContext,
146
265
  ) => {
147
266
  return onSettled(data, error, variables, {
148
267
  ...ownContext,
149
268
  ...context,
150
269
  onMutateResult,
151
- } as TContext &
152
- MutationFunctionContext & {
153
- onMutateResult: TOnMutateResult | undefined
154
- })
270
+ })
155
271
  }
156
272
  : undefined,
157
273
  })
158
274
  }
159
- result.useIsMutating = (
160
- keyParams: UseKey extends true
161
- ? UrlHasParams<Config['url']> extends true
162
- ? UrlParams<Config['url']>
163
- : never
164
- : never,
165
- ): boolean => {
275
+ result.useIsMutating = (keyParams: any): boolean => {
166
276
  if (!options.useKey) {
167
277
  throw new Error(
168
278
  'useIsMutating can only be used when useKey is set to true',
169
279
  )
170
280
  }
171
281
  const isMutating = useIsMutating({
172
- mutationKey: mutationKey({
173
- urlParams: keyParams,
174
- }),
282
+ mutationKey: mutationKey(keyParams),
175
283
  })
176
284
  return isMutating > 0
177
285
  }