Skip to Content
✨ New: Auth Plugin. JWT, OAuth2, API Key, and more authentication strategies out of the box. Read more... 🎉
Plugins NewRetry Plugin

Retry Plugin

The official retry plugin for endpoint-fetcher. Automatically retries failed requests with configurable backoff strategies.

Installation

npm install @endpoint-fetcher/retry

Requires endpoint-fetcher v3.0.0 or higher.

Quick Start

import { createApiClient, get } from 'endpoint-fetcher'; import { retryPlugin } from '@endpoint-fetcher/retry'; const api = createApiClient({ getUser: get<{ id: string }, User>((input) => `/users/${input.id}`), }, { baseUrl: 'https://api.example.com', plugins: [ retryPlugin({ maxRetries: 3, strategy: 'exponential', baseDelay: 1000, }), ], }); // Automatically retried on 5xx or network errors const user = await api.getUser({ id: '123' });

Configuration

OptionTypeDefaultDescription
maxRetriesnumber3Maximum number of retry attempts
strategy'fixed' | 'linear' | 'exponential''exponential'Delay strategy between retries
baseDelaynumber1000Base delay in milliseconds
maxDelaynumber30000Maximum delay cap in milliseconds
retryStatusCodesnumber[][500, 502, 503, 504, 429]Status codes that trigger a retry
retryMethodsstring[]All methodsHTTP methods eligible for retry
shouldRetry(error, attempt) => booleanChecks retryStatusCodesCustom retry condition
onRetry(error, attempt, delay) => void() => {}Callback on each retry attempt

Retry Strategies

StrategyPatternFormula
fixed1s, 1s, 1sbaseDelay
linear1s, 2s, 3sbaseDelay × attempt
exponential1s, 2s, 4s, 8sbaseDelay × 2^(attempt-1)

All strategies are capped at maxDelay.

Default Retry Behavior

Retries on: 500, 502, 503, 504 (server errors), 429 (rate limiting), and network errors (no status code).

Does not retry: 4xx client errors (except 429), 2xx successful responses.

Advanced Usage

Custom Retry Condition

retryPlugin({ maxRetries: 3, shouldRetry: (error, attempt) => { const err = error as { status?: number }; if (!err.status) return true; // Always retry network errors if (err.status === 429) return true; // Retry rate limiting if (err.status === 408) return true; // Retry request timeout return err.status >= 500; // Retry server errors }, })

Monitoring Retries

retryPlugin({ maxRetries: 3, onRetry: (error, attempt, delay) => { const err = error as { status?: number }; console.warn(`Retry ${attempt} after ${delay}ms (status: ${err.status ?? 'network error'})`); }, })

Only Retry Idempotent Methods

retryPlugin({ maxRetries: 3, retryMethods: ['GET', 'PUT', 'DELETE'], // skip POST })

Combining with Cache

plugins: [ retryPlugin({ maxRetries: 3, strategy: 'exponential' }), cache({ ttl: 300 }), ]

Execution order: the cache plugin checks first; on a cache miss, the retry plugin handles the request.

Troubleshooting

Requests are never retried — Check that maxRetries > 0, the status code is in retryStatusCodes, and the HTTP method is in retryMethods.

Too many retries causing performance issues — Reduce maxRetries, increase baseDelay, or set maxDelay. Use exponential strategy to naturally slow down.

Retries happening on client errors — Remove 4xx codes from retryStatusCodes, or use a custom shouldRetry that excludes them.