/** * HTTP 请求方法类型 */ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS'; /** * 请求配置接口 */ export interface RequestConfig { /** 请求 URL */ url: string; /** 请求方法 */ method?: HttpMethod; /** 请求头 */ headers?: Record; /** 请求参数(用于 GET 请求的查询参数) */ params?: Record; /** 请求体数据 */ data?: any; /** 超时时间(毫秒) */ timeout?: number; /** 响应类型 */ responseType?: 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData'; /** 是否携带凭证 */ credentials?: RequestCredentials; /** AbortSignal 用于取消请求 */ signal?: AbortSignal; } /** * 响应数据接口 */ export interface ApiResponse { /** 响应数据 */ data: T; /** HTTP 状态码 */ status: number; /** 状态文本 */ statusText: string; /** 请求是否成功 (2xx 状态码) */ success: boolean; /** 响应头(序列化为普通对象,可通过 postMessage 传递) */ headers?: Record; } /** * API 错误类 */ export declare class ApiError extends Error { status?: number; statusText?: string; response?: Response; config?: RequestConfig; code?: string; /** 业务错误响应数据 */ responseData?: any; constructor(message: string, config?: RequestConfig, response?: Response, code?: string, responseData?: any); /** * 序列化为可传递的 JSON 对象(用于 postMessage 等场景) * 移除不可序列化的对象(如 Response、Request 等) */ toJSON(): object; } /** * 通用接口测试 API 代理类 * * 特性: * - 支持所有 RESTful 请求方法 (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS) * - 完善的错误处理和边界检查 * - 超时控制和请求取消 * - 支持多种响应类型 * - 所有响应数据可序列化(可通过 postMessage 传递) */ declare class ApiProxy { /** 默认配置 */ private defaultConfig; /** 活跃的请求控制器 Map */ private activeRequests; /** * 构造函数 * @param baseConfig 基础配置 */ constructor(baseConfig?: Partial); /** * 验证 URL 格式 */ private validateUrl; /** * 构建完整 URL (包含查询参数) */ private buildUrl; /** * 序列化请求体 */ private serializeData; /** * 解析响应数据 */ private parseResponse; /** * 将 Headers 对象序列化为普通对象(可通过 postMessage 传递) */ private serializeHeaders; /** * 生成请求唯一键 */ private generateRequestKey; /** * 核心请求方法 */ request(config: RequestConfig): Promise>; /** * 执行实际请求 */ private executeRequest; /** * GET 请求 */ get(url: string, config?: Omit): Promise>; /** * POST 请求 */ post(url: string, data?: any, config?: Omit): Promise>; /** * PUT 请求 */ put(url: string, data?: any, config?: Omit): Promise>; /** * DELETE 请求 */ delete(url: string, config?: Omit): Promise>; /** * PATCH 请求 */ patch(url: string, data?: any, config?: Omit): Promise>; /** * HEAD 请求 */ head(url: string, config?: Omit): Promise>; /** * OPTIONS 请求 */ options(url: string, config?: Omit): Promise>; /** * 取消指定的请求 */ cancelRequest(requestKey: string): void; /** * 取消所有活跃请求 */ cancelAllRequests(): void; /** * 获取活跃请求数量 */ getActiveRequestCount(): number; } declare const apiProxy: ApiProxy; export default apiProxy;