@pial-al-mamun/fetch-retry
v1.0.1
Published
Minimal fetch wrapper with retry, backoff, and jitter
Maintainers
Readme
Features
- Automatic retries — Network errors and 5xx responses
- Exponential backoff — Smart delay between retries
- Jitter — Prevents thundering herd
- 0.74 KB gzipped — Zero dependencies
- JSDoc support — Type safety even in JavaScript
Install
npm install @pial/fetch-retryAPI
fetchWithRetry(input, init?)
Drop-in fetch wrapper that retries on network errors and (by default) HTTP 5xx + 429.
import { fetchWithRetry } from "@pial/fetch-retry";
const res = await fetchWithRetry("https://api.example.com/users", {
retry: 5, // or: { maxRetries, delay, backoff, jitter, shouldRetry }
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);fetchJSON<T>(input, init?)
Convenience wrapper around fetchWithRetry that calls Response.json() and returns typed JSON.
import { fetchJSON } from "@pial/fetch-retry";
type User = { id: string; name: string };
const user = await fetchJSON<User>("https://api.example.com/user/1", { retry: 3 });