Configuration
Client Config (ApiConfig)
| Option | Type | Required | Description |
|---|---|---|---|
baseUrl | string | β | Base URL prepended to all endpoint paths |
defaultHeaders | Record<string, string> | β | Headers sent with every request |
fetch | typeof fetch | β | Custom fetch instance (for mocking, polyfills, etc.) |
hooks | Hooks | β | Global hooks applied to every request |
plugins | Plugin[] | β | Plugins to extend functionality |
const api = createApiClient(endpoints, {
baseUrl: 'https://api.example.com',
defaultHeaders: {
'Content-Type': 'application/json',
'X-Client-Version': '1.0.0',
},
fetch: customFetch,
hooks: {
beforeRequest: async (url, init) => ({ url, init }),
afterResponse: async (response) => response,
onError: async (error) => console.error(error),
},
plugins: [cache({ ttl: 300 }), retryPlugin({ maxRetries: 3 })],
});Endpoint Config
| Property | Type | Required | Description |
|---|---|---|---|
method | 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | β | HTTP method |
path | string | ((input: TInput) => string) | β | Static path or function returning a path |
handler | CustomHandler<TInput, TOutput> | β | Custom handler for non-JSON or special requests |
hooks | Hooks | β | Endpoint-specific hooks |
Prefer the get(), post(), etc. helpers over constructing endpoint configs manually β they set method for you.
Group Config
| Property | Type | Description |
|---|---|---|
endpoints | Record<string, EndpointConfig> | Direct endpoint children of this group |
groups | Record<string, GroupConfig> | Nested sub-groups |
hooks | Hooks | Hooks applied to all endpoints in this group (and sub-groups) |
basePath | string | Path prefix prepended to all endpoint paths in this group (recursive) |
Hooks Interface
type Hooks = {
beforeRequest?: (url: string, init: RequestInit) => Promise<{ url: string; init: RequestInit }>;
afterResponse?: (response: Response, url: string, init: RequestInit) => Promise<Response>;
onError?: (error: unknown) => Promise<void> | void;
};