@fgrzl/fetch
v1.4.2
Published
A modern, type-safe HTTP client with middleware support for CSRF protection and authentication
Maintainers
Readme
@fgrzl/fetch
A tiny, TypeScript-first wrapper around fetch that adds typed responses and composable middleware.
What
- Lightweight HTTP client built on the browser
fetchAPI. - Returns a consistent response shape (
ok,data,error). - Built-in, optional middleware: auth, retry, cache, logging, CSRF, rate-limit.
Why
- Removes repetitive request boilerplate (base URLs, headers, retries).
- Keeps runtime small and TypeScript-friendly.
- Compose only the middleware you need — zero-config defaults when useful.
How
- Install
npm install @fgrzl/fetch- Quick example
import api from "@fgrzl/fetch";
api.setBaseUrl("https://api.example.com");
const res = await api.get<{ id: number; name: string }>("/users");
if (res.ok) console.log(res.data);Examples
Set base URL
api.setBaseUrl("https://api.example.com");
await api.get("/users");POST with JSON
const created = await api.post("/users", { name: "Ava" });Typed response
interface User {
id: number;
name: string;
}
const r = await api.get<User>("/me");
if (r.ok) r.data.name;Add authentication middleware
import { addAuthentication } from "@fgrzl/fetch/middleware/authentication";
const authed = addAuthentication(api, {
tokenProvider: () => localStorage.getItem("token") || "",
});
await authed.get("/private");Cancel / timeout
const c = new AbortController();
api.get("/data", {}, { signal: c.signal, timeout: 5000 });
c.abort();Docs: docs/getting-started.md
