@7nohe/openapi-react-query-codegen
Version:
OpenAPI React Query Codegen
30 lines (29 loc) • 1.95 kB
text/typescript
import { type VariableStatementStructure } from "ts-morph";
import type { GenerationContext, OperationInfo } from "../types.mjs";
/**
* Build a queryOptions factory for a GET operation.
* The factory centralizes queryKey and queryFn so they can be reused with
* every TanStack Query utility (useQuery, useQueries, prefetchQuery,
* ensureQueryData, setQueryData, ...) with full type safety.
* Example:
* export const findPetsOptions = (clientOptions: Options<FindPetsData, true> = {}, queryKey?: Array<unknown>) =>
* queryOptions({
* queryKey: Common.UseFindPetsKeyFn(clientOptions, queryKey),
* queryFn: () => findPets({ ...clientOptions, throwOnError: true }).then(response => response.data),
* });
*/
export declare function buildQueryOptionsFn(op: OperationInfo): VariableStatementStructure;
/**
* Build an infiniteQueryOptions factory for a paginatable GET operation.
* Uses the dedicated infinite query key and page-less options type.
* Example:
* export const findPaginatedPetsInfiniteOptions = (clientOptions: Common.FindPaginatedPetsInfiniteClientOptions = {}, queryKey?: Array<unknown>, options?: Partial<Pick<UseInfiniteQueryOptions<NonNullable<Common.FindPaginatedPetsDefaultResponse>>, "initialPageParam" | "getNextPageParam">>) =>
* infiniteQueryOptions({
* queryKey: Common.UseFindPaginatedPetsInfiniteKeyFn(clientOptions, queryKey),
* queryFn: ({ pageParam }) => findPaginatedPets({ ...clientOptions, query: { ...clientOptions.query, page: pageParam as number }, throwOnError: true } as Options<FindPaginatedPetsData, true>).then(response => response.data as NonNullable<Common.FindPaginatedPetsDefaultResponse>),
* initialPageParam: 1,
* getNextPageParam: (response: unknown) => (response as { nextPage: number }).nextPage,
* ...options,
* });
*/
export declare function buildInfiniteQueryOptionsFn(op: OperationInfo, ctx: GenerationContext): VariableStatementStructure | null;