API Reference
Functions
createApiClient(endpoints, config)
Creates a type-safe API client.
function createApiClient<TEndpoints extends EndpointDefinitions>(
endpoints: TEndpoints,
config: ApiConfig
): Client<TEndpoints>get / post / put / patch / del
Create endpoint configurations for each HTTP method:
function get<TInput = void, TOutput = any, TError = any>(
path: string | ((input: TInput) => string),
handler?: CustomHandler<TInput, TOutput>,
hooks?: Hooks
): EndpointConfig<TInput, TOutput, TError>post, put, patch share the same signature but TInput has no default (body is required).
del defaults TInput to void.
endpoint(config)
Creates an endpoint config with full control:
function endpoint<TInput = any, TOutput = any, TError = any>(
config: EndpointConfig<TInput, TOutput, TError>
): EndpointConfig<TInput, TOutput, TError>group(config)
Creates a group configuration (type helper — returns the same object):
function group<T extends GroupConfig>(config: T): TcreatePlugin(name, factory)
Creates a reusable plugin:
function createPlugin<TConfig = void>(
name: string,
factory: (config: TConfig) => PluginOptions
): (config: TConfig) => PluginTypes
ApiConfig
type ApiConfig = {
baseUrl: string;
fetch?: typeof fetch;
defaultHeaders?: HeadersInit;
hooks?: Hooks;
plugins?: Plugin[];
};EndpointConfig
type EndpointConfig<TInput = any, TOutput = any, TError = any> = {
method: HttpMethod;
path: string | ((input: TInput) => string);
hooks?: Hooks;
handler?: CustomHandler<TInput, TOutput>;
};GroupConfig
type GroupConfig = {
hooks?: Hooks;
basePath?: string;
endpoints?: Record<string, EndpointConfig>;
groups?: Record<string, GroupConfig>;
};Hooks
type Hooks = {
beforeRequest?: (
url: string,
init: RequestInit
) => Promise<{ url: string; init: RequestInit }> | { url: string; init: RequestInit };
afterResponse?: (
response: Response,
url: string,
init: RequestInit
) => Promise<Response> | Response;
onError?: (error: unknown) => Promise<void> | void;
};CustomHandler
type CustomHandler<TInput, TOutput> = (context: {
input: TInput;
fetch: typeof fetch;
method: HttpMethod;
path: string;
baseUrl: string;
}) => Promise<TOutput>;PluginOptions
type PluginOptions = {
hooks?: Hooks;
handlerWrapper?: (
originalHandler: (input: any, context: any) => Promise<any>
) => (input: any, context: any) => Promise<any>;
methods?: Record<string, (...args: any[]) => any>;
};HttpMethod
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';