Skip to content

Make fetch resilient

@vereda/http

Make fetch resilient.

Retries, backoff, timeouts, per-host isolation and circuit breaking for fetch on Node.js, Bun and Cloudflare Workers, with typed results instead of thrown errors. Zero runtime dependencies.

npm install @vereda/http

Every PR runs the suite on Node 22, Node 24 and Bun, and a smoke test in workerd. Support policy →

A host slows down at 8 st = 0 s
1.00×load, plain retries · peak 3.66×
1.00×load, with a retry budget · peak 1.10×
Both clients retry up to 3 times. Without a budget, retries keep the host overloaded even after it is fixed at 24 s. Play with it →

The loop you'd write by hand

fetch makes one attempt. Everything after that is yours to write, and the comments are the questions it leaves open.

Even once it's correct, the loop still has:

  • no limit on how many retries pile onto a struggling host,
  • no way to cancel,
  • errors you can only tell apart by parsing a message.

Vereda is that loop written carefully, once.

Compared with ky, axios and others →

by-hand.ts
async function getUser(id: string) {
for (let attempt = 0; ; attempt++) {
try {
const res = await fetch(`https://api.example.com/users/${id}`, {
signal: AbortSignal.timeout(5_000),
});
if (res.ok) return await res.json();
throw new Error(`HTTP ${res.status}`); // 404 or 503? retry both?
} catch (err) {
// timeout, DNS failure, or the throw above? caught all the same
// is this request safe to repeat? what if it were a POST?
if (attempt === 3) throw err;
}
await new Promise((r) => setTimeout(r, 200 * 2 ** attempt)); // jitter? Retry-After?
}
}

One call, every safeguard

A dropped connection, a timeout or a 503 is retried up to three times, with jittered exponential backoff, before your code sees an error.

attemptMs bounds each attempt. An optional totalMs deadline bounds the whole call.

The result is a value, not an exception: result.success or a typed result.error.kindyou can switch on.

Results and errors →

quickstart.ts
import { HttpClient } from "@vereda/http";
const api = HttpClient.create({
baseUrl: "https://api.example.com",
timeout: { attemptMs: 5_000 },
});
const result = await api.get("/users/42").toPromise(); // never rejects
if (result.success) {
const user = await result.raw.json();
} else {
console.error(result.error.kind, result.error.message); // a typed error
}

Patterns, animated

Each simulator runs the same traffic with and without the pattern. A test checks every model against the real client.

Go deeper