import { AxiosRequestConfig, AxiosResponse } from 'axios'; import { HttpClientConfig } from './http-client-config.interface'; /** * 鉴权类型枚举 */ export declare enum AuthType { NONE = "none", API_KEY = "apiKey", BEARER_TOKEN = "bearer", BASIC_AUTH = "basic", OAUTH2 = "oauth2", CUSTOM = "custom" } /** * API密钥配置 */ export interface ApiKeyConfig { key: string; location: 'header' | 'query'; name?: string; prefix?: string; } /** * Bearer Token配置 */ export interface BearerTokenConfig { token: string; scheme?: string; } /** * 基础认证配置 */ export interface BasicAuthConfig { username: string; password: string; } /** * OAuth2配置 */ export interface OAuth2Config { clientId: string; clientSecret: string; tokenUrl: string; scopes?: string[]; grantType?: 'client_credentials' | 'password' | 'authorization_code'; username?: string; password?: string; redirectUri?: string; refreshToken?: string; accessToken?: string; } /** * 自定义鉴权配置 */ export interface CustomAuthConfig { /** * 自定义鉴权函数 */ authenticator: (config: AxiosRequestConfig) => Promise; /** * 刷新token函数 */ refresher?: (config: AxiosRequestConfig, error: any) => Promise; } /** * 鉴权配置 */ export type AuthConfig = { type: AuthType.NONE; } | { type: AuthType.API_KEY; config: ApiKeyConfig; } | { type: AuthType.BEARER_TOKEN; config: BearerTokenConfig; } | { type: AuthType.BASIC_AUTH; config: BasicAuthConfig; } | { type: AuthType.OAUTH2; config: OAuth2Config; } | { type: AuthType.CUSTOM; config: CustomAuthConfig; }; /** * 响应转换器配置 */ export interface ResponseTransformerConfig { /** * 成功响应转换器 */ success?: (response: AxiosResponse) => any; /** * 错误响应转换器 */ error?: (error: any) => any; /** * 响应数据提取路径 * 例如 'data.items' 表示从 response.data.items 获取数据 */ dataPath?: string; /** * 错误信息提取路径 * 例如 'error.message' 表示从 error.response.data.error.message 获取错误信息 */ errorPath?: string; /** * 是否自动验证响应格式 */ validateResponse?: boolean; /** * 响应格式验证器 */ validator?: (data: any) => boolean; } /** * API客户端配置 * 用于创建多个具有不同配置的 HttpClient 实例 */ export interface ApiClientConfig { /** API名称(用于标识和日志) */ name: string; /** 基础URL */ baseURL: string; /** * HttpClient 配置 * 包含所有 HttpClientConfig 的配置项(timeout, retry, logging, axiosConfig 等) */ httpConfig?: Partial; /** * 重试配置(快捷方式,会合并到 httpConfig.retry) * @deprecated 使用 httpConfig.retry 代替 */ retry?: { enabled?: boolean; retries?: number; retryCondition?: (error: any) => boolean; }; /** * 鉴权配置 * API客户端特有的鉴权功能 */ auth?: AuthConfig; /** * 响应转换配置 * API客户端特有的响应处理 */ responseTransformer?: ResponseTransformerConfig; /** 默认请求头 */ defaultHeaders?: Record; /** 默认查询参数 */ defaultParams?: Record; /** 拦截器配置 */ interceptors?: { request?: Array<(config: AxiosRequestConfig) => AxiosRequestConfig | Promise>; response?: Array<(response: AxiosResponse) => AxiosResponse | Promise>; error?: Array<(error: any) => any>; }; /** 是否启用响应验证 */ enableValidation?: boolean; /** 响应时间警告阈值(毫秒) */ responseTimeWarningThreshold?: number; /** 自定义标签(用于分类和查询) */ tags?: string[]; } /** * API客户端实例配置 */ export interface ApiClientInstanceConfig extends ApiClientConfig { /** 是否覆盖现有配置 */ override?: boolean; /** 环境特定配置 */ environments?: { development?: Partial; staging?: Partial; production?: Partial; }; } /** * API客户端注册配置 */ export interface ApiClientRegistryConfig { /** 全局默认配置 */ globalDefaults?: Partial; /** 日志清理配置 */ logCleanup?: { enabled?: boolean; retentionDays?: number; maxRecords?: number; }; /** 默认响应转换器 */ defaultResponseTransformer?: ResponseTransformerConfig; /** 是否启用自动重试 */ enableGlobalRetry?: boolean; /** 是否启用熔断器 */ enableCircuitBreaker?: boolean; }