Getting Started
Installation
bash
npm install fetch-smartlyQuick Start
typescript
import { fetchWithRetry } from 'fetch-smartly';
const response = await fetchWithRetry({
url: 'https://api.example.com/data',
method: 'GET',
timeout: 5000,
retry: { maxRetries: 3 },
});
console.log(response.data); // Parsed JSON or text
console.log(response.status); // 200
console.log(response.retries); // 0
console.log(response.duration); // 142 (ms)What Happens Under the Hood
- Request —
fetchWithRetrycalls nativefetchwith an internalAbortControllerfor timeout management. - Failure Classification — If the request fails, the error is classified as
NetworkError,TimeoutError,HttpError, orRateLimitError. - Retry Decision — The failure analyzer checks if the error is retryable (5xx, 429, network errors). Client errors like 400/401/404 are never retried.
- Backoff — If retrying, the orchestrator computes an exponential delay with jitter, respecting
Retry-Afterheaders on 429 responses. - Response — On success, a normalized
SmartFetchResponseis returned with parsed data, timing, and retry metadata.
Configuration
typescript
await fetchWithRetry({
url: 'https://api.example.com/data',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ key: 'value' }),
timeout: 10000,
retry: {
maxRetries: 5,
baseDelay: 1000,
maxDelay: 30000,
backoffFactor: 2,
jitter: true,
retryOn: [408, 429, 500, 502, 503, 504],
retryOnNetworkError: true,
},
onRetry: (ctx) => {
console.log(`Retry ${ctx.attempt}/${ctx.maxRetries}, waiting ${ctx.delay}ms`);
},
debug: true,
});Next Steps
- Error Handling — Learn about the typed error hierarchy
- Retry & Backoff — Configure retry behavior in detail
- Circuit Breaker — Protect against cascading failures
- API Reference — Full API documentation