@shixinde/apifox-swagger
Version:
从 Apifox 导出 Swagger/OpenAPI 文档并生成 TypeScript 类型定义的工具
116 lines (115 loc) • 5.89 kB
TypeScript
/**
* Axios 适配器
* 提供类型安全的 axios HTTP 请求功能
*/
export interface AxiosInstance {
request<T = any>(config: AxiosRequestConfig): Promise<AxiosResponse<T>>;
}
export interface AxiosRequestConfig {
url?: string;
method?: string;
data?: any;
params?: any;
headers?: Record<string, string>;
timeout?: number;
[key: string]: any;
}
export interface AxiosResponse<T = any> {
data: T;
status: number;
statusText: string;
headers: Record<string, string>;
config: AxiosRequestConfig;
}
import type { Paths, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse } from './types.js';
import { type SafeEndpointCall } from './makeURL.js';
/**
* Axios 请求配置类型
*/
export interface AxiosApiConfig<TPaths extends Paths, TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]> extends Omit<AxiosRequestConfig, 'url' | 'method' | 'params' | 'data'> {
pathParams?: ExtractPathParams<TPaths, TPath, TMethod>;
queryParams?: ExtractQueryParams<TPaths, TPath, TMethod>;
body?: ExtractRequestBody<TPaths, TPath, TMethod>;
}
/**
* 创建类型安全的 Axios 客户端
*/
export declare class TypedAxiosClient<TPaths extends Paths> {
private axiosInstance;
private baseURL;
constructor(axiosInstance: AxiosInstance, baseURL?: string);
/**
* 执行类型安全的 HTTP 请求
*/
request<TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]>(endpoint: readonly [TPath, TMethod], config?: AxiosApiConfig<TPaths, TPath, TMethod>): Promise<AxiosResponse<ExtractResponse<TPaths, TPath, TMethod>>>;
/**
* GET 请求
*/
get<TPath extends keyof TPaths, TMethod extends 'get'>(endpoint: readonly [TPath, TMethod], config?: Omit<AxiosApiConfig<TPaths, TPath, TMethod>, 'body'>): Promise<AxiosResponse<ExtractResponse<TPaths, TPath, TMethod>>>;
/**
* POST 请求
*/
post<TPath extends keyof TPaths, TMethod extends 'post'>(endpoint: readonly [TPath, TMethod], config?: AxiosApiConfig<TPaths, TPath, TMethod>): Promise<AxiosResponse<ExtractResponse<TPaths, TPath, TMethod>>>;
/**
* PUT 请求
*/
put<TPath extends keyof TPaths, TMethod extends 'put'>(endpoint: readonly [TPath, TMethod], config?: AxiosApiConfig<TPaths, TPath, TMethod>): Promise<AxiosResponse<ExtractResponse<TPaths, TPath, TMethod>>>;
/**
* DELETE 请求
*/
delete<TPath extends keyof TPaths, TMethod extends 'delete'>(endpoint: readonly [TPath, TMethod], config?: Omit<AxiosApiConfig<TPaths, TPath, TMethod>, 'body'>): Promise<AxiosResponse<ExtractResponse<TPaths, TPath, TMethod>>>;
/**
* PATCH 请求
*/
patch<TPath extends keyof TPaths, TMethod extends 'patch'>(endpoint: readonly [TPath, TMethod], config?: AxiosApiConfig<TPaths, TPath, TMethod>): Promise<AxiosResponse<ExtractResponse<TPaths, TPath, TMethod>>>;
}
/**
* 创建类型安全的 Axios 客户端实例(需要传入 axios 实例)
*/
export declare function createTypedAxiosClient<TPaths extends Paths>(axiosInstance: AxiosInstance, baseURL?: string): TypedAxiosClient<TPaths>;
/**
* 创建类型安全的 Axios 客户端实例(使用配置对象)
* 需要预先安装并导入 axios
*/
export declare function createTypedAxiosClientWithConfig<TPaths extends Paths>(axios: any, config: AxiosRequestConfig & {
baseURL?: string;
}): TypedAxiosClient<TPaths>;
/**
* 创建类型安全的 Axios 客户端实例(同步版本,需要预先传入 axios)
*/
export declare function createTypedAxiosClientSync<TPaths extends Paths>(axios: any, config: AxiosRequestConfig & {
baseURL?: string;
}): TypedAxiosClient<TPaths>;
/**
* 简化的 API 调用函数
*/
export declare function callApi<TPaths extends Paths, TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]>(axiosInstance: AxiosInstance, call: SafeEndpointCall<TPaths, TPath, TMethod>, baseURL?: string): Promise<AxiosResponse<ExtractResponse<TPaths, TPath, TMethod>>>;
/**
* 创建 API 调用工厂函数
*/
export declare function createApiFactory<TPaths extends Paths>(axiosInstance: AxiosInstance, baseURL?: string): {
/**
* 调用 API 端点
*/
call: <TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]>(endpoint: readonly [TPath, TMethod], config?: AxiosApiConfig<TPaths, TPath, TMethod> | undefined) => Promise<AxiosResponse<ExtractResponse<TPaths, TPath, TMethod>>>;
/**
* GET 请求
*/
get: <TPath_1 extends keyof TPaths, TMethod_1 extends "get">(endpoint: readonly [TPath_1, TMethod_1], config?: Omit<AxiosApiConfig<TPaths, TPath_1, TMethod_1>, "body"> | undefined) => Promise<AxiosResponse<ExtractResponse<TPaths, TPath_1, TMethod_1>>>;
/**
* POST 请求
*/
post: <TPath_2 extends keyof TPaths, TMethod_2 extends "post">(endpoint: readonly [TPath_2, TMethod_2], config?: AxiosApiConfig<TPaths, TPath_2, TMethod_2> | undefined) => Promise<AxiosResponse<ExtractResponse<TPaths, TPath_2, TMethod_2>>>;
/**
* PUT 请求
*/
put: <TPath_3 extends keyof TPaths, TMethod_3 extends "put">(endpoint: readonly [TPath_3, TMethod_3], config?: AxiosApiConfig<TPaths, TPath_3, TMethod_3> | undefined) => Promise<AxiosResponse<ExtractResponse<TPaths, TPath_3, TMethod_3>>>;
/**
* DELETE 请求
*/
delete: <TPath_4 extends keyof TPaths, TMethod_4 extends "delete">(endpoint: readonly [TPath_4, TMethod_4], config?: Omit<AxiosApiConfig<TPaths, TPath_4, TMethod_4>, "body"> | undefined) => Promise<AxiosResponse<ExtractResponse<TPaths, TPath_4, TMethod_4>>>;
/**
* PATCH 请求
*/
patch: <TPath_5 extends keyof TPaths, TMethod_5 extends "patch">(endpoint: readonly [TPath_5, TMethod_5], config?: AxiosApiConfig<TPaths, TPath_5, TMethod_5> | undefined) => Promise<AxiosResponse<ExtractResponse<TPaths, TPath_5, TMethod_5>>>;
};