Getting started
Install
Section titled “Install”These docs target 0.2.0, pending publication on npm’s latest dist-tag.
Use the commands below after the release is published.
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.
Make a typed request
Section titled “Make a typed request”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.
Handle errors without try/catch
Section titled “Handle errors without try/catch”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.
Next steps
Section titled “Next steps”- Requests and responses: URLs, bodies, and parsing.
- Share a typed client: defaults, configuration, and SSR isolation.
- Validation: check and transform server data.
- Timeouts and retries: cancellation and retry behavior.
- Try the focused JSON and safe errors examples.