@@ -79,6 +79,59 @@ export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod,
79
79
}
80
80
> ;
81
81
82
+ // Helper type to infer TPageParam type
83
+ type InferPageParamType < T > = T extends { initialPageParam : infer P } ? P : unknown ;
84
+
85
+ export type InfiniteQueryOptionsFunction <
86
+ Paths extends Record < string , Record < HttpMethod , { } > > ,
87
+ Media extends MediaType ,
88
+ > = <
89
+ Method extends HttpMethod ,
90
+ Path extends PathsWithMethod < Paths , Method > ,
91
+ Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
92
+ Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > ,
93
+ Options extends Omit <
94
+ UseInfiniteQueryOptions <
95
+ Response [ "data" ] ,
96
+ Response [ "error" ] ,
97
+ InferSelectReturnType < InfiniteData < Response [ "data" ] > , Options [ "select" ] > ,
98
+ QueryKey < Paths , Method , Path > ,
99
+ InferPageParamType < Options >
100
+ > ,
101
+ "queryKey" | "queryFn"
102
+ > & {
103
+ pageParamName ?: string ;
104
+ initialPageParam : InferPageParamType < Options > ;
105
+ } ,
106
+ > (
107
+ method : Method ,
108
+ path : Path ,
109
+ init : InitWithUnknowns < Init > ,
110
+ options : Options ,
111
+ ) => NoInfer <
112
+ Omit <
113
+ UseInfiniteQueryOptions <
114
+ Response [ "data" ] ,
115
+ Response [ "error" ] ,
116
+ InferSelectReturnType < InfiniteData < Response [ "data" ] > , Options [ "select" ] > ,
117
+ QueryKey < Paths , Method , Path > ,
118
+ InferPageParamType < Options >
119
+ > ,
120
+ "queryFn"
121
+ > & {
122
+ queryFn : Exclude <
123
+ UseInfiniteQueryOptions <
124
+ Response [ "data" ] ,
125
+ Response [ "error" ] ,
126
+ InferSelectReturnType < InfiniteData < Response [ "data" ] > , Options [ "select" ] > ,
127
+ QueryKey < Paths , Method , Path > ,
128
+ InferPageParamType < Options >
129
+ > [ "queryFn" ] ,
130
+ SkipToken | undefined
131
+ > ;
132
+ }
133
+ > ;
134
+
82
135
export type UseQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
83
136
Method extends HttpMethod ,
84
137
Path extends PathsWithMethod < Paths , Method > ,
@@ -166,6 +219,7 @@ export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}
166
219
167
220
export interface OpenapiQueryClient < Paths extends { } , Media extends MediaType = MediaType > {
168
221
queryOptions : QueryOptionsFunction < Paths , Media > ;
222
+ infiniteQueryOptions : InfiniteQueryOptionsFunction < Paths , Media > ;
169
223
useQuery : UseQueryMethod < Paths , Media > ;
170
224
useSuspenseQuery : UseSuspenseQueryMethod < Paths , Media > ;
171
225
useInfiniteQuery : UseInfiniteQueryMethod < Paths , Media > ;
@@ -214,44 +268,47 @@ export default function createClient<Paths extends {}, Media extends MediaType =
214
268
...options ,
215
269
} ) ;
216
270
271
+ const infiniteQueryOptions : InfiniteQueryOptionsFunction < Paths , Media > = ( method , path , init , options ) => {
272
+ const { pageParamName = "cursor" , initialPageParam, ...restOptions } = options ;
273
+ const { queryKey } = queryOptions ( method , path , init ) ;
274
+
275
+ return {
276
+ queryKey,
277
+ initialPageParam,
278
+ queryFn : async ( { queryKey : [ method , path , init ] , pageParam, signal } ) => {
279
+ const mth = method . toUpperCase ( ) as Uppercase < typeof method > ;
280
+ const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
281
+ const mergedInit = {
282
+ ...init ,
283
+ signal,
284
+ params : {
285
+ ...( init ?. params || { } ) ,
286
+ query : {
287
+ ...( init ?. params as { query ?: DefaultParamsOption } ) ?. query ,
288
+ [ pageParamName ] : pageParam ,
289
+ } ,
290
+ } ,
291
+ } ;
292
+
293
+ const { data, error } = await fn ( path , mergedInit as any ) ;
294
+ if ( error ) {
295
+ throw error ;
296
+ }
297
+ return data ;
298
+ } ,
299
+ ...restOptions ,
300
+ } ;
301
+ } ;
302
+
217
303
return {
218
304
queryOptions,
305
+ infiniteQueryOptions,
219
306
useQuery : ( method , path , ...[ init , options , queryClient ] ) =>
220
307
useQuery ( queryOptions ( method , path , init as InitWithUnknowns < typeof init > , options ) , queryClient ) ,
221
308
useSuspenseQuery : ( method , path , ...[ init , options , queryClient ] ) =>
222
309
useSuspenseQuery ( queryOptions ( method , path , init as InitWithUnknowns < typeof init > , options ) , queryClient ) ,
223
- useInfiniteQuery : ( method , path , init , options , queryClient ) => {
224
- const { pageParamName = "cursor" , ...restOptions } = options ;
225
- const { queryKey } = queryOptions ( method , path , init ) ;
226
- return useInfiniteQuery (
227
- {
228
- queryKey,
229
- queryFn : async ( { queryKey : [ method , path , init ] , pageParam = 0 , signal } ) => {
230
- const mth = method . toUpperCase ( ) as Uppercase < typeof method > ;
231
- const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
232
- const mergedInit = {
233
- ...init ,
234
- signal,
235
- params : {
236
- ...( init ?. params || { } ) ,
237
- query : {
238
- ...( init ?. params as { query ?: DefaultParamsOption } ) ?. query ,
239
- [ pageParamName ] : pageParam ,
240
- } ,
241
- } ,
242
- } ;
243
-
244
- const { data, error } = await fn ( path , mergedInit as any ) ;
245
- if ( error ) {
246
- throw error ;
247
- }
248
- return data ;
249
- } ,
250
- ...restOptions ,
251
- } ,
252
- queryClient ,
253
- ) ;
254
- } ,
310
+ useInfiniteQuery : ( method , path , init , options , queryClient ) =>
311
+ useInfiniteQuery ( infiniteQueryOptions ( method , path , init , options ) , queryClient ) ,
255
312
useMutation : ( method , path , options , queryClient ) =>
256
313
useMutation (
257
314
{
0 commit comments