Quick start
Create a client, define three typed requests and send them. Every snippet runs as is in a browser, Node.js 18+, Bun or Deno, against a public test API.
1. Install
- npm
- yarn
- pnpm
npm install @hyper-fetch/core
yarn add @hyper-fetch/core
pnpm add @hyper-fetch/core
2. Create the client
Create one client per backend and export it. It stores the base URL, the adapter (the built-in fetch adapter unless you pick another), the cache and the request queues.
import { createClient } from "@hyper-fetch/core";
type ApiError = { message: string };
export const client = createClient<{ error: ApiError | Error }>({
url: "https://jsonplaceholder.typicode.com",
});
The error generic types the error of every request. Keep Error in the union, because timeouts, aborts and network
failures arrive as Error instances.
3. Define requests
A request describes one endpoint. Pass its types in the first call and its options in the second. The split lets
TypeScript read :userId from the endpoint string and require it when you send.
import { client } from "./client";
export type User = { id: number; name: string; email: string };
export const getUsers = client.createRequest<{
response: User[];
queryParams?: { _limit?: number };
}>()({ endpoint: "/users" });
export const getUser = client.createRequest<{ response: User }>()({
endpoint: "/users/:userId",
});
export const createUser = client.createRequest<{
response: User;
payload: Omit<User, "id">;
}>()({ endpoint: "/users", method: "POST" });
Define requests once, at module level. They are templates: you reuse and derive them, you never recreate them.
4. Send them
send() resolves with the data, the error and the status. It never rejects, so you branch on the result.
import { getUsers, getUser, createUser } from "./api/users";
// GET /users?_limit=3
const users = await getUsers.send({ queryParams: { _limit: 3 } });
console.log(users.data); // User[] | null
// GET /users/1, `params` is required and typed by the endpoint
const { data, error, status } = await getUser.send({ params: { userId: 1 } });
if (data) {
console.log(data.name); // "Leanne Graham"
} else {
console.error(status, error); // ApiError | Error | null
}
// POST /users with a typed payload
const created = await createUser.send({
payload: { name: "Ann", email: "ann@example.com" },
});
console.log(created.status); // 201
Requests are immutable, so you can also set values ahead of time and keep the result:
const getFirstUser = getUser.setParams({ userId: 1 });
await getFirstUser.send(); // no options needed anymore
What you get without configuring anything
| Behavior | Default |
|---|---|
| Deduplication | Identical GET requests in flight at the same time share one call |
| Cache | Successful responses are stored for 5 minutes and fresh for 5 minutes |
| Offline | Requests sent while offline wait for the connection instead of failing |
| Timeout | The fetch adapter aborts a request after 5 seconds |
| Retries | Off. Turn them on per request with retry and retryTime |
On the server, the client does not cache by default, so data of one user never reaches another. Give a request a scope
with setScope(id) to cache it per user, see Cache.
What just happened
- The client holds the base URL, the fetch adapter, the cache and two dispatchers.
- Each request is a typed, immutable template. Setters return a new request.
send()went through a dispatcher, the adapter and the cache, and resolved with{ data, error, status }.