Skip to content

createClient

createClient(config?): Client

Defined in: packages/fetch-kit/src/create-client.ts:198

Create a typed HTTP client.

Built on fetch, it adds: timeouts via AbortController, configurable retry with backoff, typed error classes, request/response interceptors, optional schema validation, response caching, in-flight request deduplication, and GraphQL support.

ClientConfig = {}

Client

const api = createClient({ baseUrl: "https://api.example.com" });
const user = await api.get<User>("/users/me");
const api = createClient({
baseUrl: "/api",
timeout: 10_000,
retry: { attempts: 3, backoff: "exponential" },
// auth() returns the full Authorization header value. Pick your scheme.
auth: () => {
const token = localStorage.getItem("token");
return token ? `Bearer ${token}` : null;
},
onError: (err) => logger.error(err),
});
// Or with a custom header / scheme, no string concatenation:
createClient({ auth: () => ({ header: "X-Api-Key", token: key }) });
createClient({ auth: () => ({ scheme: "Token", token: key }) });
const api = createClient({
baseUrl: "https://api.example.com",
cache: { ttl: 30_000 }, // 30s default TTL, in-memory LRU
dedupe: true, // share in-flight identical requests
graphqlEndpoint: "/graphql",
});
const me = await api.graphql<{ me: User }>(`query { me { id name } }`);
import { z } from "zod";
const UserSchema = z.object({ id: z.string(), email: z.string().email() });
const user = await api.get("/users/me", { schema: UserSchema });