v0.2.0No runtime dependenciesTypeScriptMIT

fast-http-client

A small HTTP client for browsers and Node, built on fetch, with the things production code ends up needing.

Terminal
npm install fast-http-client

What it does

  • Retries

    Idempotent requests are retried with exponential backoff and jitter, and Retry-After is honoured.

  • Circuit breaker

    One circuit per origin. A failing service gets a rest, and callers fail fast instead of waiting.

  • Caching

    GET responses cached in memory, IndexedDB, Redis or a store of your own, with a TTL per request.

  • Interceptors

    Change every request on its way out and every response on its way back, or recover from an error.

  • Token refresh

    A 401 calls your refresh function once, however many requests are waiting, then replays them.

  • Timeouts and cancellation

    A timeout on every attempt, an AbortSignal to cancel, and typed errors for each way a request fails.

A first request

Create one client per API and share it. Retries, the circuit breaker and the cache all live on the client.

api.ts
import { createHttp, HttpError } from "fast-http-client";

const api = createHttp({
  baseUrl: "https://api.example.com/v1",
  retry: { attempts: 3 },
  cache: "memory",
});

interface User {
  id: string;
  name: string;
}

try {
  const { data: user } = await api.get<User>("/users/42");
  console.log(user.name);
} catch (err) {
  if (err instanceof HttpError && err.status === 404) {
    console.log("No such user");
  }
}

Read the guide for every option, or start with the quick start.