@navios/react-query 0.4.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.
- package/CHANGELOG.md +74 -0
- package/dist/src/client/declare-client.d.mts +70 -0
- package/dist/src/client/declare-client.d.mts.map +1 -0
- package/dist/src/client/index.d.mts +3 -0
- package/dist/src/client/index.d.mts.map +1 -0
- package/dist/src/client/types.d.mts +287 -0
- package/dist/src/client/types.d.mts.map +1 -0
- package/dist/src/common/index.d.mts +2 -0
- package/dist/src/common/index.d.mts.map +1 -0
- package/dist/src/common/types.d.mts +21 -0
- package/dist/src/common/types.d.mts.map +1 -0
- package/dist/src/index.d.mts +13 -8
- package/dist/src/index.d.mts.map +1 -1
- package/dist/src/mutation/index.d.mts +4 -0
- package/dist/src/mutation/index.d.mts.map +1 -0
- package/dist/src/mutation/key-creator.d.mts +36 -0
- package/dist/src/mutation/key-creator.d.mts.map +1 -0
- package/dist/src/mutation/make-hook.d.mts +25 -0
- package/dist/src/mutation/make-hook.d.mts.map +1 -0
- package/dist/src/mutation/types.d.mts +50 -0
- package/dist/src/mutation/types.d.mts.map +1 -0
- package/dist/src/query/index.d.mts +5 -0
- package/dist/src/query/index.d.mts.map +1 -0
- package/dist/src/query/key-creator.d.mts +17 -0
- package/dist/src/query/key-creator.d.mts.map +1 -0
- package/dist/src/query/make-infinite-options.d.mts +23 -0
- package/dist/src/query/make-infinite-options.d.mts.map +1 -0
- package/dist/src/query/make-options.d.mts +24 -0
- package/dist/src/query/make-options.d.mts.map +1 -0
- package/dist/src/query/types.d.mts +83 -0
- package/dist/src/query/types.d.mts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/_tsup-dts-rollup.d.mts +500 -250
- package/lib/_tsup-dts-rollup.d.ts +500 -250
- package/lib/index.d.mts +26 -19
- package/lib/index.d.ts +26 -19
- package/lib/index.js +92 -66
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +92 -68
- package/lib/index.mjs.map +1 -1
- package/package.json +7 -7
- package/project.json +9 -1
- package/src/__tests__/client-type-check.spec.mts +1 -1
- package/src/__tests__/declare-client.spec.mts +1 -1
- package/src/__tests__/make-mutation.spec.mts +1 -1
- package/src/__tests__/makeDataTag.spec.mts +1 -1
- package/src/__tests__/makeInfiniteQueryOptions.spec.mts +1 -2
- package/src/__tests__/makeQueryOptions.spec.mts +1 -1
- package/src/{declare-client.mts → client/declare-client.mts} +93 -33
- package/src/client/index.mts +2 -0
- package/src/{types/client-instance.mts → client/types.mts} +429 -145
- package/src/common/index.mts +1 -0
- package/src/common/types.mts +31 -0
- package/src/index.mts +28 -8
- package/src/mutation/index.mts +3 -0
- package/src/mutation/key-creator.mts +64 -0
- package/src/{make-mutation.mts → mutation/make-hook.mts} +19 -8
- package/src/mutation/types.mts +106 -0
- package/src/query/index.mts +4 -0
- package/src/{utils/query-key-creator.mts → query/key-creator.mts} +22 -48
- package/src/{make-infinite-query-options.mts → query/make-infinite-options.mts} +28 -14
- package/src/{make-query-options.mts → query/make-options.mts} +26 -25
- package/src/query/types.mts +163 -0
- package/dist/tsdown.config.d.mts +0 -3
- package/dist/tsdown.config.d.mts.map +0 -1
- package/dist/tsup.config.d.mts +0 -3
- package/dist/tsup.config.d.mts.map +0 -1
- package/dist/vitest.config.d.mts +0 -3
- package/dist/vitest.config.d.mts.map +0 -1
- package/src/types/client-endpoint-helper.mts +0 -29
- package/src/types/index.mts +0 -7
- package/src/types/mutation-args.mts +0 -10
- package/src/types/mutation-helpers.mts +0 -13
- package/src/types/query-args.mts +0 -8
- package/src/types/query-helpers.mts +0 -34
- package/src/types/query-url-params-args.mts +0 -6
- package/src/types.mts +0 -118
- package/src/utils/mutation-key.creator.mts +0 -65
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AnyEndpointConfig,
|
|
3
|
+
BaseEndpointConfig,
|
|
4
|
+
HttpMethod,
|
|
5
|
+
UrlHasParams,
|
|
6
|
+
UrlParams,
|
|
7
|
+
Util_FlatObject,
|
|
8
|
+
} from '@navios/builder'
|
|
9
|
+
import type {
|
|
10
|
+
DataTag,
|
|
11
|
+
InfiniteData,
|
|
12
|
+
QueryClient,
|
|
13
|
+
UseQueryResult,
|
|
14
|
+
UseSuspenseQueryResult,
|
|
15
|
+
} from '@tanstack/react-query'
|
|
16
|
+
import type { z, ZodObject, ZodType } from 'zod/v4'
|
|
17
|
+
|
|
18
|
+
import type { Split } from '../common/types.mjs'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Arguments for query functions based on URL params and query schema.
|
|
22
|
+
*/
|
|
23
|
+
export type QueryArgs<
|
|
24
|
+
Url extends string = string,
|
|
25
|
+
QuerySchema = ZodObject,
|
|
26
|
+
RequestSchema = undefined,
|
|
27
|
+
> = (UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {}) &
|
|
28
|
+
(QuerySchema extends ZodObject ? { params: z.input<QuerySchema> } : {}) &
|
|
29
|
+
(RequestSchema extends ZodType ? { data: z.input<RequestSchema> } : {})
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Arguments containing only URL params (for invalidateAll operations).
|
|
33
|
+
*/
|
|
34
|
+
export type QueryUrlParamsArgs<Url extends string = string> =
|
|
35
|
+
UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {} | undefined
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Base parameters for query configuration.
|
|
39
|
+
*/
|
|
40
|
+
export type QueryParams<Config extends AnyEndpointConfig, Res = any> = {
|
|
41
|
+
keyPrefix?: string[]
|
|
42
|
+
keySuffix?: string[]
|
|
43
|
+
onFail?: (err: unknown) => void
|
|
44
|
+
processResponse: (data: z.output<Config['responseSchema']>) => Res
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Result type from the query key creator function.
|
|
49
|
+
*/
|
|
50
|
+
export type QueryKeyCreatorResult<
|
|
51
|
+
QuerySchema = undefined,
|
|
52
|
+
Url extends string = string,
|
|
53
|
+
Result = unknown,
|
|
54
|
+
IsInfinite extends boolean = false,
|
|
55
|
+
HasParams extends UrlHasParams<Url> = UrlHasParams<Url>,
|
|
56
|
+
> = {
|
|
57
|
+
template: Split<Url, '/'>
|
|
58
|
+
dataTag: (
|
|
59
|
+
params: (HasParams extends true ? { urlParams: UrlParams<Url> } : {}) &
|
|
60
|
+
(QuerySchema extends ZodObject ? { params: z.input<QuerySchema> } : {}),
|
|
61
|
+
) => DataTag<
|
|
62
|
+
Split<Url, '/'>,
|
|
63
|
+
IsInfinite extends true ? InfiniteData<Result> : Result,
|
|
64
|
+
Error
|
|
65
|
+
>
|
|
66
|
+
filterKey: (
|
|
67
|
+
params: HasParams extends true ? { urlParams: UrlParams<Url> } : {},
|
|
68
|
+
) => DataTag<
|
|
69
|
+
Split<Url, '/'>,
|
|
70
|
+
IsInfinite extends true ? InfiniteData<Result> : Result,
|
|
71
|
+
Error
|
|
72
|
+
>
|
|
73
|
+
bindToUrl: (
|
|
74
|
+
params: (HasParams extends true ? { urlParams: UrlParams<Url> } : {}) &
|
|
75
|
+
(QuerySchema extends ZodObject ? { params: z.infer<QuerySchema> } : {}),
|
|
76
|
+
) => string
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Helper methods attached to query options.
|
|
81
|
+
*/
|
|
82
|
+
export type QueryHelpers<
|
|
83
|
+
Url extends string,
|
|
84
|
+
QuerySchema extends ZodObject | undefined = undefined,
|
|
85
|
+
Result = undefined,
|
|
86
|
+
IsInfinite extends boolean = false,
|
|
87
|
+
RequestSchema extends ZodType | undefined = undefined,
|
|
88
|
+
> = {
|
|
89
|
+
queryKey: QueryKeyCreatorResult<QuerySchema, Url, Result, IsInfinite>
|
|
90
|
+
use: (
|
|
91
|
+
params: Util_FlatObject<QueryArgs<Url, QuerySchema, RequestSchema>>,
|
|
92
|
+
) => UseQueryResult<Result, Error>
|
|
93
|
+
useSuspense: (
|
|
94
|
+
params: Util_FlatObject<QueryArgs<Url, QuerySchema, RequestSchema>>,
|
|
95
|
+
) => UseSuspenseQueryResult<Result, Error>
|
|
96
|
+
invalidate: (
|
|
97
|
+
queryClient: QueryClient,
|
|
98
|
+
params: Util_FlatObject<QueryArgs<Url, QuerySchema, RequestSchema>>,
|
|
99
|
+
) => () => Promise<void>
|
|
100
|
+
invalidateAll: (
|
|
101
|
+
queryClient: QueryClient,
|
|
102
|
+
params: Util_FlatObject<QueryUrlParamsArgs<Url>>,
|
|
103
|
+
) => () => Promise<void>
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Options for infinite query configuration.
|
|
108
|
+
*/
|
|
109
|
+
export type InfiniteQueryOptions<
|
|
110
|
+
Config extends BaseEndpointConfig<HttpMethod, string, ZodObject>,
|
|
111
|
+
Res = any,
|
|
112
|
+
> = {
|
|
113
|
+
keyPrefix?: string[]
|
|
114
|
+
keySuffix?: string[]
|
|
115
|
+
processResponse: (data: z.infer<Config['responseSchema']>) => Res
|
|
116
|
+
onFail?: (err: unknown) => void
|
|
117
|
+
getNextPageParam: (
|
|
118
|
+
lastPage: Res,
|
|
119
|
+
allPages: Res[],
|
|
120
|
+
lastPageParam: z.infer<Config['querySchema']> | undefined,
|
|
121
|
+
allPageParams: z.infer<Config['querySchema']>[] | undefined,
|
|
122
|
+
) =>
|
|
123
|
+
| z.input<Config['querySchema']>
|
|
124
|
+
| z.infer<Config['querySchema']>
|
|
125
|
+
| undefined
|
|
126
|
+
getPreviousPageParam?: (
|
|
127
|
+
firstPage: Res,
|
|
128
|
+
allPages: Res[],
|
|
129
|
+
lastPageParam: z.infer<Config['querySchema']> | undefined,
|
|
130
|
+
allPageParams: z.infer<Config['querySchema']>[] | undefined,
|
|
131
|
+
) => z.input<Config['querySchema']>
|
|
132
|
+
initialPageParam?:
|
|
133
|
+
| z.input<Config['querySchema']>
|
|
134
|
+
| z.infer<Config['querySchema']>
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Legacy type aliases for backwards compatibility
|
|
138
|
+
/** @deprecated Use QueryArgs instead */
|
|
139
|
+
export type ClientQueryArgs<
|
|
140
|
+
Url extends string = string,
|
|
141
|
+
QuerySchema = ZodObject,
|
|
142
|
+
RequestSchema = undefined,
|
|
143
|
+
> = QueryArgs<Url, QuerySchema, RequestSchema>
|
|
144
|
+
|
|
145
|
+
/** @deprecated Use QueryUrlParamsArgs instead */
|
|
146
|
+
export type ClientQueryUrlParamsArgs<Url extends string = string> =
|
|
147
|
+
QueryUrlParamsArgs<Url>
|
|
148
|
+
|
|
149
|
+
/** @deprecated Use QueryParams instead */
|
|
150
|
+
export type BaseQueryParams<
|
|
151
|
+
Config extends AnyEndpointConfig,
|
|
152
|
+
Res = unknown,
|
|
153
|
+
> = QueryParams<Config, Res>
|
|
154
|
+
|
|
155
|
+
/** @deprecated Use QueryArgs instead */
|
|
156
|
+
export type BaseQueryArgs<Config extends AnyEndpointConfig> = (UrlHasParams<
|
|
157
|
+
Config['url']
|
|
158
|
+
> extends true
|
|
159
|
+
? { urlParams: UrlParams<Config['url']> }
|
|
160
|
+
: {}) &
|
|
161
|
+
(Config['querySchema'] extends ZodObject
|
|
162
|
+
? { params: z.input<Config['querySchema']> }
|
|
163
|
+
: {})
|
package/dist/tsdown.config.d.mts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tsdown.config.d.mts","sourceRoot":"","sources":["../tsdown.config.mts"],"names":[],"mappings":";AAEA,wBAQE"}
|
package/dist/tsup.config.d.mts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
declare const _default: import("tsup").Options | import("tsup").Options[] | ((overrideOptions: import("tsup").Options) => import("tsup").Options | import("tsup").Options[] | Promise<import("tsup").Options | import("tsup").Options[]>);
|
|
2
|
-
export default _default;
|
|
3
|
-
//# sourceMappingURL=tsup.config.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tsup.config.d.mts","sourceRoot":"","sources":["../tsup.config.mts"],"names":[],"mappings":";AAEA,wBAQE"}
|
package/dist/vitest.config.d.mts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"vitest.config.d.mts","sourceRoot":"","sources":["../vitest.config.mts"],"names":[],"mappings":";AAEA,wBAME"}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
BaseEndpointConfig,
|
|
3
|
-
EndpointFunctionArgs,
|
|
4
|
-
HttpMethod,
|
|
5
|
-
Util_FlatObject,
|
|
6
|
-
} from '@navios/builder'
|
|
7
|
-
import type { z } from 'zod/v4'
|
|
8
|
-
|
|
9
|
-
export type ClientEndpointHelper<
|
|
10
|
-
Method extends HttpMethod = HttpMethod,
|
|
11
|
-
Url extends string = string,
|
|
12
|
-
RequestSchema = unknown,
|
|
13
|
-
ResponseSchema extends z.ZodType = z.ZodType,
|
|
14
|
-
QuerySchema = unknown,
|
|
15
|
-
> = {
|
|
16
|
-
endpoint: ((
|
|
17
|
-
params: Util_FlatObject<
|
|
18
|
-
EndpointFunctionArgs<Url, QuerySchema, RequestSchema>
|
|
19
|
-
>,
|
|
20
|
-
) => Promise<z.output<ResponseSchema>>) & {
|
|
21
|
-
config: BaseEndpointConfig<
|
|
22
|
-
Method,
|
|
23
|
-
Url,
|
|
24
|
-
QuerySchema,
|
|
25
|
-
ResponseSchema,
|
|
26
|
-
RequestSchema
|
|
27
|
-
>
|
|
28
|
-
}
|
|
29
|
-
}
|
package/src/types/index.mts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export * from './client-endpoint-helper.mjs'
|
|
2
|
-
export * from './client-instance.mjs'
|
|
3
|
-
export * from './mutation-args.mjs'
|
|
4
|
-
export * from './mutation-helpers.mjs'
|
|
5
|
-
export * from './query-args.mjs'
|
|
6
|
-
export * from './query-helpers.mjs'
|
|
7
|
-
export * from './query-url-params-args.mjs'
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { UrlHasParams, UrlParams } from '@navios/builder'
|
|
2
|
-
import type { z, ZodObject } from 'zod/v4'
|
|
3
|
-
|
|
4
|
-
export type ClientMutationArgs<
|
|
5
|
-
Url extends string = string,
|
|
6
|
-
RequestSchema = unknown,
|
|
7
|
-
QuerySchema = unknown,
|
|
8
|
-
> = (UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {}) &
|
|
9
|
-
(RequestSchema extends ZodObject ? { data: z.input<RequestSchema> } : {}) &
|
|
10
|
-
(QuerySchema extends ZodObject ? { params: z.input<QuerySchema> } : {})
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { UrlHasParams, UrlParams } from '@navios/builder'
|
|
2
|
-
import type { DataTag } from '@tanstack/react-query'
|
|
3
|
-
|
|
4
|
-
export type MutationHelpers<Url extends string, Result = unknown> =
|
|
5
|
-
UrlHasParams<Url> extends true
|
|
6
|
-
? {
|
|
7
|
-
mutationKey: (params: UrlParams<Url>) => DataTag<[Url], Result, Error>
|
|
8
|
-
useIsMutating: (keyParams: UrlParams<Url>) => boolean
|
|
9
|
-
}
|
|
10
|
-
: {
|
|
11
|
-
mutationKey: () => DataTag<[Url], Result, Error>
|
|
12
|
-
useIsMutating: () => boolean
|
|
13
|
-
}
|
package/src/types/query-args.mts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { UrlHasParams, UrlParams } from '@navios/builder'
|
|
2
|
-
import type { z, ZodObject } from 'zod/v4'
|
|
3
|
-
|
|
4
|
-
export type ClientQueryArgs<
|
|
5
|
-
Url extends string = string,
|
|
6
|
-
QuerySchema = ZodObject,
|
|
7
|
-
> = (UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {}) &
|
|
8
|
-
(QuerySchema extends ZodObject ? { params: z.input<QuerySchema> } : {})
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { Util_FlatObject } from '@navios/builder'
|
|
2
|
-
import type {
|
|
3
|
-
QueryClient,
|
|
4
|
-
UseQueryResult,
|
|
5
|
-
UseSuspenseQueryResult,
|
|
6
|
-
} from '@tanstack/react-query'
|
|
7
|
-
import type { ZodObject } from 'zod/v4'
|
|
8
|
-
|
|
9
|
-
import type { QueryKeyCreatorResult } from '../utils/query-key-creator.mjs'
|
|
10
|
-
import type { ClientQueryArgs } from './query-args.mjs'
|
|
11
|
-
import type { ClientQueryUrlParamsArgs } from './query-url-params-args.mjs'
|
|
12
|
-
|
|
13
|
-
export type QueryHelpers<
|
|
14
|
-
Url extends string,
|
|
15
|
-
QuerySchema extends ZodObject | undefined = undefined,
|
|
16
|
-
Result = undefined,
|
|
17
|
-
IsInfinite extends boolean = false,
|
|
18
|
-
> = {
|
|
19
|
-
queryKey: QueryKeyCreatorResult<QuerySchema, Url, Result, IsInfinite>
|
|
20
|
-
use: (
|
|
21
|
-
params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>,
|
|
22
|
-
) => UseQueryResult<Result, Error>
|
|
23
|
-
useSuspense: (
|
|
24
|
-
params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>,
|
|
25
|
-
) => UseSuspenseQueryResult<Result, Error>
|
|
26
|
-
invalidate: (
|
|
27
|
-
queryClient: QueryClient,
|
|
28
|
-
params: Util_FlatObject<ClientQueryArgs<Url, QuerySchema>>,
|
|
29
|
-
) => () => Promise<void>
|
|
30
|
-
invalidateAll: (
|
|
31
|
-
queryClient: QueryClient,
|
|
32
|
-
params: Util_FlatObject<ClientQueryUrlParamsArgs<Url>>,
|
|
33
|
-
) => () => Promise<void>
|
|
34
|
-
}
|
package/src/types.mts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AnyEndpointConfig,
|
|
3
|
-
BaseEndpointConfig,
|
|
4
|
-
BuilderInstance,
|
|
5
|
-
HttpMethod,
|
|
6
|
-
NaviosZodRequest,
|
|
7
|
-
UrlHasParams,
|
|
8
|
-
UrlParams,
|
|
9
|
-
} from '@navios/builder'
|
|
10
|
-
import type { QueryClient, UseMutationOptions } from '@tanstack/react-query'
|
|
11
|
-
import type { z, ZodObject } from 'zod/v4'
|
|
12
|
-
|
|
13
|
-
export type Split<S extends string, D extends string> = string extends S
|
|
14
|
-
? string[]
|
|
15
|
-
: S extends ''
|
|
16
|
-
? []
|
|
17
|
-
: S extends `${infer T}${D}${infer U}`
|
|
18
|
-
? [T, ...Split<U, D>]
|
|
19
|
-
: [S]
|
|
20
|
-
|
|
21
|
-
export type ProcessResponseFunction<TData = unknown, TVariables = unknown> = (
|
|
22
|
-
variables: TVariables,
|
|
23
|
-
) => Promise<TData> | TData
|
|
24
|
-
|
|
25
|
-
export type ClientOptions<ProcessResponse = unknown> = {
|
|
26
|
-
api: BuilderInstance
|
|
27
|
-
defaults?: {
|
|
28
|
-
keyPrefix?: string[]
|
|
29
|
-
keySuffix?: string[]
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type BaseQueryParams<Config extends AnyEndpointConfig, Res = any> = {
|
|
34
|
-
keyPrefix?: string[]
|
|
35
|
-
keySuffix?: string[]
|
|
36
|
-
onFail?: (err: unknown) => void
|
|
37
|
-
processResponse: (data: z.output<Config['responseSchema']>) => Res
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface BaseMutationParams<
|
|
41
|
-
Config extends AnyEndpointConfig,
|
|
42
|
-
TData = unknown,
|
|
43
|
-
TVariables = BaseMutationArgs<Config>,
|
|
44
|
-
TResponse = z.output<Config['responseSchema']>,
|
|
45
|
-
TContext = unknown,
|
|
46
|
-
UseKey extends boolean = false,
|
|
47
|
-
> extends Omit<
|
|
48
|
-
UseMutationOptions<TData, Error, TVariables>,
|
|
49
|
-
'mutationKey' | 'mutationFn' | 'onSuccess' | 'onError' | 'scope'
|
|
50
|
-
> {
|
|
51
|
-
processResponse: ProcessResponseFunction<TData, TResponse>
|
|
52
|
-
/**
|
|
53
|
-
* React hooks that will prepare the context for the mutation onSuccess and onError
|
|
54
|
-
* callbacks. This is useful for when you want to use the context in the callbacks
|
|
55
|
-
*/
|
|
56
|
-
useContext?: () => TContext
|
|
57
|
-
onSuccess?: (
|
|
58
|
-
queryClient: QueryClient,
|
|
59
|
-
data: TData,
|
|
60
|
-
variables: TVariables,
|
|
61
|
-
context: TContext,
|
|
62
|
-
) => void | Promise<void>
|
|
63
|
-
onError?: (
|
|
64
|
-
queryClient: QueryClient,
|
|
65
|
-
err: unknown,
|
|
66
|
-
variables: TVariables,
|
|
67
|
-
context: TContext,
|
|
68
|
-
) => void | Promise<void>
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* If true, we will create a mutation key that can be shared across the project.
|
|
72
|
-
*/
|
|
73
|
-
useKey?: UseKey
|
|
74
|
-
keyPrefix?: UseKey extends true
|
|
75
|
-
? UrlHasParams<Config['url']> extends true
|
|
76
|
-
? string[]
|
|
77
|
-
: never
|
|
78
|
-
: never
|
|
79
|
-
keySuffix?: UseKey extends true
|
|
80
|
-
? UrlHasParams<Config['url']> extends true
|
|
81
|
-
? string[]
|
|
82
|
-
: never
|
|
83
|
-
: never
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export type BaseQueryArgs<Config extends AnyEndpointConfig> = (UrlHasParams<
|
|
87
|
-
Config['url']
|
|
88
|
-
> extends true
|
|
89
|
-
? { urlParams: UrlParams<Config['url']> }
|
|
90
|
-
: {}) &
|
|
91
|
-
(Config['querySchema'] extends ZodObject
|
|
92
|
-
? { params: z.input<Config['querySchema']> }
|
|
93
|
-
: {})
|
|
94
|
-
|
|
95
|
-
export type BaseMutationArgs<Config extends AnyEndpointConfig> =
|
|
96
|
-
NaviosZodRequest<Config>
|
|
97
|
-
|
|
98
|
-
export type InfiniteQueryOptions<
|
|
99
|
-
Config extends BaseEndpointConfig<HttpMethod, string, ZodObject>,
|
|
100
|
-
Res = any,
|
|
101
|
-
> = {
|
|
102
|
-
keyPrefix?: string[]
|
|
103
|
-
keySuffix?: string[]
|
|
104
|
-
processResponse: (data: z.infer<Config['responseSchema']>) => Res
|
|
105
|
-
onFail?: (err: unknown) => void
|
|
106
|
-
getNextPageParam: (
|
|
107
|
-
lastPage: Res,
|
|
108
|
-
allPages: Res[],
|
|
109
|
-
lastPageParam: z.infer<Config['querySchema']> | undefined,
|
|
110
|
-
allPageParams: z.infer<Config['querySchema']>[] | undefined,
|
|
111
|
-
) =>
|
|
112
|
-
| z.input<Config['querySchema']>
|
|
113
|
-
| z.infer<Config['querySchema']>
|
|
114
|
-
| undefined
|
|
115
|
-
initialPageParam?:
|
|
116
|
-
| z.input<Config['querySchema']>
|
|
117
|
-
| z.infer<Config['querySchema']>
|
|
118
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AnyEndpointConfig,
|
|
3
|
-
UrlHasParams,
|
|
4
|
-
UrlParams,
|
|
5
|
-
} from '@navios/builder'
|
|
6
|
-
import type { DataTag } from '@tanstack/react-query'
|
|
7
|
-
|
|
8
|
-
import type { BaseQueryParams } from '../types.mjs'
|
|
9
|
-
|
|
10
|
-
import { queryKeyCreator } from './query-key-creator.mjs'
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Creates a mutation key for a given endpoint configuration and options.
|
|
14
|
-
*
|
|
15
|
-
* @param {config: Config } config - The endpoint object containing the configuration.
|
|
16
|
-
* @param {Options} [options] - Optional query parameters with a default `processResponse` function that processes the response data.
|
|
17
|
-
*
|
|
18
|
-
* @returns {Object} An object containing the `mutationKey` function.
|
|
19
|
-
*
|
|
20
|
-
* The `mutationKey` function generates a mutation key based on the provided parameters:
|
|
21
|
-
* - If the URL has parameters (`HasParams` is `true`), it expects an object with `urlParams`.
|
|
22
|
-
* - The return type of the `mutationKey` function depends on the `processResponse` function in `options`.
|
|
23
|
-
* If `processResponse` is defined, the return type is a `DataTag` containing the processed result and an error type.
|
|
24
|
-
*
|
|
25
|
-
* @example Example usage:
|
|
26
|
-
* ```typescript
|
|
27
|
-
* const createMutationKey = mutationKeyCreator(endpoint.config);
|
|
28
|
-
* const mutationKey = createMutationKey({ urlParams: { id: 123 } });
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* @example Advanced usage:
|
|
32
|
-
* ```ts
|
|
33
|
-
* const createMutationKey = mutationKeyCreator(endpoint.config, {
|
|
34
|
-
* processResponse: (data) => {
|
|
35
|
-
* if (!data.success) {
|
|
36
|
-
* throw new Error(data.message);
|
|
37
|
-
* }
|
|
38
|
-
* return data.data;
|
|
39
|
-
* },
|
|
40
|
-
* });
|
|
41
|
-
* // We create a mutation that will be shared across the project for all passed userId
|
|
42
|
-
* const mutationKey = createMutationKey({ urlParams: { projectId: 123, userId: 'wildcard' } });
|
|
43
|
-
*/
|
|
44
|
-
export function mutationKeyCreator<
|
|
45
|
-
Config extends AnyEndpointConfig,
|
|
46
|
-
Options extends BaseQueryParams<Config>,
|
|
47
|
-
Url extends Config['url'] = Config['url'],
|
|
48
|
-
HasParams extends UrlHasParams<Url> = UrlHasParams<Url>,
|
|
49
|
-
>(
|
|
50
|
-
config: Config,
|
|
51
|
-
options: Options = {
|
|
52
|
-
processResponse: (data) => data,
|
|
53
|
-
} as Options,
|
|
54
|
-
): (
|
|
55
|
-
params: HasParams extends true ? { urlParams: UrlParams<Url> } : {},
|
|
56
|
-
) => Options['processResponse'] extends (...args: any[]) => infer Result
|
|
57
|
-
? DataTag<[Config['url']], Result, Error>
|
|
58
|
-
: never {
|
|
59
|
-
const queryKey = queryKeyCreator(config, options, false)
|
|
60
|
-
|
|
61
|
-
// @ts-expect-error We have correct types in return type
|
|
62
|
-
return (params) => {
|
|
63
|
-
return queryKey.filterKey(params)
|
|
64
|
-
}
|
|
65
|
-
}
|