Skip to content

Add support for generating useInfiniteQuery custom hooks #117

Open
@7nohe

Description

@7nohe

Is your feature request related to a problem? Please describe.
Currently, this library does not support generating custom hooks for useInfiniteQuery. This limitation makes it difficult to handle APIs that require pagination or infinite scrolling, leading to additional manual coding and reduced efficiency.

Describe the solution you'd like
I would like the library to support the generation of useInfiniteQuery custom hooks directly from OpenAPI schemas. This feature should automatically create hooks that handle pagination parameters and fetch data incrementally as needed, making it easier for developers to integrate infinite scrolling functionality into their applications. Specifically, the solution would involve adding CLI options --next-page-param and --page-param to specify the relevant parameters. If the API has query parameters and responses that match these options, the library should automatically generate useInfiniteQuery custom hooks.

Here is an example of a generated custom hook:

export const useInfiniteDefaultServiceFindPets = <
  TData = Common.DefaultServiceFindPetsDefaultResponse,
  TError = unknown,
  TQueryKey extends Array<unknown> = unknown[]
>(
  {
    limit,
    tags,
  }: {
    limit?: number;
    tags?: string[];
  } = {},
  queryKey?: TQueryKey,
  options?: Omit<UseInfiniteQueryOptions<TData, TError>, "queryKey" | "queryFn">
) =>
  useInfiniteQuery({
    queryKey: Common.UseDefaultServiceFindPetsKeyFn({ limit, tags }, queryKey),
    queryFn: (({ pageParam }) => DefaultService.findPets({ limit, tags, page: pageParam as number })) as QueryFunction<TData, QueryKey, unknown>,
    initialPageParam: 1,
    getNextPageParam: (response) => (response as { nextPage: number }).nextPage,
    ...options,
  });

Additional context
Here is the documentation for useInfiniteQuery for reference.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions