@navios/react-query 0.7.0 → 1.0.0-alpha.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 (124) hide show
  1. package/CHANGELOG.md +166 -0
  2. package/README.md +152 -4
  3. package/dist/src/__tests__/errorSchema.spec.d.mts +2 -0
  4. package/dist/src/__tests__/errorSchema.spec.d.mts.map +1 -0
  5. package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts +2 -0
  6. package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts.map +1 -0
  7. package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts +2 -0
  8. package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts.map +1 -0
  9. package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts +2 -0
  10. package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts.map +1 -0
  11. package/dist/src/client/__type-tests__/mutation.spec-d.d.mts +2 -0
  12. package/dist/src/client/__type-tests__/mutation.spec-d.d.mts.map +1 -0
  13. package/dist/src/client/__type-tests__/query.spec-d.d.mts +2 -0
  14. package/dist/src/client/__type-tests__/query.spec-d.d.mts.map +1 -0
  15. package/dist/src/client/declare-client.d.mts +15 -8
  16. package/dist/src/client/declare-client.d.mts.map +1 -1
  17. package/dist/src/client/types/from-endpoint.d.mts +130 -0
  18. package/dist/src/client/types/from-endpoint.d.mts.map +1 -0
  19. package/dist/src/client/types/helpers.d.mts +74 -0
  20. package/dist/src/client/types/helpers.d.mts.map +1 -0
  21. package/dist/src/client/types/index.d.mts +21 -0
  22. package/dist/src/client/types/index.d.mts.map +1 -0
  23. package/dist/src/client/types/infinite-query.d.mts +61 -0
  24. package/dist/src/client/types/infinite-query.d.mts.map +1 -0
  25. package/dist/src/client/types/multipart-mutation.d.mts +98 -0
  26. package/dist/src/client/types/multipart-mutation.d.mts.map +1 -0
  27. package/dist/src/client/types/mutation.d.mts +75 -0
  28. package/dist/src/client/types/mutation.d.mts.map +1 -0
  29. package/dist/src/client/types/query.d.mts +65 -0
  30. package/dist/src/client/types/query.d.mts.map +1 -0
  31. package/dist/src/client/types.d.mts +1 -608
  32. package/dist/src/client/types.d.mts.map +1 -1
  33. package/dist/src/common/types.d.mts +30 -3
  34. package/dist/src/common/types.d.mts.map +1 -1
  35. package/dist/src/mutation/index.d.mts +1 -0
  36. package/dist/src/mutation/index.d.mts.map +1 -1
  37. package/dist/src/mutation/make-hook.d.mts +42 -16
  38. package/dist/src/mutation/make-hook.d.mts.map +1 -1
  39. package/dist/src/mutation/optimistic.d.mts +166 -0
  40. package/dist/src/mutation/optimistic.d.mts.map +1 -0
  41. package/dist/src/mutation/types.d.mts +51 -19
  42. package/dist/src/mutation/types.d.mts.map +1 -1
  43. package/dist/src/query/index.d.mts +1 -0
  44. package/dist/src/query/index.d.mts.map +1 -1
  45. package/dist/src/query/key-creator.d.mts.map +1 -1
  46. package/dist/src/query/make-infinite-options.d.mts +3 -2
  47. package/dist/src/query/make-infinite-options.d.mts.map +1 -1
  48. package/dist/src/query/make-options.d.mts +42 -12
  49. package/dist/src/query/make-options.d.mts.map +1 -1
  50. package/dist/src/query/prefetch.d.mts +245 -0
  51. package/dist/src/query/prefetch.d.mts.map +1 -0
  52. package/dist/src/query/types.d.mts +35 -17
  53. package/dist/src/query/types.d.mts.map +1 -1
  54. package/dist/tsconfig.tsbuildinfo +1 -1
  55. package/lib/index.cjs +454 -37
  56. package/lib/index.cjs.map +1 -1
  57. package/lib/index.d.cts +1021 -598
  58. package/lib/index.d.cts.map +1 -1
  59. package/lib/index.d.mts +1019 -596
  60. package/lib/index.d.mts.map +1 -1
  61. package/lib/index.mjs +441 -29
  62. package/lib/index.mjs.map +1 -1
  63. package/package.json +8 -8
  64. package/src/__tests__/declare-client.spec.mts +1 -2
  65. package/src/__tests__/errorSchema.spec.mts +391 -0
  66. package/src/__tests__/make-mutation.spec.mts +6 -5
  67. package/src/__tests__/makeDataTag.spec.mts +2 -1
  68. package/src/__tests__/makeQueryOptions.spec.mts +2 -1
  69. package/src/client/__type-tests__/from-endpoint.spec-d.mts +550 -0
  70. package/src/client/__type-tests__/infinite-query.spec-d.mts +648 -0
  71. package/src/client/__type-tests__/multipart-mutation.spec-d.mts +725 -0
  72. package/src/client/__type-tests__/mutation.spec-d.mts +757 -0
  73. package/src/client/__type-tests__/query.spec-d.mts +701 -0
  74. package/src/client/declare-client.mts +59 -34
  75. package/src/client/types/from-endpoint.mts +345 -0
  76. package/src/client/types/helpers.mts +140 -0
  77. package/src/client/types/index.mts +26 -0
  78. package/src/client/types/infinite-query.mts +133 -0
  79. package/src/client/types/multipart-mutation.mts +264 -0
  80. package/src/client/types/mutation.mts +176 -0
  81. package/src/client/types/query.mts +132 -0
  82. package/src/client/types.mts +1 -1935
  83. package/src/common/types.mts +48 -3
  84. package/src/mutation/index.mts +1 -0
  85. package/src/mutation/make-hook.mts +171 -63
  86. package/src/mutation/optimistic.mts +294 -0
  87. package/src/mutation/types.mts +102 -29
  88. package/src/query/index.mts +1 -0
  89. package/src/query/key-creator.mts +24 -13
  90. package/src/query/make-infinite-options.mts +53 -10
  91. package/src/query/make-options.mts +184 -43
  92. package/src/query/prefetch.mts +326 -0
  93. package/src/query/types.mts +76 -16
  94. package/dist/src/declare-client.d.mts +0 -31
  95. package/dist/src/declare-client.d.mts.map +0 -1
  96. package/dist/src/make-infinite-query-options.d.mts +0 -13
  97. package/dist/src/make-infinite-query-options.d.mts.map +0 -1
  98. package/dist/src/make-mutation.d.mts +0 -15
  99. package/dist/src/make-mutation.d.mts.map +0 -1
  100. package/dist/src/make-query-options.d.mts +0 -15
  101. package/dist/src/make-query-options.d.mts.map +0 -1
  102. package/dist/src/types/client-endpoint-helper.d.mts +0 -8
  103. package/dist/src/types/client-endpoint-helper.d.mts.map +0 -1
  104. package/dist/src/types/client-instance.d.mts +0 -211
  105. package/dist/src/types/client-instance.d.mts.map +0 -1
  106. package/dist/src/types/index.d.mts +0 -8
  107. package/dist/src/types/index.d.mts.map +0 -1
  108. package/dist/src/types/mutation-args.d.mts +0 -10
  109. package/dist/src/types/mutation-args.d.mts.map +0 -1
  110. package/dist/src/types/mutation-helpers.d.mts +0 -10
  111. package/dist/src/types/mutation-helpers.d.mts.map +0 -1
  112. package/dist/src/types/query-args.d.mts +0 -8
  113. package/dist/src/types/query-args.d.mts.map +0 -1
  114. package/dist/src/types/query-helpers.d.mts +0 -14
  115. package/dist/src/types/query-helpers.d.mts.map +0 -1
  116. package/dist/src/types/query-url-params-args.d.mts +0 -5
  117. package/dist/src/types/query-url-params-args.d.mts.map +0 -1
  118. package/dist/src/types.d.mts +0 -49
  119. package/dist/src/types.d.mts.map +0 -1
  120. package/dist/src/utils/mutation-key.creator.d.mts +0 -39
  121. package/dist/src/utils/mutation-key.creator.d.mts.map +0 -1
  122. package/dist/src/utils/query-key-creator.d.mts +0 -24
  123. package/dist/src/utils/query-key-creator.d.mts.map +0 -1
  124. package/src/client/__type-tests__/client-instance.spec-d.mts +0 -852
@@ -0,0 +1,176 @@
1
+ import type {
2
+ EndpointOptions,
3
+ ErrorSchemaRecord,
4
+ HttpMethod,
5
+ RequestArgs,
6
+ Simplify,
7
+ UrlHasParams,
8
+ UrlParams,
9
+ } from '@navios/builder'
10
+ import type {
11
+ MutationFunctionContext,
12
+ UseMutationResult,
13
+ } from '@tanstack/react-query'
14
+ import type { ZodObject, ZodType } from 'zod/v4'
15
+
16
+ import type { MutationHelpers } from '../../mutation/types.mjs'
17
+ import type { ComputeBaseResult, EndpointHelper } from './helpers.mjs'
18
+
19
+ /**
20
+ * Compute variables type from URL, schemas
21
+ */
22
+ type ComputeVariables<
23
+ Url extends string,
24
+ QuerySchema extends ZodObject | undefined,
25
+ RequestSchema extends ZodType | undefined,
26
+ UrlParamsSchema extends ZodObject | undefined,
27
+ > = Simplify<RequestArgs<Url, QuerySchema, RequestSchema, UrlParamsSchema>>
28
+
29
+ /**
30
+ * Extended endpoint options interface for mutation that includes processResponse and callbacks.
31
+ */
32
+ interface MutationEndpointConfig<
33
+ _UseDiscriminator extends boolean,
34
+ Method extends HttpMethod,
35
+ Url extends string,
36
+ QuerySchema extends ZodObject | undefined,
37
+ RequestSchema extends ZodType | undefined,
38
+ ResponseSchema extends ZodType,
39
+ ErrorSchema extends ErrorSchemaRecord | undefined,
40
+ UrlParamsSchema extends ZodObject | undefined,
41
+ UseKey extends boolean,
42
+ TBaseResult,
43
+ Result,
44
+ OnMutateResult,
45
+ Context,
46
+ Variables,
47
+ > extends EndpointOptions {
48
+ method: Method
49
+ url: Url
50
+ querySchema?: QuerySchema
51
+ requestSchema?: RequestSchema
52
+ responseSchema: ResponseSchema
53
+ errorSchema?: ErrorSchema
54
+ urlParamsSchema?: UrlParamsSchema
55
+ processResponse?: (data: TBaseResult) => Result | Promise<Result>
56
+ useContext?: () => Context
57
+ useKey?: UseKey
58
+ onMutate?: (
59
+ variables: Variables,
60
+ context: Context & MutationFunctionContext,
61
+ ) => OnMutateResult | Promise<OnMutateResult>
62
+ onSuccess?: (
63
+ data: NoInfer<Result>,
64
+ variables: Variables,
65
+ context: Context &
66
+ MutationFunctionContext & {
67
+ onMutateResult: OnMutateResult | undefined
68
+ },
69
+ ) => void | Promise<void>
70
+ onError?: (
71
+ error: Error,
72
+ variables: Variables,
73
+ context: Context &
74
+ MutationFunctionContext & {
75
+ onMutateResult: OnMutateResult | undefined
76
+ },
77
+ ) => void | Promise<void>
78
+ onSettled?: (
79
+ data: NoInfer<Result> | undefined,
80
+ error: Error | null,
81
+ variables: Variables,
82
+ context: Context &
83
+ MutationFunctionContext & {
84
+ onMutateResult: OnMutateResult | undefined
85
+ },
86
+ ) => void | Promise<void>
87
+ }
88
+
89
+ /**
90
+ * Mutation method using decomposed generics pattern for proper processResponse typing.
91
+ *
92
+ * @template UseDiscriminator - When `true`, errors are returned as union types.
93
+ * When `false` (default), errors are thrown and not included in TData.
94
+ */
95
+ export interface ClientMutationMethods<
96
+ UseDiscriminator extends boolean = false,
97
+ > {
98
+ /**
99
+ * Creates a type-safe mutation with automatic type inference.
100
+ *
101
+ * Uses decomposed generic pattern to infer types from the configuration object.
102
+ * All schema combinations are handled by a single method.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * const createUser = client.mutation({
107
+ * method: 'POST',
108
+ * url: '/users',
109
+ * requestSchema: createUserSchema,
110
+ * responseSchema: userSchema,
111
+ * processResponse: (data) => data,
112
+ * })
113
+ *
114
+ * const { mutate } = createUser()
115
+ * mutate({ data: { name: 'John' } })
116
+ * ```
117
+ */
118
+ mutation<
119
+ const Method extends HttpMethod = HttpMethod,
120
+ const Url extends string = string,
121
+ const QuerySchema extends ZodObject | undefined = undefined,
122
+ const RequestSchema extends ZodType | undefined = undefined,
123
+ const ResponseSchema extends ZodType = ZodType,
124
+ const ErrorSchema extends ErrorSchemaRecord | undefined = undefined,
125
+ const UrlParamsSchema extends ZodObject | undefined = undefined,
126
+ const UseKey extends boolean = false,
127
+ const TBaseResult = ComputeBaseResult<
128
+ UseDiscriminator,
129
+ ResponseSchema,
130
+ ErrorSchema
131
+ >,
132
+ const Result = TBaseResult,
133
+ const OnMutateResult = unknown,
134
+ const Context = unknown,
135
+ const Variables = ComputeVariables<
136
+ Url,
137
+ QuerySchema,
138
+ RequestSchema,
139
+ UrlParamsSchema
140
+ >,
141
+ const Options extends EndpointOptions = {
142
+ method: Method
143
+ url: Url
144
+ querySchema: QuerySchema
145
+ requestSchema: RequestSchema
146
+ responseSchema: ResponseSchema
147
+ errorSchema: ErrorSchema
148
+ urlParamsSchema: UrlParamsSchema
149
+ },
150
+ >(
151
+ config: MutationEndpointConfig<
152
+ UseDiscriminator,
153
+ Method,
154
+ Url,
155
+ QuerySchema,
156
+ RequestSchema,
157
+ ResponseSchema,
158
+ ErrorSchema,
159
+ UrlParamsSchema,
160
+ UseKey,
161
+ TBaseResult,
162
+ Result,
163
+ OnMutateResult,
164
+ Context,
165
+ Variables
166
+ >,
167
+ ): ((
168
+ ...args: UseKey extends true
169
+ ? UrlHasParams<Url> extends true
170
+ ? [{ urlParams: UrlParams<Url> }]
171
+ : [{}]
172
+ : []
173
+ ) => UseMutationResult<Result, Error, Variables, OnMutateResult>) &
174
+ (UseKey extends true ? MutationHelpers<Url, Result> : {}) &
175
+ EndpointHelper<Options, UseDiscriminator>
176
+ }
@@ -0,0 +1,132 @@
1
+ import type {
2
+ EndpointOptions,
3
+ ErrorSchemaRecord,
4
+ HttpMethod,
5
+ InferEndpointParams,
6
+ Simplify,
7
+ } from '@navios/builder'
8
+ import type { DataTag, UseSuspenseQueryOptions } from '@tanstack/react-query'
9
+ import type { ZodObject, ZodType } from 'zod/v4'
10
+
11
+ import type { Split } from '../../common/types.mjs'
12
+ import type { QueryHelpers } from '../../query/types.mjs'
13
+ import type { ComputeBaseResult, EndpointHelper } from './helpers.mjs'
14
+
15
+ /**
16
+ * Helper type to build endpoint options without including undefined properties.
17
+ * This ensures HasProperty correctly identifies missing properties.
18
+ */
19
+ type BuildEndpointOptions<
20
+ Method extends HttpMethod,
21
+ Url extends string,
22
+ QuerySchema extends ZodObject | undefined,
23
+ RequestSchema extends ZodType | undefined,
24
+ ResponseSchema extends ZodType,
25
+ ErrorSchema extends ErrorSchemaRecord | undefined,
26
+ UrlParamsSchema extends ZodObject | undefined,
27
+ > = {
28
+ method: Method
29
+ url: Url
30
+ responseSchema: ResponseSchema
31
+ } & (QuerySchema extends undefined ? {} : { querySchema: QuerySchema }) &
32
+ (RequestSchema extends undefined ? {} : { requestSchema: RequestSchema }) &
33
+ (ErrorSchema extends undefined ? {} : { errorSchema: ErrorSchema }) &
34
+ (UrlParamsSchema extends undefined
35
+ ? {}
36
+ : { urlParamsSchema: UrlParamsSchema })
37
+
38
+ /**
39
+ * Extended endpoint options interface for query that includes processResponse.
40
+ */
41
+ interface QueryEndpointConfig<
42
+ Method extends HttpMethod,
43
+ Url extends string,
44
+ QuerySchema extends ZodObject | undefined,
45
+ RequestSchema extends ZodType | undefined,
46
+ ResponseSchema extends ZodType,
47
+ ErrorSchema extends ErrorSchemaRecord | undefined,
48
+ UrlParamsSchema extends ZodObject | undefined,
49
+ TBaseResult,
50
+ Result,
51
+ > extends EndpointOptions {
52
+ method: Method
53
+ url: Url
54
+ querySchema?: QuerySchema
55
+ requestSchema?: RequestSchema
56
+ responseSchema: ResponseSchema
57
+ errorSchema?: ErrorSchema
58
+ urlParamsSchema?: UrlParamsSchema
59
+ processResponse?: (data: TBaseResult) => Result
60
+ }
61
+
62
+ /**
63
+ * Query method using decomposed generics pattern for proper processResponse typing.
64
+ *
65
+ * @template UseDiscriminator - When `true`, errors are returned as union types.
66
+ * When `false` (default), errors are thrown and not included in TData.
67
+ */
68
+ export interface ClientQueryMethods<UseDiscriminator extends boolean = false> {
69
+ /**
70
+ * Creates a type-safe query with automatic type inference.
71
+ *
72
+ * Uses decomposed generic pattern to infer types from the configuration object.
73
+ * All schema combinations are handled by a single method.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * const getUser = client.query({
78
+ * method: 'GET',
79
+ * url: '/users/$userId',
80
+ * responseSchema: userSchema,
81
+ * urlParamsSchema: z.object({ userId: z.string().uuid() }),
82
+ * })
83
+ *
84
+ * const { data } = getUser.useSuspense({ urlParams: { userId: '123' } })
85
+ * ```
86
+ */
87
+ query<
88
+ const Method extends HttpMethod = HttpMethod,
89
+ const Url extends string = string,
90
+ const QuerySchema extends ZodObject | undefined = undefined,
91
+ const RequestSchema extends ZodType | undefined = undefined,
92
+ const ResponseSchema extends ZodType = ZodType,
93
+ const ErrorSchema extends ErrorSchemaRecord | undefined = undefined,
94
+ const UrlParamsSchema extends ZodObject | undefined = undefined,
95
+ const TBaseResult = ComputeBaseResult<
96
+ UseDiscriminator,
97
+ ResponseSchema,
98
+ ErrorSchema
99
+ >,
100
+ const Result = TBaseResult,
101
+ const Options extends EndpointOptions = BuildEndpointOptions<
102
+ Method,
103
+ Url,
104
+ QuerySchema,
105
+ RequestSchema,
106
+ ResponseSchema,
107
+ ErrorSchema,
108
+ UrlParamsSchema
109
+ >,
110
+ >(
111
+ config: QueryEndpointConfig<
112
+ Method,
113
+ Url,
114
+ QuerySchema,
115
+ RequestSchema,
116
+ ResponseSchema,
117
+ ErrorSchema,
118
+ UrlParamsSchema,
119
+ TBaseResult,
120
+ Result
121
+ >,
122
+ ): ((
123
+ params: Simplify<InferEndpointParams<Options>>,
124
+ ) => UseSuspenseQueryOptions<
125
+ Result,
126
+ Error,
127
+ Result,
128
+ DataTag<Split<Url, '/'>, Result, Error>
129
+ >) &
130
+ QueryHelpers<Url, QuerySchema, Result, false, RequestSchema> &
131
+ EndpointHelper<Options, UseDiscriminator>
132
+ }