type CircuitState = 'CLOSED' | 'OPEN' | 'HALF_OPEN'; interface RetryOptions { /** Maximum number of attempts (default: 3) */ attempts?: number; /** Base delay between retries in ms (default: 500) */ baseDelay?: number; /** Maximum delay cap in ms (default: 30_000) */ maxDelay?: number; /** Jitter factor 0–1 to randomise delays (default: 0.2) */ jitter?: number; /** Timeout per attempt in ms — 0 = no timeout (default: 0) */ timeout?: number; /** * Which errors should trigger a retry. * Return true to retry, false to throw immediately. * Default: retry on all errors. */ shouldRetry?: (error: unknown, attempt: number) => boolean; /** Called after each failed attempt */ onRetry?: (error: unknown, attempt: number, delayMs: number) => void; } interface CircuitBreakerOptions { /** Failures needed to open the circuit (default: 5) */ threshold?: number; /** How long the circuit stays open in ms (default: 30_000) */ resetAfter?: number; /** Successes needed in HALF_OPEN to close again (default: 2) */ halfOpenSuccesses?: number; } interface SmartRetryOptions extends RetryOptions { /** Enable circuit breaker for this operation */ circuitBreaker?: CircuitBreakerOptions; } /** * Error thrown when all retry attempts are exhausted. */ declare class RetryExhaustedError extends Error { readonly name = "RetryExhaustedError"; readonly attempts: number; readonly lastError: unknown; constructor(attempts: number, lastError: unknown); } /** * Error thrown when a single attempt exceeds the configured timeout. */ declare class TimeoutError extends Error { readonly name = "TimeoutError"; constructor(timeoutMs: number); } /** * SmartRetry — production-grade retry logic with exponential backoff, jitter, * per-attempt timeouts, and an optional circuit breaker. * * @example * // Simple: 3 attempts, exponential backoff * const data = await retry(() => fetch('/api/data').then(r => r.json())); * * // Advanced: with circuit breaker * const retry = new SmartRetry({ * attempts: 5, * timeout: 5_000, * circuitBreaker: { threshold: 5, resetAfter: 30_000 }, * }); * const result = await retry.execute(() => callExternalApi()); */ declare class SmartRetry { private readonly attempts; private readonly baseDelay; private readonly maxDelay; private readonly jitter; private readonly timeout; private readonly shouldRetry; private readonly onRetry; private readonly breaker; constructor(options?: SmartRetryOptions); /** * Execute `fn` with retry logic applied. */ execute(fn: () => Promise): Promise; /** Current circuit breaker state (undefined if no breaker configured) */ get circuitState(): string | undefined; /** Manually reset the circuit breaker */ resetCircuit(): void; private runWithTimeout; /** * Exponential backoff with jitter: * delay = clamp(baseDelay * 2^(attempt-1) ± jitter%, 0, maxDelay) */ private calculateDelay; private sleep; } /** * Convenience function for one-off retries. * * @example * const data = await retry(() => api.getData(), { attempts: 3, timeout: 5000 }); */ declare function retry(fn: () => Promise, options?: SmartRetryOptions): Promise; /** * Circuit Breaker — protects your app from cascading failures. * * States: * - **CLOSED** (normal): requests flow through; failures are counted. * - **OPEN** (tripped): all requests fail fast — no upstream calls made. * - **HALF_OPEN** (testing): a limited number of requests probe if recovery happened. * * @example * const breaker = new CircuitBreaker({ threshold: 5, resetAfter: 30_000 }); * await breaker.execute(() => fetch('/api/payments')); */ declare class CircuitBreaker { private state; private failureCount; private successCount; private openedAt; private readonly threshold; private readonly resetAfter; private readonly halfOpenSuccesses; constructor(options?: CircuitBreakerOptions); /** Current state of the circuit */ get currentState(): CircuitState; /** * Execute `fn` through the circuit breaker. * Throws `CircuitOpenError` when the circuit is OPEN. */ execute(fn: () => Promise): Promise; /** Reset the circuit to CLOSED (useful in tests or manual recovery) */ reset(): void; private onSuccess; private onFailure; private trip; private transitionIfNeeded; } /** * Thrown when a call is rejected because the circuit is open. */ declare class CircuitOpenError extends Error { readonly name = "CircuitOpenError"; constructor(message: string); } export { CircuitBreaker, type CircuitBreakerOptions, CircuitOpenError, type CircuitState, RetryExhaustedError, type RetryOptions, SmartRetry, type SmartRetryOptions, TimeoutError, retry };