Getting started

These docs target 0.2.0, pending publication on npm’s latest dist-tag. Use the commands below after the release is published.

bun add fetchzen

The package is ESM-only, with no runtime dependencies. Use Node.js 20+ or a modern browser, Bun, Deno, or edge runtime with the standard Fetch APIs.

Run this in a TypeScript module. The public endpoint returns a plain JSON object, so choose rest explicitly:

import { Http, rest } from "fetchzen";
type User = { id: number; name: string };
const api = Http.create("https://jsonplaceholder.typicode.com", {
preset: rest,
cache: false,
});
const { data, status } = await api.get<User>("/users/1");
console.log(data.name, status);

rest returns { data, status, headers, response }. The generic describes the expected JSON; it does not validate server data. Add a schema when you need runtime validation.

cache: false makes this client independent. To share one across your app, follow the shared-client recipe. If you omit preset, the default is envelope, which uses payload instead of data and checks API failure conventions. See presets.

Using the same api, call safe.get with the same arguments:

const { data: result, error } = await api.safe.get<User>("/users/1");
if (error) {
console.error(error.message);
} else {
console.log(result.data.name);
}

Safe methods return either a preset-shaped data result or an Error. They still run validation and error hooks. Ordinary methods reject on failure; see errors to inspect HTTP failures or use try/catch.