import { AxiosRequestConfig, AxiosResponse } from 'axios'; import { LogCleanupConfig } from '../services/log-cleanup.service'; import { SSRFProtectionConfig, URLValidationConfig } from '../utils/security-validator.util'; import type { AgentOptions } from 'https'; /** * SSL/TLS 证书配置 * 兼容 Node.js https.Agent 的选项 * 参考: https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options */ export interface SslCertificateConfig extends Partial> { /** * 是否禁用证书验证(仅用于开发/测试环境) * 警告:设置为 true 会使连接不安全,生产环境严禁使用 */ rejectUnauthorized?: boolean; /** * CA 证书内容(PEM 格式) * 可以是单个证书或多个证书的字符串 */ ca?: string | string[] | Buffer; /** * 客户端证书内容(PEM 格式) */ cert?: string | string[] | Buffer; /** * 客户端私钥内容(PEM 格式) */ key?: string | string[] | Buffer; /** * 私钥密码 */ passphrase?: string; /** * 服务器名称(用于 SNI) */ servername?: string; /** * 支持的 TLS 版本 */ minVersion?: 'TLSv1' | 'TLSv1.1' | 'TLSv1.2' | 'TLSv1.3'; maxVersion?: 'TLSv1' | 'TLSv1.1' | 'TLSv1.2' | 'TLSv1.3'; /** * 支持的加密套件 */ ciphers?: string; /** * 是否支持 DH 参数 */ dhparam?: string | Buffer; /** * 是否使用安全 renegotiation */ secureOptions?: number; } /** * Axios 原生配置透传 * 基于 AxiosRequestConfig,排除已在 HttpClientConfig 中定义的字段 * * 使用方式: * - 直接传 Axios 原生配置对象 * - 类型安全,支持所有 Axios 配置项 * - ssl 配置会被自动转换为 httpsAgent */ export type AxiosNativeConfig = Omit & { /** * SSL/TLS 证书配置(简化版) * 会自动转换为 httpsAgent,优先级高于直接指定的 httpsAgent */ ssl?: SslCertificateConfig; }; /** * HTTP客户端配置接口 * 基于Spring Boot的@ConfigurationProperties设计理念 * 所有字段都是可选的,模块会使用合理的默认值 */ export interface HttpClientConfig { /** 基础配置 */ baseURL?: string; timeout?: number; /** 重试配置 */ retry?: Partial; /** 熔断器配置 */ circuitBreaker?: Partial; /** 代理配置 */ proxy?: Partial; /** 日志配置 */ logging?: Partial; /** 日志清理配置 */ logCleanup?: Partial; /** 拦截器配置 */ interceptors?: Partial; /** 连接池配置 */ connectionPool?: Partial; /** 安全配置 */ security?: { /** URL验证配置 */ urlValidation?: Partial; /** SSRF防护配置 */ ssrfProtection?: Partial; /** 是否启用安全验证 */ enabled?: boolean; }; /** * Axios 原生配置透传 * 用于直接传递 Axios 支持的任何配置项,提供最大灵活性 * 优先级低于上述显式配置,会进行深度合并 */ axiosConfig?: AxiosNativeConfig; } /** * 重试配置 * 基于axios-retry库的配置 * 所有字段都是可选的 */ export interface RetryConfig { enabled?: boolean; retries?: number; retryDelay?: (retryCount: number) => number; retryCondition?: (error: any) => boolean; shouldResetTimeout?: boolean; onRetry?: (retryCount: number, error: any, requestConfig: AxiosRequestConfig) => void; } /** * 熔断器配置 * 类似Spring Cloud CircuitBreaker * 所有字段都是可选的 */ export interface CircuitBreakerConfig { enabled?: boolean; failureThreshold?: number; recoveryTimeoutMs?: number; monitoringPeriodMs?: number; minimumThroughputThreshold?: number; countHalfOpenCalls?: boolean; } /** * 代理配置 * 所有字段都是可选的 */ export interface ProxyConfig { enabled?: boolean; /** 是否从环境变量读取代理配置 (HTTP_PROXY, HTTPS_PROXY, NO_PROXY) */ fromEnvironment?: boolean; /** 手动指定的代理主机 (当 fromEnvironment 为 false 时使用) */ host?: string; /** 手动指定的代理端口 (当 fromEnvironment 为 false 时使用) */ port?: number; /** 代理协议 */ protocol?: 'http' | 'https'; /** 代理认证信息 */ auth?: { username?: string; password?: string; }; } /** * 日志配置 * 类似Spring Boot的logging配置 * 所有字段都是可选的 */ export interface LoggingConfig { enabled?: boolean; logRequests?: boolean; logResponses?: boolean; logErrors?: boolean; logHeaders?: boolean; logBody?: boolean; maxBodyLength?: number; /** * 脱敏配置 * 敏感字段列表,统一适用于 headers、body、query string * 例如:['password', 'token', 'secret', 'apiKey'] */ sanitize?: string[]; logLevel?: 'debug' | 'info' | 'warn' | 'error'; databaseLogging?: { enabled?: boolean; dataSource?: string; }; } /** * 拦截器配置 * 所有字段都是可选的 */ export interface InterceptorConfig { requestInterceptors?: string[]; responseInterceptors?: string[]; errorInterceptors?: string[]; } /** * 连接池配置 * 所有字段都是可选的 */ export interface ConnectionPoolConfig { enabled?: boolean; maxSockets?: number; maxFreeSockets?: number; timeoutMs?: number; keepAlive?: boolean; } /** * HTTP请求上下文 * 类似Spring的RequestContextHolder */ export interface HttpContext { requestId: string; startTime: number; attemptCount: number; parentRequestId?: string; traceId?: string; userId?: string; correlationId?: string; metadata: Record; } /** * HTTP请求日志实体 * 用于TypeORM数据库记录 */ export interface HttpLogEntity { id: string; requestId: string; method: string; url: string; headers: Record; body?: string; statusCode?: number; responseTime: number; attemptCount: number; success: boolean; errorMessage?: string; userId?: string; timestamp: Date; metadata: Record; } /** * 拦截器接口 */ export interface HttpInterceptor { name: string; order: number; intercept(request: AxiosRequestConfig, next: () => Promise): Promise; } /** * HTTP方法类型 */ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS'; /** * HTTP请求统计信息 */ export interface HttpStats { totalRequests: number; successfulRequests: number; failedRequests: number; averageResponseTime: number; requestsByMethod: Record; requestsByStatus: Record; circuitBreakerStats: Record; }