import { AppError } from '../monitoring/errorTypes'; /** * Recovery strategy types */ export type RecoveryStrategy = 'retry' | 'fallback' | 'cache' | 'degrade' | 'skip' | 'escalate'; /** * Recovery context */ export interface RecoveryContext { error: AppError; operation: string; attempts: number; startTime: number; metadata?: Record; } /** * Recovery result */ export interface RecoveryResult { success: boolean; value?: T; strategy: RecoveryStrategy; recovered: boolean; error?: AppError; } /** * Recovery handler function */ export type RecoveryHandler = (context: RecoveryContext) => Promise> | RecoveryResult; /** * Recovery configuration */ export interface RecoveryConfig { /** Maximum recovery attempts */ maxAttempts?: number; /** Strategies to try in order */ strategies?: RecoveryStrategy[]; /** Custom handlers for each strategy */ handlers?: Partial>>; /** Fallback value */ fallbackValue?: T | (() => T | Promise); /** Cache key for cache strategy */ cacheKey?: string; /** Degraded functionality */ degradedFn?: () => T | Promise; /** Should escalate condition */ shouldEscalate?: (error: AppError, attempts: number) => boolean; /** On recovery callback */ onRecovery?: (result: RecoveryResult, context: RecoveryContext) => void; } /** * Error recovery manager */ export declare class ErrorRecovery { private config; private cache; private readonly CACHE_TTL; constructor(config?: RecoveryConfig); /** * Execute operation with recovery */ execute(operation: () => Promise, operationName: string, metadata?: Record): Promise; /** * Clear cache */ clearCache(): void; /** * Try a specific recovery strategy */ private tryStrategy; /** * Retry strategy */ private retryStrategy; /** * Fallback strategy */ private fallbackStrategy; /** * Cache strategy */ private cacheStrategy; /** * Degrade strategy */ private degradeStrategy; /** * Skip strategy */ private skipStrategy; /** * Escalate strategy */ private escalateStrategy; /** * Check if error is retryable */ private isRetryableError; } /** * Recovery builder for fluent configuration */ export declare class RecoveryBuilder { private config; /** * Set max attempts */ maxAttempts(n: number): RecoveryBuilder; /** * Set strategies */ strategies(...strategies: RecoveryStrategy[]): RecoveryBuilder; /** * Set fallback value */ fallback(value: T | (() => T | Promise)): RecoveryBuilder; /** * Set cache key */ cache(key: string): RecoveryBuilder; /** * Set degraded function */ degrade(fn: () => T | Promise): RecoveryBuilder; /** * Set custom handler */ handler(strategy: RecoveryStrategy, handler: RecoveryHandler): RecoveryBuilder; /** * Set recovery callback */ onRecovery(callback: (result: RecoveryResult, context: RecoveryContext) => void): RecoveryBuilder; /** * Build the recovery manager */ build(): ErrorRecovery; /** * Execute with configured recovery */ execute(operation: () => Promise, operationName: string, metadata?: Record): Promise; } /** * Create a recovery builder */ export declare function recover(): RecoveryBuilder; /** * Graceful degradation wrapper */ export declare function withGracefulDegradation(primaryFn: () => Promise, fallbackFn: () => T | Promise, options?: { shouldDegrade?: (error: unknown) => boolean; onDegrade?: (error: unknown) => void; }): Promise<{ value: T; degraded: boolean; }>; /** * Circuit breaker with fallback */ export declare function withCircuitFallback(primaryFn: () => Promise, fallbackFn: () => T | Promise, options?: { failureThreshold?: number; resetTimeout?: number; }): () => Promise; /** * Stale-while-revalidate pattern */ export declare function staleWhileRevalidate(fetchFn: () => Promise, options: { cacheKey: string; staleTime?: number; maxAge?: number; storage?: Storage; }): () => Promise; /** * Automatic retry with exponential backoff */ export declare function autoRetry(fn: () => Promise, options?: { maxAttempts?: number; baseDelay?: number; maxDelay?: number; shouldRetry?: (error: unknown, attempt: number) => boolean; onRetry?: (error: unknown, attempt: number, delay: number) => void; }): Promise; /** * Safe async operation wrapper */ export declare function safe(fn: () => Promise): Promise<[T, null] | [null, Error]>; /** * Safe sync operation wrapper */ export declare function safeSync(fn: () => T): [T, null] | [null, Error];