import { HttpRequestConfig, HttpResponse, HttpError } from './http'; /** * Interceptor priority levels */ export declare enum InterceptorPriority { /** Execute first - critical operations like tracing */ HIGHEST = 0, /** Execute early - auth, correlation */ HIGH = 25, /** Default priority */ NORMAL = 50, /** Execute later - logging, metrics */ LOW = 75, /** Execute last - cleanup, timing */ LOWEST = 100 } /** * Interceptor context for sharing state between interceptors */ export interface InterceptorContext { /** Distributed trace ID */ traceId: string; /** Current span ID */ spanId: string; /** Parent span ID for nested calls */ parentSpanId?: string; /** Request start time */ startTime: number; /** Shared metadata between interceptors */ metadata: Map; /** Tags for categorization */ tags: string[]; /** Retry attempt number */ retryAttempt: number; /** Original request config (before modifications) */ originalConfig: HttpRequestConfig; } /** * Request interceptor with context awareness */ export interface ContextualRequestInterceptor { /** Unique name for the interceptor */ name: string; /** Execution priority (lower = earlier) */ priority: number; /** Whether interceptor is enabled */ enabled?: boolean; /** Intercept function */ intercept: (config: HttpRequestConfig, context: InterceptorContext) => HttpRequestConfig | Promise; /** Error callback */ onError?: (error: Error, context: InterceptorContext) => void; } /** * Response interceptor with context awareness */ export interface ContextualResponseInterceptor { /** Unique name for the interceptor */ name: string; /** Execution priority (lower = earlier) */ priority: number; /** Whether interceptor is enabled */ enabled?: boolean; /** Intercept function */ intercept: (response: HttpResponse, context: InterceptorContext) => HttpResponse | Promise>; /** Error callback */ onError?: (error: Error, context: InterceptorContext) => void; } /** * Error interceptor with recovery support */ export interface ContextualErrorInterceptor { /** Unique name for the interceptor */ name: string; /** Execution priority (lower = earlier) */ priority: number; /** Whether interceptor is enabled */ enabled?: boolean; /** Intercept function - can return recovered response */ intercept: (error: HttpError, context: InterceptorContext) => HttpError | Promise; /** Check if this interceptor can handle the error */ canHandle?: (error: HttpError) => boolean; } /** * Interceptor chain manager with priority ordering */ export declare class InterceptorChain { private requestInterceptors; private responseInterceptors; private errorInterceptors; /** * Add request interceptor with priority ordering */ addRequestInterceptor(interceptor: ContextualRequestInterceptor): () => void; /** * Add response interceptor with priority ordering */ addResponseInterceptor(interceptor: ContextualResponseInterceptor): () => void; /** * Add error interceptor with priority ordering */ addErrorInterceptor(interceptor: ContextualErrorInterceptor): () => void; /** * Create new interceptor context */ createContext(config: HttpRequestConfig, parentContext?: Partial): InterceptorContext; /** * Execute request interceptor chain */ executeRequestChain(config: HttpRequestConfig, context: InterceptorContext): Promise; /** * Execute response interceptor chain */ executeResponseChain(response: HttpResponse, context: InterceptorContext): Promise>; /** * Execute error interceptor chain with recovery support */ executeErrorChain(error: HttpError, context: InterceptorContext): Promise; /** * Get all registered interceptors */ getInterceptors(): { request: ContextualRequestInterceptor[]; response: ContextualResponseInterceptor[]; error: ContextualErrorInterceptor[]; }; /** * Remove interceptor by name */ removeByName(name: string): void; /** * Clear all interceptors */ clear(): void; } /** * Circuit breaker state */ export interface CircuitBreakerState { /** Number of consecutive failures */ failures: number; /** Last failure timestamp */ lastFailure: number; /** Current circuit state */ state: 'closed' | 'open' | 'half-open'; /** Next attempt timestamp (when open) */ nextAttempt: number; /** Successful requests in half-open state */ halfOpenSuccesses: number; } /** * Circuit breaker configuration */ export interface CircuitBreakerConfig { /** Number of failures before opening circuit */ failureThreshold?: number; /** Time to wait before attempting recovery (ms) */ resetTimeout?: number; /** Number of successful requests needed to close circuit */ halfOpenSuccessThreshold?: number; /** Which HTTP status codes are considered failures */ failureStatuses?: number[]; /** Key extractor for service identification */ getServiceKey?: (config: HttpRequestConfig) => string; /** Callback when circuit opens */ onOpen?: (serviceKey: string, state: CircuitBreakerState) => void; /** Callback when circuit closes */ onClose?: (serviceKey: string) => void; /** Callback when circuit enters half-open */ onHalfOpen?: (serviceKey: string) => void; } /** * Circuit breaker error */ export declare class CircuitBreakerError extends Error { readonly isCircuitBreakerError = true; readonly serviceKey: string; readonly retryAfterMs: number; readonly state: CircuitBreakerState; constructor(serviceKey: string, state: CircuitBreakerState); } /** * Create circuit breaker interceptor */ export declare function createCircuitBreakerInterceptor(config?: CircuitBreakerConfig): { request: ContextualRequestInterceptor; error: ContextualErrorInterceptor; response: ContextualResponseInterceptor; getState: (serviceKey: string) => CircuitBreakerState | undefined; reset: (serviceKey: string) => void; resetAll: () => void; }; /** * Tracing configuration */ export interface TracingConfig { /** Service name for tracing */ serviceName?: string; /** Header names for trace propagation */ headers?: { traceId?: string; spanId?: string; parentSpanId?: string; sampled?: string; }; /** Sampling rate (0-1) */ samplingRate?: number; /** Callback for trace spans */ onSpan?: (span: TraceSpan) => void; } /** * Trace span data */ export interface TraceSpan { traceId: string; spanId: string; parentSpanId?: string; operationName: string; serviceName: string; startTime: number; endTime?: number; duration?: number; status: 'ok' | 'error'; tags: Record; logs: Array<{ timestamp: number; message: string; data?: unknown; }>; } /** * Create distributed tracing interceptors */ export declare function createTracingInterceptors(config?: TracingConfig): { request: ContextualRequestInterceptor; response: ContextualResponseInterceptor; error: ContextualErrorInterceptor; }; /** * Create correlation ID interceptor */ export declare function createCorrelationInterceptor(headerName?: string): ContextualRequestInterceptor; /** * Timing configuration */ export interface TimingConfig { /** Threshold for slow request warning (ms) */ slowThreshold?: number; /** Callback for timing data */ onTiming?: (data: TimingData) => void; /** Log slow requests */ logSlowRequests?: boolean; } /** * Timing data */ export interface TimingData { url: string; method: string; duration: number; status: number; traceId: string; isSlow: boolean; } /** * Create timing interceptor */ export declare function createTimingInterceptor(config?: TimingConfig): ContextualResponseInterceptor; /** * Retry configuration */ export interface RetryConfig { /** Maximum retry attempts */ maxRetries?: number; /** Base delay between retries (ms) */ baseDelay?: number; /** Maximum delay between retries (ms) */ maxDelay?: number; /** Exponential backoff factor */ backoffFactor?: number; /** Add jitter to delay */ jitter?: boolean; /** Which status codes should trigger retry */ retryStatuses?: number[]; /** Retry on network errors */ retryOnNetworkError?: boolean; /** Callback on retry */ onRetry?: (error: HttpError, attempt: number, delay: number) => void; } /** * Create retry interceptor with exponential backoff */ export declare function createRetryInterceptor(config: RetryConfig | undefined, httpClientFn: (config: HttpRequestConfig) => Promise): ContextualErrorInterceptor; /** * Create a fully configured interceptor chain */ export declare function createEnhancedInterceptorChain(config?: { tracing?: TracingConfig | false; circuitBreaker?: CircuitBreakerConfig | false; timing?: TimingConfig | false; enableCorrelation?: boolean; }): InterceptorChain; /** * Default interceptor chain instance */ export declare const enhancedInterceptorChain: InterceptorChain;