@shixinde/apifox-swagger
Version:
从 Apifox 导出 Swagger/OpenAPI 文档并生成 TypeScript 类型定义的工具
71 lines (70 loc) • 3.15 kB
TypeScript
/**
* makeURL 函数和相关类型推断工具
* 提供类型安全的 URL 和方法组合
*/
import type { HttpMethod, Paths, ExtractMethods, ExtractPathParams, ExtractQueryParams, ExtractRequestBody, ExtractResponse, PathParams } from './types.js';
/**
* 创建类型安全的 URL 和方法组合
* @param url - API 路径
* @param method - HTTP 方法
* @returns 包含 URL 和方法的元组
*/
export declare function makeURL<U extends keyof Paths, M extends ExtractMethods<Paths, U>>(url: U, method: M): readonly [U, M];
/**
* 通用的 makeURL 函数,适用于任何路径类型
*/
export declare function makeURLGeneric<TPaths extends Paths, U extends keyof TPaths, M extends ExtractMethods<TPaths, U>>(url: U, method: M): readonly [U, M];
/**
* 从 makeURL 结果中提取 API 端点信息
*/
export type ExtractEndpointInfo<TPaths extends Paths, T extends readonly [keyof TPaths, HttpMethod]> = T extends readonly [infer U, infer M] ? U extends keyof TPaths ? M extends keyof TPaths[U] ? {
path: U;
method: M;
pathParams: ExtractPathParams<TPaths, U, M>;
queryParams: ExtractQueryParams<TPaths, U, M>;
requestBody: ExtractRequestBody<TPaths, U, M>;
response: ExtractResponse<TPaths, U, M>;
} : never : never : never;
/**
* 构建完整的 URL(包含路径参数替换)
*/
export declare function buildURL<T extends string>(path: T, params?: PathParams<T>): string;
/**
* 构建查询字符串
*/
export declare function buildQueryString(params?: Record<string, any>): string;
/**
* 构建完整的请求 URL
*/
export declare function buildFullURL<T extends string>(path: T, pathParams?: PathParams<T>, queryParams?: Record<string, any>): string;
/**
* API 端点配置类型
*/
export interface EndpointConfig<TPaths extends Paths, TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]> {
url: TPath;
method: TMethod;
pathParams?: ExtractPathParams<TPaths, TPath, TMethod>;
queryParams?: ExtractQueryParams<TPaths, TPath, TMethod>;
body?: ExtractRequestBody<TPaths, TPath, TMethod>;
}
/**
* 创建端点配置
*/
export declare function createEndpoint<TPaths extends Paths, TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]>(config: EndpointConfig<TPaths, TPath, TMethod>): EndpointConfig<TPaths, TPath, TMethod>;
/**
* 类型安全的端点调用配置
*/
export interface SafeEndpointCall<TPaths extends Paths, TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]> {
endpoint: readonly [TPath, TMethod];
pathParams?: ExtractPathParams<TPaths, TPath, TMethod>;
queryParams?: ExtractQueryParams<TPaths, TPath, TMethod>;
body?: ExtractRequestBody<TPaths, TPath, TMethod>;
}
/**
* 创建类型安全的端点调用
*/
export declare function createSafeCall<TPaths extends Paths, TPath extends keyof TPaths, TMethod extends keyof TPaths[TPath]>(endpoint: readonly [TPath, TMethod], options?: {
pathParams?: ExtractPathParams<TPaths, TPath, TMethod>;
queryParams?: ExtractQueryParams<TPaths, TPath, TMethod>;
body?: ExtractRequestBody<TPaths, TPath, TMethod>;
}): SafeEndpointCall<TPaths, TPath, TMethod>;