@@ -11,10 +11,13 @@ import {
11
11
type QueryClient ,
12
12
type QueryFunctionContext ,
13
13
type SkipToken ,
14
+ useInfiniteQuery ,
15
+ type DataTag ,
14
16
useMutation ,
15
17
useQuery ,
16
- useSuspenseQuery ,
17
- useInfiniteQuery ,
18
+ useSuspenseQuery ,
19
+ dataTagSymbol ,
20
+ dataTagErrorSymbol ,
18
21
} from "@tanstack/react-query" ;
19
22
import type {
20
23
ClientMethod ,
@@ -34,8 +37,12 @@ export type QueryKey<
34
37
Paths extends Record < string , Record < HttpMethod , { } > > ,
35
38
Method extends HttpMethod ,
36
39
Path extends PathsWithMethod < Paths , Method > ,
37
- Init = MaybeOptionalInit < Paths [ Path ] , Method > ,
38
- > = Init extends undefined ? readonly [ Method , Path ] : readonly [ Method , Path , Init ] ;
40
+ Media extends MediaType ,
41
+ Init extends MaybeOptionalInit < Paths [ Path ] , Method > = MaybeOptionalInit < Paths [ Path ] , Method > ,
42
+ Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > = Required <
43
+ FetchResponse < Paths [ Path ] [ Method ] , Init , Media >
44
+ > ,
45
+ > = DataTag < readonly [ Method , Path , Init ] , Response [ "data" ] >
39
46
40
47
export type QueryOptionsFunction < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
41
48
Method extends HttpMethod ,
@@ -47,7 +54,7 @@ export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod,
47
54
Response [ "data" ] ,
48
55
Response [ "error" ] ,
49
56
InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > ,
50
- QueryKey < Paths , Method , Path >
57
+ QueryKey < Paths , Method , Path , Media >
51
58
> ,
52
59
"queryKey" | "queryFn"
53
60
> ,
@@ -63,7 +70,7 @@ export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod,
63
70
Response [ "data" ] ,
64
71
Response [ "error" ] ,
65
72
InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > ,
66
- QueryKey < Paths , Method , Path >
73
+ QueryKey < Paths , Method , Path , Media >
67
74
> ,
68
75
"queryFn"
69
76
> & {
@@ -72,7 +79,7 @@ export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod,
72
79
Response [ "data" ] ,
73
80
Response [ "error" ] ,
74
81
InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > ,
75
- QueryKey < Paths , Method , Path >
82
+ QueryKey < Paths , Method , Path , Media >
76
83
> [ "queryFn" ] ,
77
84
SkipToken | undefined
78
85
> ;
@@ -89,7 +96,7 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
89
96
Response [ "data" ] ,
90
97
Response [ "error" ] ,
91
98
InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > ,
92
- QueryKey < Paths , Method , Path >
99
+ QueryKey < Paths , Method , Path , Media >
93
100
> ,
94
101
"queryKey" | "queryFn"
95
102
> ,
@@ -112,7 +119,7 @@ export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMetho
112
119
Response [ "error" ] ,
113
120
InfiniteData < Response [ "data" ] > ,
114
121
Response [ "data" ] ,
115
- QueryKey < Paths , Method , Path > ,
122
+ QueryKey < Paths , Method , Path , Media > ,
116
123
unknown
117
124
> ,
118
125
"queryKey" | "queryFn"
@@ -137,7 +144,7 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
137
144
Response [ "data" ] ,
138
145
Response [ "error" ] ,
139
146
InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > ,
140
- QueryKey < Paths , Method , Path >
147
+ QueryKey < Paths , Method , Path , Media >
141
148
> ,
142
149
"queryKey" | "queryFn"
143
150
> ,
@@ -188,7 +195,7 @@ export default function createClient<Paths extends {}, Media extends MediaType =
188
195
const queryFn = async < Method extends HttpMethod , Path extends PathsWithMethod < Paths , Method > > ( {
189
196
queryKey : [ method , path , init ] ,
190
197
signal,
191
- } : QueryFunctionContext < QueryKey < Paths , Method , Path > > ) => {
198
+ } : QueryFunctionContext < QueryKey < Paths , Method , Path , Media > > ) => {
192
199
const mth = method . toUpperCase ( ) as Uppercase < typeof method > ;
193
200
const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
194
201
const { data, error } = await fn ( path , { signal, ...( init as any ) } ) ; // TODO: find a way to avoid as any
@@ -200,11 +207,15 @@ export default function createClient<Paths extends {}, Media extends MediaType =
200
207
} ;
201
208
202
209
const queryOptions : QueryOptionsFunction < Paths , Media > = ( method , path , ...[ init , options ] ) => ( {
203
- queryKey : ( init === undefined ? ( [ method , path ] as const ) : ( [ method , path , init ] as const ) ) as QueryKey <
204
- Paths ,
205
- typeof method ,
206
- typeof path
207
- > ,
210
+ queryKey : Object . assign ( init === undefined ? [ method , path ] as const : [ method , path , init ] as const , {
211
+ [ dataTagSymbol ] : { } as any ,
212
+ [ dataTagErrorSymbol ] : { } as any ,
213
+ } ) as QueryKey <
214
+ Paths ,
215
+ typeof method ,
216
+ typeof path ,
217
+ Media
218
+ > ,
208
219
queryFn,
209
220
...options ,
210
221
} ) ;
0 commit comments