UNPKG

@ai-growth/nextjs

Version:

Seamlessly integrate Sanity CMS with Next.js applications for automated blog routing and rendering

151 lines 4.98 kB
/** * @fileoverview Retry logic utilities for Sanity CMS operations * * This module provides configurable retry mechanisms with exponential backoff, * jitter, and support for different retry strategies. It integrates with the * error handling system to determine which errors are retryable. * * @example * ```typescript * import { withRetry, RetryConfig } from '@ai-growth/nextjs/utils'; * * const config: RetryConfig = { * maxAttempts: 3, * baseDelay: 1000, * maxDelay: 30000, * backoffFactor: 2, * jitter: true * }; * * const result = await withRetry( * () => sanityClient.fetch(query), * config * ); * ``` */ /** * Retry strategy types */ export type RetryStrategy = 'exponential' | 'linear' | 'fixed'; /** * Configuration for retry behavior */ export interface RetryConfig { /** Maximum number of retry attempts (default: 3) */ maxAttempts: number; /** Base delay in milliseconds (default: 1000) */ baseDelay: number; /** Maximum delay in milliseconds (default: 30000) */ maxDelay: number; /** Backoff factor for exponential strategy (default: 2) */ backoffFactor: number; /** Retry strategy to use (default: 'exponential') */ strategy: RetryStrategy; /** Add random jitter to delays (default: true) */ jitter: boolean; /** Custom function to determine if an error is retryable */ isRetryable?: (error: Error) => boolean; /** Callback called before each retry attempt */ onRetry?: (error: Error, attempt: number, delay: number) => void; /** Callback called when all retries are exhausted */ onMaxAttemptsReached?: (error: Error, attempts: number) => void; } /** * Default retry configuration */ export declare const DEFAULT_RETRY_CONFIG: RetryConfig; /** * Calculate delay for a retry attempt based on strategy */ export declare function calculateDelay(attempt: number, config: RetryConfig): number; /** * Sleep for a specified number of milliseconds */ export declare function sleep(ms: number): Promise<void>; /** * Retry an async operation with configurable retry logic */ export declare function withRetry<T>(operation: () => Promise<T>, config?: Partial<RetryConfig>): Promise<T>; /** * Create a retry wrapper for a function */ export declare function createRetryWrapper<TArgs extends unknown[], TReturn>(fn: (...args: TArgs) => Promise<TReturn>, config?: Partial<RetryConfig>): (...args: TArgs) => Promise<TReturn>; /** * Retry configuration for specific error types */ export interface ConditionalRetryConfig extends RetryConfig { /** Only retry for specific error codes */ retryOnCodes?: string[]; /** Don't retry for specific error codes */ skipOnCodes?: string[]; /** Only retry for specific HTTP status codes */ retryOnStatus?: number[]; /** Don't retry for specific HTTP status codes */ skipOnStatus?: number[]; } /** * Advanced retry with conditional logic */ export declare function withConditionalRetry<T>(operation: () => Promise<T>, config?: Partial<ConditionalRetryConfig>): Promise<T>; /** * Retry with circuit breaker pattern */ export declare class CircuitBreaker { private readonly failureThreshold; private readonly recoveryTimeout; private failures; private lastFailureTime; private state; constructor(failureThreshold?: number, recoveryTimeout?: number); execute<T>(operation: () => Promise<T>): Promise<T>; getState(): { state: string; failures: number; lastFailureTime: number; }; reset(): void; } /** * Create a circuit breaker for Sanity operations */ export declare function createSanityCircuitBreaker(failureThreshold?: number, recoveryTimeout?: number): CircuitBreaker; /** * Utility to create retry configurations for common scenarios */ export declare const RETRY_PRESETS: { /** Quick retries for fast operations */ readonly fast: { readonly maxAttempts: 2; readonly baseDelay: 500; readonly maxDelay: 2000; readonly strategy: "exponential"; readonly jitter: true; }; /** Standard retries for most operations */ readonly standard: RetryConfig; /** Patient retries for slow operations */ readonly patient: { readonly maxAttempts: 5; readonly baseDelay: 2000; readonly maxDelay: 60000; readonly strategy: "exponential"; readonly jitter: true; }; /** Aggressive retries for critical operations */ readonly aggressive: { readonly maxAttempts: 10; readonly baseDelay: 100; readonly maxDelay: 10000; readonly strategy: "exponential"; readonly jitter: true; }; /** No jitter for predictable timing */ readonly predictable: { readonly maxAttempts: 3; readonly baseDelay: 1000; readonly maxDelay: 30000; readonly strategy: "exponential"; readonly jitter: false; }; }; //# sourceMappingURL=retry.d.ts.map