| 1 | interface RetryOptions {
|
|---|
| 2 | /**
|
|---|
| 3 | * Delay between retries. Can be a static number (milliseconds) or a function
|
|---|
| 4 | * that computes delay dynamically based on the current attempt.
|
|---|
| 5 | *
|
|---|
| 6 | * @default 0
|
|---|
| 7 | * @example
|
|---|
| 8 | * delay: (attempts) => attempt * 50
|
|---|
| 9 | */
|
|---|
| 10 | delay?: number | ((attempts: number) => number);
|
|---|
| 11 | /**
|
|---|
| 12 | * The number of retries to attempt.
|
|---|
| 13 | * @default Number.POSITIVE_INFINITY
|
|---|
| 14 | */
|
|---|
| 15 | retries?: number;
|
|---|
| 16 | /**
|
|---|
| 17 | * An AbortSignal to cancel the retry operation.
|
|---|
| 18 | */
|
|---|
| 19 | signal?: AbortSignal;
|
|---|
| 20 | /**
|
|---|
| 21 | * A function that determines whether to retry based on the error and attempt number.
|
|---|
| 22 | * If not provided, all errors will trigger a retry.
|
|---|
| 23 | *
|
|---|
| 24 | * @param {unknown} error - The error that occurred.
|
|---|
| 25 | * @param {number} attempt - The current attempt number (0-indexed).
|
|---|
| 26 | * @returns {boolean} Whether to retry.
|
|---|
| 27 | *
|
|---|
| 28 | * @example
|
|---|
| 29 | * shouldRetry: (error, attempt) => error.status >= 500
|
|---|
| 30 | */
|
|---|
| 31 | shouldRetry?: (error: unknown, attempt: number) => boolean;
|
|---|
| 32 | }
|
|---|
| 33 | /**
|
|---|
| 34 | * Retries a function that returns a promise until it resolves successfully.
|
|---|
| 35 | *
|
|---|
| 36 | * @template T
|
|---|
| 37 | * @param {() => Promise<T>} func - The function to retry.
|
|---|
| 38 | * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
|
|---|
| 39 | *
|
|---|
| 40 | * @example
|
|---|
| 41 | * // Basic usage with default retry options
|
|---|
| 42 | * retry(() => fetchData()).then(data => console.log(data));
|
|---|
| 43 | */
|
|---|
| 44 | declare function retry<T>(func: () => Promise<T>): Promise<T>;
|
|---|
| 45 | /**
|
|---|
| 46 | * Retries a function that returns a promise a specified number of times.
|
|---|
| 47 | *
|
|---|
| 48 | * @template T
|
|---|
| 49 | * @param {() => Promise<T>} func - The function to retry. It should return a promise.
|
|---|
| 50 | * @param {number} retries - The number of retries to attempt. Default is Infinity.
|
|---|
| 51 | * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
|
|---|
| 52 | *
|
|---|
| 53 | * @example
|
|---|
| 54 | * // Retry a function up to 3 times
|
|---|
| 55 | * retry(() => fetchData(), 3).then(data => console.log(data));
|
|---|
| 56 | */
|
|---|
| 57 | declare function retry<T>(func: () => Promise<T>, retries: number): Promise<T>;
|
|---|
| 58 | /**
|
|---|
| 59 | * Retries a function that returns a promise with specified options.
|
|---|
| 60 | *
|
|---|
| 61 | * @template T
|
|---|
| 62 | * @param {() => Promise<T>} func - The function to retry. It should return a promise.
|
|---|
| 63 | * @param {RetryOptions} options - Options to configure the retry behavior.
|
|---|
| 64 | * @param {number | ((attempts: number) => number)} [options.delay=0] - Delay(milliseconds) between retries.
|
|---|
| 65 | * @param {number} [options.retries=Infinity] - The number of retries to attempt.
|
|---|
| 66 | * @param {AbortSignal} [options.signal] - An AbortSignal to cancel the retry operation.
|
|---|
| 67 | * @param {(error: unknown, attempt: number) => boolean} [options.shouldRetry] - A function that determines whether to retry.
|
|---|
| 68 | * @returns {Promise<T>} A promise that resolves with the value of the successful function call.
|
|---|
| 69 | *
|
|---|
| 70 | * @example
|
|---|
| 71 | * // Retry a function with a delay of 1000ms between attempts
|
|---|
| 72 | * retry(() => fetchData(), { delay: 1000, times: 5 }).then(data => console.log(data));
|
|---|
| 73 | *
|
|---|
| 74 | * @example
|
|---|
| 75 | * // Retry a function with a fixed delay
|
|---|
| 76 | * retry(() => fetchData(), { delay: 1000, retries: 5 });
|
|---|
| 77 | *
|
|---|
| 78 | * // Retry a function with a delay increasing linearly by 50ms per attempt
|
|---|
| 79 | * retry(() => fetchData(), { delay: (attempts) => attempt * 50, retries: 5 });
|
|---|
| 80 | *
|
|---|
| 81 | * @example
|
|---|
| 82 | * // Retry a function with exponential backoff + jitter (max delay 10 seconds)
|
|---|
| 83 | * retry(() => fetchData(), {
|
|---|
| 84 | * delay: (attempts) => Math.min(Math.random() * 100 * 2 ** attempts, 10000),
|
|---|
| 85 | * retries: 5
|
|---|
| 86 | * });
|
|---|
| 87 | */
|
|---|
| 88 | declare function retry<T>(func: () => Promise<T>, options: RetryOptions): Promise<T>;
|
|---|
| 89 |
|
|---|
| 90 | export { retry };
|
|---|