@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-clientimport { 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");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
| axios | ky | @mrzr/api-client | |
|---|---|---|---|
| Zero runtime dependencies | no | yes | yes |
| Built on | XHR / node:http | fetch | fetch |
| Retry with backoff | via axios-retry | built in | not yet |
| Coalesced token refresh | build it yourself | build it yourself | built in |
| Web Worker token isolation | no | no | yes |
| Cross-tab auth sync | no | no | yes |
| httpOnly cookie session restore | no | no | yes |
| Cancel by URL pattern / scope | no | no | yes |
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.
Getting started
Advanced
More in api layer