@7nohe/openapi-react-query-codegen
Version:
OpenAPI React Query Codegen
130 lines (129 loc) • 7.35 kB
text/typescript
import { type VariableStatementStructure } from "ts-morph";
import type { GenerationContext, OperationInfo } from "../types.mjs";
/**
* SDK call arguments shared by every generated queryFn/mutationFn.
* throwOnError: true forces the SDK call to reject on error responses; the
* hey-api runtime default is false, which would resolve undefined data and
* swallow the error instead of surfacing it to TanStack Query (#172).
*/
export declare const SDK_CALL_ARGS = "{ ...clientOptions, throwOnError: true }";
/** SDK call arguments for TanStack query functions with cancellation. */
export declare const QUERY_SDK_CALL_ARGS = "{ ...clientOptions, signal, throwOnError: true }";
/** Resolve the OpenAPI page parameter type, preserving older numeric output. */
export declare function getPageParamType(op: OperationInfo): string;
/**
* Build the client options parameter string.
*/
export declare function buildClientOptionsParam(op: OperationInfo): string;
/**
* Build the clientOptions parameter typed with the page-less infinite
* options type — the page parameter is supplied by TanStack Query's
* pageParam mechanism.
*/
export declare function buildInfiniteClientOptionsParam(op: OperationInfo): string;
/**
* The type of a single page. TanStack Query instantiates the infinite options
* with this as TQueryFnData, so it is what `getNextPageParam` receives as
* `lastPage` (#203). NonNullable because `throwOnError: true` means a resolved
* page is always present.
*/
export declare function getPageType(op: OperationInfo): string;
/**
* Build the paginated SDK call shared by every infinite query builder.
* The resolved response is narrowed to the page type so it matches the
* TQueryFnData the infinite options are instantiated with (#203). Spelled as
* a cast rather than `!` because generated code is linted downstream, and a
* non-null assertion trips biome's noNonNullAssertion.
*/
export declare function buildPagedQueryFn(op: OperationInfo, ctx: GenerationContext): string;
/**
* Format the initialPageParam literal. Emits `undefined` when the caller opted
* to omit it (#177); otherwise a numeric literal when possible so the inferred
* pageParam type matches what getNextPageParam returns.
*/
export declare function formatInitialPageParam(ctx: GenerationContext, op?: OperationInfo): string;
/**
* Build the nested type for getNextPageParam.
* E.g., "meta.next" becomes "{ meta: { next: number } }"
*/
export declare function buildNestedNextPageType(nextPageParam: string, pageParamType?: string): string;
/**
* Build the getNextPageParam expression. The parameter is annotated because
* not every TanStack entry point contextually types it (prefetchInfiniteQuery
* does not, which would fail noImplicitAny).
*/
export declare function buildGetNextPageParamExpr(ctx: GenerationContext, op?: OperationInfo): string;
/**
* Build an options type where the pagination fields TanStack Query marks as
* required become optional overrides: the generator supplies them, and
* callers may replace them for custom pagination schemes (#156, #146).
*
* The first type argument is TQueryFnData — a single page — not TData (#203).
* Passing TData there made `getNextPageParam` receive the aggregated
* `InfiniteData<...>` as `lastPage`, so any custom pagination logic failed to
* compile. Only the first three type arguments are written: TanStack Query
* dropped TQueryData from `UseInfiniteQueryOptions` within the v5 line, so
* positions beyond TData are not stable across the supported peer range.
*/
export declare function buildOverridableInfiniteOptionsType(optionsTypeName: string, pageType: string): string;
/**
* Build useQuery hook.
* Example:
* export const useFindPets = <TData = Common.FindPetsDefaultResponse, TError = FindPetsError, TQueryKey extends Array<unknown> = unknown[]>(
* clientOptions: Options<FindPetsData, true> = {},
* queryKey?: TQueryKey,
* options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">
* ) => useQuery<TData, TError>({
* queryKey: Common.UseFindPetsKeyFn(clientOptions, queryKey),
* queryFn: () => findPets({ ...clientOptions, throwOnError: true }).then(response => response.data as TData) as TData,
* ...options
* });
*/
export declare function buildUseQueryHook(op: OperationInfo, ctx: GenerationContext): VariableStatementStructure;
/**
* Build useSuspenseQuery hook.
*/
export declare function buildUseSuspenseQueryHook(op: OperationInfo, ctx: GenerationContext): VariableStatementStructure;
/**
* Build useInfiniteQuery hook.
*/
export declare function buildUseInfiniteQueryHook(op: OperationInfo, ctx: GenerationContext): VariableStatementStructure | null;
/**
* Build useSuspenseInfiniteQuery hook.
*/
export declare function buildUseSuspenseInfiniteQueryHook(op: OperationInfo, ctx: GenerationContext): VariableStatementStructure | null;
/**
* Build prefetch function.
* Example:
* export const prefetchUseFindPets = (queryClient: QueryClient, clientOptions: Options<FindPetsData, true> = {}, options?: Omit<FetchQueryOptions<Common.FindPetsDefaultResponse>, "queryKey" | "queryFn">) =>
* queryClient.prefetchQuery({
* queryKey: Common.UseFindPetsKeyFn(clientOptions),
* queryFn: () => findPets({ ...clientOptions, throwOnError: true }).then(response => response.data),
* ...options
* });
*/
export declare function buildPrefetchFn(op: OperationInfo): VariableStatementStructure;
/**
* Build prefetchInfiniteQuery function for a paginatable operation.
* Example:
* export const prefetchUseFindPaginatedPetsInfinite = (queryClient: QueryClient, clientOptions: Common.FindPaginatedPetsInfiniteClientOptions = {}, options?: Omit<FetchInfiniteQueryOptions<NonNullable<Common.FindPaginatedPetsDefaultResponse>>, "queryKey" | "queryFn" | "initialPageParam" | "getNextPageParam"> & Partial<Pick<FetchInfiniteQueryOptions<NonNullable<Common.FindPaginatedPetsDefaultResponse>>, "initialPageParam">> & { getNextPageParam?: GetNextPageParamFunction<unknown, NonNullable<Common.FindPaginatedPetsDefaultResponse>> }) =>
* queryClient.prefetchInfiniteQuery({
* queryKey: Common.UseFindPaginatedPetsInfiniteKeyFn(clientOptions),
* 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 buildPrefetchInfiniteQueryFn(op: OperationInfo, ctx: GenerationContext): VariableStatementStructure | null;
/**
* Build ensureQueryData function.
* Example:
* export const ensureUseFindPetsData = (queryClient: QueryClient, clientOptions: Options<FindPetsData, true> = {}, options?: Omit<EnsureQueryDataOptions<Common.FindPetsDefaultResponse>, "queryKey" | "queryFn">) =>
* queryClient.ensureQueryData({
* queryKey: Common.UseFindPetsKeyFn(clientOptions),
* queryFn: () => findPets({ ...clientOptions, throwOnError: true }).then(response => response.data),
* ...options
* });
*/
export declare function buildEnsureQueryDataFn(op: OperationInfo): VariableStatementStructure;