SDK
An SDK describes a whole API as one TypeScript type and gives you its requests by dot notation, such as
sdk.users.$userId.$get. createSdk(client) builds each request on access with a proxy, so nothing is created up front.
Use it for APIs with many endpoints, or generate the schema from OpenAPI with the CLI.
- One schema for the whole API, with response, payload, query params and endpoint per leaf.
- Typed dot notation: paths are keys,
$paramkeys are URL params,$getand friends are methods. - Central defaults per endpoint with
$configure, checked against the schema.
import { createClient, createSdk, type RequestModel } from "@hyper-fetch/core";
type User = { id: string; name: string };
type ApiSchema = {
users: {
$get: RequestModel<{
response: User[];
queryParams?: { page?: number } | null;
endpoint: "/users";
}>;
$userId: {
$get: RequestModel<{ response: User; endpoint: "/users/:userId" }>;
$delete: RequestModel<{ response: User; endpoint: "/users/:userId" }>;
};
};
};
export const client = createClient({ url: "https://api.example.com" });
export const sdk = createSdk<typeof client, ApiSchema>(client);
// GET /users/123
const { data } = await sdk.users.$userId.$get.send({
params: { userId: "123" },
});
Every leaf is a regular request: send(), setParams, useFetch and useSubmit all work with it.
Write the schema
- Path keys become URL segments. camelCase keys turn into kebab-case:
userProfilesis/user-profiles. $namekeys below a path are URL params:$userIdis/:userId.$get,$post,$put,$patch,$deleteare the methods, upper-cased bymethodTransform.- Leaves are
RequestModel<{ response, payload, queryParams, error, endpoint }>. You do not pass the client:createSdkadds it, with its adapter and global error type.
Nested keys must not be named like a request property (send, cache, method, params and so on), because the request
property wins. The types mark such a key with SdkReservedKeyError, so rename it in the schema.
RequestModel or RequestInstance
Both describe requests at the type level, for opposite jobs.
RequestModel<{...}> | RequestInstance<{...}> | |
|---|---|---|
| Describes | This one endpoint | Any request matching a partial shape |
| Use for | SDK schema leaves, single-endpoint types | Generic constraints of helpers and components |
| Omitted fields default to | Strict types such as unknown, Error and false | any, so any concrete request satisfies it |
// This endpoint: no payload, no query params
type GetUsers = RequestModel<{ response: User[]; endpoint: "/users" }>;
// Any request that returns User[], whatever its payload or query params
function countCachedUsers<T extends RequestInstance<{ response: User[] }>>(
request: T,
) {
return request.read()?.data?.length ?? 0;
}
See extend Request for the helper pattern.
Configure endpoints with $configure
$configure sets request defaults per endpoint and returns a new SDK. Values are an object of options, or a function
that receives the request and returns a changed one, with access to every setter.
export const api = sdk.$configure({
"*": { retry: 2 }, // every request
"/users/*": { staleTime: 60_000 }, // /users/:userId and deeper
// one SDK leaf
"users.$userId.$delete": (request) => request.setCancelable(true),
});
| Key pattern | Matches | Applied |
|---|---|---|
"*" | Every request | First |
"/users" | Every method of that exact endpoint | Second |
"/users/*" | Endpoints below /users | Second |
"users.$userId.$get" | One SDK leaf | Last |
Later matches override earlier ones. Dot-path keys are checked against the schema, so a typo fails to compile. Object
values accept headers, auth, cache, cacheTime, staleTime, retry, retryTime, cancelable, queued,
offline, deduplicate, deduplicateTime and structuralSharing.
Split a large configuration by domain with createConfiguration, then chain the $configure calls:
import { createConfiguration } from "@hyper-fetch/core";
export const usersConfig = createConfiguration<ApiSchema>()({
"users.$get": { staleTime: 30_000 },
});
// api/sdk.ts
export const configuredSdk = sdk.$configure(usersConfig);
Options
| Option | Default | Description |
|---|---|---|
camelCaseToKebabCase | true | Turn camelCase keys into kebab-case segments (userProfiles → user-profiles) |
pathSegments | none | URL segment of a key when it differs, such as { findByStatus: "findByStatus" } |
methodTransform | (method) => method.toUpperCase() | Turn a $ key into the request method |
defaults | none | The same map as $configure |
Prefer $configure over defaults: its keys are type-checked against the schema.
Keep a reference to a leaf
Every property access creates a new request. When you attach a mock or $hooks, store the request first and use that
variable:
const getUsers = sdk.users.$get;
getUsers.setMock(() => ({ data: [], status: 200 }));
await getUsers.send();
- Describe the API as a schema of
RequestModelleaves and callcreateSdk<typeof client, Schema>(client). - Paths are keys,
$paramkeys are URL params,$get,$postand the others are methods. - Set defaults per endpoint with
$configureorcreateConfiguration. Keep leaves in variables before mocking them.
Next steps
Runnable examples: Typed SDK and OpenAPI codegen.