diff --git a/packages/openapi-react-query/src/index.ts b/packages/openapi-react-query/src/index.ts index 0ca534c34..beccc929d 100644 --- a/packages/openapi-react-query/src/index.ts +++ b/packages/openapi-react-query/src/index.ts @@ -101,6 +101,9 @@ export type UseQueryMethod>, : [InitWithUnknowns, Options?, QueryClient?] ) => UseQueryResult, Response["error"]>; +// Helper type to infer TPageParam type +type InferPageParamType = T extends { initialPageParam: infer P } ? P : unknown; + export type UseInfiniteQueryMethod>, Media extends MediaType> = < Method extends HttpMethod, Path extends PathsWithMethod, @@ -111,13 +114,13 @@ export type UseInfiniteQueryMethod, Options["select"]>, - Response["data"], QueryKey, - unknown + InferPageParamType >, "queryKey" | "queryFn" > & { pageParamName?: string; + initialPageParam: InferPageParamType; }, >( method: Method, @@ -194,7 +197,10 @@ export default function createClient>) => { const mth = method.toUpperCase() as Uppercase; const fn = client[mth] as ClientMethod; - const { data, error, response } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any + const { data, error, response } = await fn(path, { + signal, + ...(init as any), + }); // TODO: find a way to avoid as any if (error) { throw error; } @@ -222,12 +228,14 @@ export default function createClient useSuspenseQuery(queryOptions(method, path, init as InitWithUnknowns, options), queryClient), useInfiniteQuery: (method, path, init, options, queryClient) => { - const { pageParamName = "cursor", ...restOptions } = options; + const { pageParamName = "cursor", initialPageParam, ...restOptions } = options; const { queryKey } = queryOptions(method, path, init); + return useInfiniteQuery( { queryKey, - queryFn: async ({ queryKey: [method, path, init], pageParam = 0, signal }) => { + initialPageParam, + queryFn: async ({ queryKey: [method, path, init], pageParam, signal }) => { const mth = method.toUpperCase() as Uppercase; const fn = client[mth] as ClientMethod; const mergedInit = {