@@ -79,6 +79,59 @@ export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod,
7979 }
8080> ;
8181
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+
82135export type UseQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
83136 Method extends HttpMethod ,
84137 Path extends PathsWithMethod < Paths , Method > ,
@@ -167,6 +220,7 @@ export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}
167220
168221export interface OpenapiQueryClient < Paths extends { } , Media extends MediaType = MediaType > {
169222 queryOptions : QueryOptionsFunction < Paths , Media > ;
223+ infiniteQueryOptions : InfiniteQueryOptionsFunction < Paths , Media > ;
170224 useQuery : UseQueryMethod < Paths , Media > ;
171225 useSuspenseQuery : UseSuspenseQueryMethod < Paths , Media > ;
172226 useInfiniteQuery : UseInfiniteQueryMethod < Paths , Media > ;
@@ -215,44 +269,47 @@ export default function createClient<Paths extends {}, Media extends MediaType =
215269 ...options ,
216270 } ) ;
217271
272+ const infiniteQueryOptions : InfiniteQueryOptionsFunction < Paths , Media > = ( method , path , init , options ) => {
273+ const { pageParamName = "cursor" , initialPageParam, ...restOptions } = options ;
274+ const { queryKey } = queryOptions ( method , path , init ) ;
275+
276+ return {
277+ queryKey,
278+ initialPageParam,
279+ queryFn : async ( { queryKey : [ method , path , init ] , pageParam, signal } ) => {
280+ const mth = method . toUpperCase ( ) as Uppercase < typeof method > ;
281+ const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
282+ const mergedInit = {
283+ ...init ,
284+ signal,
285+ params : {
286+ ...( init ?. params || { } ) ,
287+ query : {
288+ ...( init ?. params as { query ?: DefaultParamsOption } ) ?. query ,
289+ [ pageParamName ] : pageParam ,
290+ } ,
291+ } ,
292+ } ;
293+
294+ const { data, error } = await fn ( path , mergedInit as any ) ;
295+ if ( error ) {
296+ throw error ;
297+ }
298+ return data ;
299+ } ,
300+ ...restOptions ,
301+ } ;
302+ } ;
303+
218304 return {
219305 queryOptions,
306+ infiniteQueryOptions,
220307 useQuery : ( method , path , ...[ init , options , queryClient ] ) =>
221308 useQuery ( queryOptions ( method , path , init as InitWithUnknowns < typeof init > , options ) , queryClient ) ,
222309 useSuspenseQuery : ( method , path , ...[ init , options , queryClient ] ) =>
223310 useSuspenseQuery ( queryOptions ( method , path , init as InitWithUnknowns < typeof init > , options ) , queryClient ) ,
224- useInfiniteQuery : ( method , path , init , options , queryClient ) => {
225- const { pageParamName = "cursor" , ...restOptions } = options ;
226- const { queryKey } = queryOptions ( method , path , init ) ;
227- return useInfiniteQuery (
228- {
229- queryKey,
230- queryFn : async ( { queryKey : [ method , path , init ] , pageParam = 0 , signal } ) => {
231- const mth = method . toUpperCase ( ) as Uppercase < typeof method > ;
232- const fn = client [ mth ] as ClientMethod < Paths , typeof method , Media > ;
233- const mergedInit = {
234- ...init ,
235- signal,
236- params : {
237- ...( init ?. params || { } ) ,
238- query : {
239- ...( init ?. params as { query ?: DefaultParamsOption } ) ?. query ,
240- [ pageParamName ] : pageParam ,
241- } ,
242- } ,
243- } ;
244-
245- const { data, error } = await fn ( path , mergedInit as any ) ;
246- if ( error ) {
247- throw error ;
248- }
249- return data ;
250- } ,
251- ...restOptions ,
252- } ,
253- queryClient ,
254- ) ;
255- } ,
311+ useInfiniteQuery : ( method , path , init , options , queryClient ) =>
312+ useInfiniteQuery ( infiniteQueryOptions ( method , path , init , options ) , queryClient ) ,
256313 useMutation : ( method , path , options , queryClient ) =>
257314 useMutation (
258315 {
0 commit comments