netstorage
Version:
A TypeScript API and CLI for the Akamai NetStorage REST interface
51 lines (50 loc) • 2.5 kB
TypeScript
import { type NetStorageClientConfig, type NetStorageOperation } from '../index';
/**
* Configuration for retry behavior using exponential backoff.
*
* @property retries - Maximum number of retry attempts.
* @property baseDelayMs - Base delay in milliseconds before retrying.
* @property maxDelayMs - Maximum delay in milliseconds between retries.
* @property jitter - Whether to apply random jitter to the backoff delay.
* @property shouldRetry - Function to determine if an error should trigger a retry.
* @property beforeAttempt - Hook executed before each retry attempt (e.g., for rate limiting).
* @property onRetry - Callback invoked after a failed attempt, before the delay.
*/
export interface WithRetriesOptions {
retries: number;
baseDelayMs: number;
maxDelayMs: number;
jitter: boolean;
shouldRetry: (error: unknown) => boolean;
beforeAttempt?: () => Promise<void>;
onRetry?: (error: unknown, attempt: number, delay: number) => void;
}
/**
* Calculates exponential backoff delay with optional jitter.
*
* @param attempt - The current retry attempt (zero-based).
* @param base - The base delay in milliseconds.
* @param max - The maximum delay allowed.
* @param jitter - Whether to apply jitter to the delay.
* @returns The delay in milliseconds.
*/
export declare function calculateDelay(attempt: number, base: number, max: number, jitter: boolean): number;
/**
* Determines if a system-level error is transient and retryable.
*
* @param config - NetStorage client config for logging.
* @param err - The error to evaluate.
* @param method - The method or operation name for config.
* @returns True if the error should be retried; otherwise, false.
*/
export declare function shouldRetrySystemError(config: NetStorageClientConfig, err: Error, method?: string): boolean;
/**
* Executes a function with retry logic using exponential backoff and optional jitter.
*
* @param config - NetStorage client config for logging and rate limiting.
* @param method - The NetStorage operation being executed.
* @param fn - The asynchronous function to execute with retry logic.
* @param overrides - Optional overrides for retry behavior.
* @returns A promise that resolves with the function's result or rejects after final failure.
*/
export declare function withRetries<T>(config: NetStorageClientConfig, method: NetStorageOperation, fn: () => Promise<T>, overrides?: Partial<Pick<WithRetriesOptions, 'shouldRetry' | 'onRetry'>>): Promise<T>;