DEV Community

Aubaid farroukh
Aubaid farroukh

Posted on

I Built a Retry Library Because I Kept Losing Failed API Calls

Every retry library I found did the same thing: wrap a function, retry it a few times, throw if it still fails.
Fine until the retries run out and the error just... disappears into a catch block. No record of what request failed, what it was trying to send, or why. I'd find out a webhook silently dropped three days later when someone asked why an order never synced.

So I built smart-retry a small TypeScript retry library that does two things most others don't:

  1. Drop-in Axios and Fetch clients, not just a generic wrapper function
  2. Automatic failure logging when a request exhausts all retries, it gets written to disk with its URL, method, headers, body, and error, so you can inspect or replay it later instead of losing it the moment it throws

Quick look

import { createAxiosRetry } from '@aubaid/smart-retry';

const client = createAxiosRetry({
  maxRetries: 5,
  delay: 2000,
  backoff: 'exponential',
});
Enter fullscreen mode Exit fullscreen mode

const response = await client.get('https://api.example.com/users');
If that fails after 5 attempts, you don't just get a thrown error — you get a log:

const manager = client.getRetryManager();
const failed = await manager.getFailedRequests();

console.log(`${failed.length} requests logged`);
console.log('Log file:', manager.getLogFilePath());
Enter fullscreen mode Exit fullscreen mode

It only retries things worth retrying by default network errors, timeouts, 429s, and 5xx and you can override that with a shouldRetry callback.

Where I need help

All labeled good first issue. CONTRIBUTING.md has the dev setup if you want to jump in.

npm install @aubaid/smart-retry
Repo: https://github.com/AubaidFarrukh/smart-retry issues, PRs, and "this API is weird" feedback all welcome.

Top comments (0)