@mrzr/api-client

A fetch client built around secure browser auth.

Coalesced token refresh, Web Worker token isolation and cross-tab session sync. Zero runtime dependencies, and it runs in every JavaScript runtime.

npm install @mrzr/api-client
Version
v3.1.0
Published
Oct 2026
License
MIT
Requirements
Any runtime with fetch and AbortController: modern browsers, Node 20+, Deno, Bun, Cloudflare Workers.
Create a clientts
import { createClient } from "@mrzr/api-client";

export const api = createClient({ baseUrl: "https://api.example.com" });

// Tokens are captured, stored, refreshed and rotated across tabs for you.
await api.login({ email, password });
const { data } = await api.get<User[]>("/users");
Under TanStack Queryts
useQuery({
  queryKey: ["users"],
  queryFn: ({ signal }) => api.get<User[]>("/users", { signal }).then((r) => r.data),
});

Failures reject with a typed ApiError. Cancellation resolves with canceled: true, so a route change never looks like an error.

What it does

Coalesced refresh

50 simultaneous 401s trigger exactly one refresh call: a shared promise, not a polling loop.

Web Worker isolation

Requests run in a worker by default, so tokens never enter the main-thread heap.

Cross-tab sync

Login, logout and refresh travel over BroadcastChannel, and Web Locks make tabs take turns refreshing.

httpOnly cookie mode

restoreSession() answers the “am I logged in?” question cookies make unanswerable from JS.

Real cancellation

Cancel by URL pattern, scope or key on page change or modal close, in worker mode too.

Uploads and sockets

FormData, Blob and streams with refresh mid-upload, and server-issued socket tickets.

Is this for you?

Use it if

  • Your access token expires and 20 concurrent requests each fire their own refresh
  • You want tokens off the main thread, where XSS can’t read them
  • Logging out in one tab should log out the others
  • You use httpOnly cookies and can’t tell on page load whether a session exists
  • You need refresh to work during a five-minute file upload

Use something else if not

For a small fetch wrapper with built-in retry, use ky. For the widest legacy support and ecosystem, use axios. Neither focuses on browser auth.

How it compares

axiosky@mrzr/api-client
Zero runtime dependenciesnoyesyes
Built onXHR / node:httpfetchfetch
Retry with backoffvia axios-retrybuilt innot yet
Coalesced token refreshbuild it yourselfbuild it yourselfbuilt in
Web Worker token isolationnonoyes
Cross-tab auth syncnonoyes
httpOnly cookie session restorenonoyes
Cancel by URL pattern / scopenonoyes

Retry is not implemented yet: it has to work correctly with refresh-and-retry, cancellation and takeLatest, and shipping it half-right would be worse than not shipping it.

Documentation

Everything is documented.

Start reading

More in api layer