@@ -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 > ,
@@ -166,6 +219,7 @@ export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}
166219
167220export interface OpenapiQueryClient < Paths extends { } , Media extends MediaType = MediaType > {
168221 queryOptions : QueryOptionsFunction < Paths , Media > ;
222+ infiniteQueryOptions : InfiniteQueryOptionsFunction < Paths , Media > ;
169223 useQuery : UseQueryMethod < Paths , Media > ;
170224 useSuspenseQuery : UseSuspenseQueryMethod < Paths , Media > ;
171225 useInfiniteQuery : UseInfiniteQueryMethod < Paths , Media > ;
@@ -214,44 +268,47 @@ export default function createClient<Paths extends {}, Media extends MediaType =
214268 ...options ,
215269 } ) ;
216270
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+
217303 return {
218304 queryOptions,
305+ infiniteQueryOptions,
219306 useQuery : ( method , path , ...[ init , options , queryClient ] ) =>
220307 useQuery ( queryOptions ( method , path , init as InitWithUnknowns < typeof init > , options ) , queryClient ) ,
221308 useSuspenseQuery : ( method , path , ...[ init , options , queryClient ] ) =>
222309 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 ) ,
255312 useMutation : ( method , path , options , queryClient ) =>
256313 useMutation (
257314 {
0 commit comments