UNPKG

@shixinde/apifox-swagger

Version:

从 Apifox 导出 Swagger/OpenAPI 文档并生成 TypeScript 类型定义的工具

107 lines (106 loc) 5.1 kB
/** * React Query (TanStack Query) 适配器 * 提供类型安全的 React Query hooks 和查询功能 */ import type { Paths, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse } from './types.js'; import type { AxiosInstance, AxiosResponse } from './axios-adapter.js'; export interface QueryKey extends ReadonlyArray<unknown> { } export interface QueryFunctionContext<TQueryKey extends QueryKey = QueryKey> { queryKey: TQueryKey; signal?: AbortSignal; meta?: Record<string, unknown>; } export type QueryFunction<T = unknown, TQueryKey extends QueryKey = QueryKey> = (context: QueryFunctionContext<TQueryKey>) => T | Promise<T>; export interface UseQueryOptions<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> { queryKey?: TQueryKey; queryFn?: QueryFunction<TQueryFnData, TQueryKey>; enabled?: boolean; retry?: boolean | number; retryDelay?: number; staleTime?: number; cacheTime?: number; refetchOnMount?: boolean; refetchOnWindowFocus?: boolean; refetchOnReconnect?: boolean; select?: (data: TQueryFnData) => TData; onSuccess?: (data: TData) => void; onError?: (error: TError) => void; [key: string]: any; } export interface UseMutationOptions<TData = unknown, TError = unknown, TVariables = void, TContext = unknown> { mutationFn?: (variables: TVariables) => Promise<TData>; onSuccess?: (data: TData, variables: TVariables, context: TContext) => void; onError?: (error: TError, variables: TVariables, context?: TContext) => void; onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context?: TContext) => void; retry?: boolean | number; retryDelay?: number; [key: string]: any; } /** * 查询键生成器 */ export declare function createQueryKey(endpoint: readonly [any, any], pathParams?: any, queryParams?: any): QueryKey; /** * 创建查询函数 */ export declare function createQueryFunction<TPaths extends Paths, TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]>(axiosInstance: AxiosInstance, endpoint: readonly [TPath, TMethod], baseURL?: string): QueryFunction<AxiosResponse<ExtractResponse<TPaths, TPath, TMethod>>>; /** * 创建变更函数 */ export declare function createMutationFunction<TPaths extends Paths, TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]>(axiosInstance: AxiosInstance, endpoint: readonly [TPath, TMethod], baseURL?: string): (variables: { pathParams?: ExtractPathParams<TPaths, TPath, TMethod>; queryParams?: ExtractQueryParams<TPaths, TPath, TMethod>; body?: ExtractRequestBody<TPaths, TPath, TMethod>; }) => Promise<AxiosResponse<ExtractResponse<TPaths, TPath, TMethod>>>; /** * React Query 工厂类 */ export declare class ReactQueryFactory<TPaths extends Paths> { private axiosInstance; private baseURL; constructor(axiosInstance: AxiosInstance, baseURL?: string); /** * 创建查询配置 */ createQuery(endpoint: readonly [any, any], params?: { pathParams?: any; queryParams?: any; }, options?: Omit<UseQueryOptions<any, unknown, any>, 'queryKey' | 'queryFn'>): UseQueryOptions<any, unknown, any>; /** * 创建变更配置 */ createMutation(endpoint: readonly [any, any], options?: Omit<UseMutationOptions<any, unknown, any>, 'mutationFn'>): UseMutationOptions<any, unknown, any>; /** * 创建无限查询配置 */ createInfiniteQuery(endpoint: readonly [any, any], params?: { pathParams?: any; queryParams?: any; }, options?: { getNextPageParam?: (lastPage: any, allPages: any[]) => any; getPreviousPageParam?: (firstPage: any, allPages: any[]) => any; [key: string]: any; }): { getNextPageParam?: ((lastPage: any, allPages: any[]) => any) | undefined; getPreviousPageParam?: ((firstPage: any, allPages: any[]) => any) | undefined; queryKey: QueryKey; queryFn: ({ queryKey, pageParam }: any) => AxiosResponse<any> | Promise<AxiosResponse<any>>; select: (data: any) => any; }; } /** * 创建 React Query 工厂实例 */ export declare function createReactQueryFactory<TPaths extends Paths>(axiosInstance: AxiosInstance, baseURL?: string): ReactQueryFactory<TPaths>; /** * 便捷的查询配置创建函数 */ export declare function createQueryConfig<TPaths extends Paths, TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]>(axiosInstance: AxiosInstance, endpoint: readonly [TPath, TMethod], params?: { pathParams?: ExtractPathParams<TPaths, TPath, TMethod>; queryParams?: ExtractQueryParams<TPaths, TPath, TMethod>; }, baseURL?: string): UseQueryOptions<any, unknown, any, QueryKey>; /** * 便捷的变更配置创建函数 */ export declare function createMutationConfig<TPaths extends Paths, TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]>(axiosInstance: AxiosInstance, endpoint: readonly [TPath, TMethod], baseURL?: string): UseMutationOptions<any, unknown, any, unknown>;