@@ -17,7 +17,13 @@ import {
17
17
useSuspenseQuery ,
18
18
useInfiniteQuery ,
19
19
} from "@tanstack/react-query" ;
20
- import type { ClientMethod , FetchResponse , MaybeOptionalInit , Client as FetchClient , DefaultParamsOption } from "openapi-fetch" ;
20
+ import type {
21
+ ClientMethod ,
22
+ FetchResponse ,
23
+ MaybeOptionalInit ,
24
+ Client as FetchClient ,
25
+ DefaultParamsOption ,
26
+ } from "openapi-fetch" ;
21
27
import type { HttpMethod , MediaType , PathsWithMethod , RequiredKeysOf } from "openapi-typescript-helpers" ;
22
28
23
29
// Helper type to dynamically infer the type from the `select` property
@@ -95,33 +101,29 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
95
101
: [ InitWithUnknowns < Init > , Options ?, QueryClient ?]
96
102
) => UseQueryResult < InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > , Response [ "error" ] > ;
97
103
98
- export type UseInfiniteQueryMethod <
99
- Paths extends Record < string , Record < HttpMethod , { } > > ,
100
- Media extends MediaType
101
- > =
102
- ( <
103
- Method extends HttpMethod ,
104
- Path extends PathsWithMethod < Paths , Method > ,
105
- Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
106
- Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > ,
107
- Options extends Omit <
108
- UseInfiniteQueryOptions <
109
- Response [ "data" ] ,
110
- Response [ "error" ] ,
111
- InfiniteData < Response [ "data" ] > ,
112
- Response [ "data" ] ,
113
- QueryKey < Paths , Method , Path > ,
114
- unknown
115
- > ,
116
- "queryKey" | "queryFn"
117
- >
118
- > (
119
- method : Method ,
120
- url : Path ,
121
- init : InitWithUnknowns < Init > ,
122
- options : Options ,
123
- queryClient ?: QueryClient
124
- ) => UseInfiniteQueryResult < InfiniteData < Response [ "data" ] > , Response [ "error" ] > ) ;
104
+ export type UseInfiniteQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
105
+ Method extends HttpMethod ,
106
+ Path extends PathsWithMethod < Paths , Method > ,
107
+ Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
108
+ Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > ,
109
+ Options extends Omit <
110
+ UseInfiniteQueryOptions <
111
+ Response [ "data" ] ,
112
+ Response [ "error" ] ,
113
+ InfiniteData < Response [ "data" ] > ,
114
+ Response [ "data" ] ,
115
+ QueryKey < Paths , Method , Path > ,
116
+ unknown
117
+ > ,
118
+ "queryKey" | "queryFn"
119
+ > ,
120
+ > (
121
+ method : Method ,
122
+ url : Path ,
123
+ init : InitWithUnknowns < Init > ,
124
+ options : Options ,
125
+ queryClient ?: QueryClient ,
126
+ ) => UseInfiniteQueryResult < InfiniteData < Response [ "data" ] > , Response [ "error" ] > ;
125
127
126
128
export type UseSuspenseQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
127
129
Method extends HttpMethod ,
@@ -197,41 +199,38 @@ export default function createClient<Paths extends {}, Media extends MediaType =
197
199
useSuspenseQuery : ( method , path , ...[ init , options , queryClient ] ) =>
198
200
useSuspenseQuery ( queryOptions ( method , path , init as InitWithUnknowns < typeof init > , options ) , queryClient ) ,
199
201
useInfiniteQuery : ( method , path , init , options , queryClient ) =>
200
- useInfiniteQuery (
201
- {
202
- queryKey : [ method , path , init ] as const ,
203
- queryFn : async <
204
- Method extends HttpMethod ,
205
- Path extends PathsWithMethod < Paths , Method > ,
206
- > ( {
207
- queryKey : [ method , path , init ] ,
208
- pageParam = 0 ,
209
- signal,
210
- } : QueryFunctionContext < QueryKey < Paths , Method , Path > , unknown > ) => {
211
- const mth = method . toUpperCase ( ) as Uppercase < typeof method > ;
212
- const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
213
- const mergedInit = {
214
- ...init ,
215
- signal,
216
- params : {
217
- ...( init ?. params || { } ) ,
218
- query : {
219
- ...( init ?. params as { query ?: DefaultParamsOption } ) ?. query ,
220
- cursor : pageParam ,
221
- } ,
222
- } ,
223
- } ;
224
-
225
- const { data, error } = await fn ( path , mergedInit as any ) ;
226
- if ( error ) {
227
- throw error ;
228
- }
229
- return data ;
202
+ useInfiniteQuery (
203
+ {
204
+ queryKey : [ method , path , init ] as const ,
205
+ queryFn : async < Method extends HttpMethod , Path extends PathsWithMethod < Paths , Method > > ( {
206
+ queryKey : [ method , path , init ] ,
207
+ pageParam = 0 ,
208
+ signal,
209
+ } : QueryFunctionContext < QueryKey < Paths , Method , Path > , unknown > ) => {
210
+ const mth = method . toUpperCase ( ) as Uppercase < typeof method > ;
211
+ const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
212
+ const mergedInit = {
213
+ ...init ,
214
+ signal,
215
+ params : {
216
+ ...( init ?. params || { } ) ,
217
+ query : {
218
+ ...( init ?. params as { query ?: DefaultParamsOption } ) ?. query ,
219
+ cursor : pageParam ,
220
+ } ,
230
221
} ,
231
- ...options ,
232
- } ,
233
- queryClient ,
234
- ) ,
222
+ } ;
223
+
224
+ const { data, error } = await fn ( path , mergedInit as any ) ;
225
+ if ( error ) {
226
+ throw error ;
227
+ }
228
+ return data ;
229
+ } ,
230
+ ...options ,
231
+ } ,
232
+ queryClient ,
233
+ ) ,
235
234
useMutation : ( method , path , options , queryClient ) =>
236
235
useMutation (
237
236
{
@@ -252,4 +251,3 @@ export default function createClient<Paths extends {}, Media extends MediaType =
252
251
) ,
253
252
} ;
254
253
}
255
-
0 commit comments