@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.
- package/CHANGELOG.md +166 -0
- package/README.md +152 -4
- package/dist/src/__tests__/errorSchema.spec.d.mts +2 -0
- package/dist/src/__tests__/errorSchema.spec.d.mts.map +1 -0
- package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts +2 -0
- package/dist/src/client/__type-tests__/from-endpoint.spec-d.d.mts.map +1 -0
- package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts +2 -0
- package/dist/src/client/__type-tests__/infinite-query.spec-d.d.mts.map +1 -0
- package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts +2 -0
- package/dist/src/client/__type-tests__/multipart-mutation.spec-d.d.mts.map +1 -0
- package/dist/src/client/__type-tests__/mutation.spec-d.d.mts +2 -0
- package/dist/src/client/__type-tests__/mutation.spec-d.d.mts.map +1 -0
- package/dist/src/client/__type-tests__/query.spec-d.d.mts +2 -0
- package/dist/src/client/__type-tests__/query.spec-d.d.mts.map +1 -0
- package/dist/src/client/declare-client.d.mts +15 -8
- package/dist/src/client/declare-client.d.mts.map +1 -1
- package/dist/src/client/types/from-endpoint.d.mts +130 -0
- package/dist/src/client/types/from-endpoint.d.mts.map +1 -0
- package/dist/src/client/types/helpers.d.mts +74 -0
- package/dist/src/client/types/helpers.d.mts.map +1 -0
- package/dist/src/client/types/index.d.mts +21 -0
- package/dist/src/client/types/index.d.mts.map +1 -0
- package/dist/src/client/types/infinite-query.d.mts +61 -0
- package/dist/src/client/types/infinite-query.d.mts.map +1 -0
- package/dist/src/client/types/multipart-mutation.d.mts +98 -0
- package/dist/src/client/types/multipart-mutation.d.mts.map +1 -0
- package/dist/src/client/types/mutation.d.mts +75 -0
- package/dist/src/client/types/mutation.d.mts.map +1 -0
- package/dist/src/client/types/query.d.mts +65 -0
- package/dist/src/client/types/query.d.mts.map +1 -0
- package/dist/src/client/types.d.mts +1 -608
- package/dist/src/client/types.d.mts.map +1 -1
- package/dist/src/common/types.d.mts +30 -3
- package/dist/src/common/types.d.mts.map +1 -1
- package/dist/src/mutation/index.d.mts +1 -0
- package/dist/src/mutation/index.d.mts.map +1 -1
- package/dist/src/mutation/make-hook.d.mts +42 -16
- package/dist/src/mutation/make-hook.d.mts.map +1 -1
- package/dist/src/mutation/optimistic.d.mts +166 -0
- package/dist/src/mutation/optimistic.d.mts.map +1 -0
- package/dist/src/mutation/types.d.mts +51 -19
- package/dist/src/mutation/types.d.mts.map +1 -1
- package/dist/src/query/index.d.mts +1 -0
- package/dist/src/query/index.d.mts.map +1 -1
- package/dist/src/query/key-creator.d.mts.map +1 -1
- package/dist/src/query/make-infinite-options.d.mts +3 -2
- package/dist/src/query/make-infinite-options.d.mts.map +1 -1
- package/dist/src/query/make-options.d.mts +42 -12
- package/dist/src/query/make-options.d.mts.map +1 -1
- package/dist/src/query/prefetch.d.mts +245 -0
- package/dist/src/query/prefetch.d.mts.map +1 -0
- package/dist/src/query/types.d.mts +35 -17
- package/dist/src/query/types.d.mts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/index.cjs +454 -37
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +1021 -598
- package/lib/index.d.cts.map +1 -1
- package/lib/index.d.mts +1019 -596
- package/lib/index.d.mts.map +1 -1
- package/lib/index.mjs +441 -29
- package/lib/index.mjs.map +1 -1
- package/package.json +8 -8
- package/src/__tests__/declare-client.spec.mts +1 -2
- package/src/__tests__/errorSchema.spec.mts +391 -0
- package/src/__tests__/make-mutation.spec.mts +6 -5
- package/src/__tests__/makeDataTag.spec.mts +2 -1
- package/src/__tests__/makeQueryOptions.spec.mts +2 -1
- package/src/client/__type-tests__/from-endpoint.spec-d.mts +550 -0
- package/src/client/__type-tests__/infinite-query.spec-d.mts +648 -0
- package/src/client/__type-tests__/multipart-mutation.spec-d.mts +725 -0
- package/src/client/__type-tests__/mutation.spec-d.mts +757 -0
- package/src/client/__type-tests__/query.spec-d.mts +701 -0
- package/src/client/declare-client.mts +59 -34
- package/src/client/types/from-endpoint.mts +345 -0
- package/src/client/types/helpers.mts +140 -0
- package/src/client/types/index.mts +26 -0
- package/src/client/types/infinite-query.mts +133 -0
- package/src/client/types/multipart-mutation.mts +264 -0
- package/src/client/types/mutation.mts +176 -0
- package/src/client/types/query.mts +132 -0
- package/src/client/types.mts +1 -1935
- package/src/common/types.mts +48 -3
- package/src/mutation/index.mts +1 -0
- package/src/mutation/make-hook.mts +171 -63
- package/src/mutation/optimistic.mts +294 -0
- package/src/mutation/types.mts +102 -29
- package/src/query/index.mts +1 -0
- package/src/query/key-creator.mts +24 -13
- package/src/query/make-infinite-options.mts +53 -10
- package/src/query/make-options.mts +184 -43
- package/src/query/prefetch.mts +326 -0
- package/src/query/types.mts +76 -16
- package/dist/src/declare-client.d.mts +0 -31
- package/dist/src/declare-client.d.mts.map +0 -1
- package/dist/src/make-infinite-query-options.d.mts +0 -13
- package/dist/src/make-infinite-query-options.d.mts.map +0 -1
- package/dist/src/make-mutation.d.mts +0 -15
- package/dist/src/make-mutation.d.mts.map +0 -1
- package/dist/src/make-query-options.d.mts +0 -15
- package/dist/src/make-query-options.d.mts.map +0 -1
- package/dist/src/types/client-endpoint-helper.d.mts +0 -8
- package/dist/src/types/client-endpoint-helper.d.mts.map +0 -1
- package/dist/src/types/client-instance.d.mts +0 -211
- package/dist/src/types/client-instance.d.mts.map +0 -1
- package/dist/src/types/index.d.mts +0 -8
- package/dist/src/types/index.d.mts.map +0 -1
- package/dist/src/types/mutation-args.d.mts +0 -10
- package/dist/src/types/mutation-args.d.mts.map +0 -1
- package/dist/src/types/mutation-helpers.d.mts +0 -10
- package/dist/src/types/mutation-helpers.d.mts.map +0 -1
- package/dist/src/types/query-args.d.mts +0 -8
- package/dist/src/types/query-args.d.mts.map +0 -1
- package/dist/src/types/query-helpers.d.mts +0 -14
- package/dist/src/types/query-helpers.d.mts.map +0 -1
- package/dist/src/types/query-url-params-args.d.mts +0 -5
- package/dist/src/types/query-url-params-args.d.mts.map +0 -1
- package/dist/src/types.d.mts +0 -49
- package/dist/src/types.d.mts.map +0 -1
- package/dist/src/utils/mutation-key.creator.d.mts +0 -39
- package/dist/src/utils/mutation-key.creator.d.mts.map +0 -1
- package/dist/src/utils/query-key-creator.d.mts +0 -24
- package/dist/src/utils/query-key-creator.d.mts.map +0 -1
- 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
|
+
}
|