fetch-armor
v1.0.1
Published
A fetch armor HTTP client with retry, timeout, and backoff support
Maintainers
Readme
fetch-armor
A lightweight, production-ready HTTP client for Node.js with built-in retry, timeout, and backoff support.
🚀 Features
- Automatic retries for failed requests
- Exponential backoff support
- Request timeout handling
- Custom retry conditions
- Lifecycle hooks (onRetry, onSuccess, onFail)
- Debug logging
- Works with native fetch (Node 18+)
📦 Installation
npm install fetch-armor🧑💻 Usage
import { fetchWithRetry } from "fetch-armor";
const data = await fetchWithRetry("https://api.example.com", {
retries: 3,
timeout: 10000,
backoff: "exponential",
});
console.log(data);⚙️ Options
| Option | Type | Default | Description | | ------- | ------- | ----------- | ------------------------ | | retries | number | 3 | Number of retry attempts | | delay | number | 1000 | Initial delay (ms) | | backoff | string | exponential | Backoff strategy | | timeout | number | 10000 | Request timeout (ms) | | debug | boolean | false | Enable debug logs |
🔁 Hooks
await fetchWithRetry(url, {
onRetry: (err, attempt, wait) => {
console.log("Retrying...", attempt);
},
onSuccess: (res) => {
console.log("Success");
},
onFail: (err) => {
console.log("Failed", err.message);
}
});🧠 Custom Retry Condition
await fetchWithRetry(url, {
retryCondition: (err) => {
return err.message.includes("timeout");
}
});🐞 Debug Mode
await fetchWithRetry(url, {
debug: true
});⚠️ Notes
- Only retries network errors and 5xx responses by default
- Uses AbortController for timeout handling
📄 License
MIT
👨💻 Author
Rudranarayan Sahu
