createApi(executor, router)
Produces a fully-typed API client. Each endpoint becomes an async function with the signature (params, signal?) => Promise<Response>.
import { createApi } from '@routar/core'
const todoApi = createApi(executor, todoRouter)
// params follow { path?, query?, body? }
await todoApi.getDetail({ path: { id: 1 } })
await todoApi.create({ body: { title: 'buy milk' } })
await todoApi.update({ path: { id: 1 }, body: { completed: true }, query: { version: 2 } })Overloads
| Form | Usage |
|---|---|
createApi(executor, router) | Pre-built router via defineRouter — preferred for multi-environment setups |
createApi(executor, prefix, endpoints) | Inline endpoints with a URL prefix |
createApi(executor, endpoints) | Inline endpoints without a prefix — simplest form for small APIs |
// with a pre-built router
createApi(executor, todoRouter)
// inline with prefix
createApi(executor, '/todos', { getList: endpoint({ ... }) })
// inline without prefix
createApi(executor, { getList: endpoint({ ... }) })Parameters
| Parameter | Type | Description |
|---|---|---|
executor | Executor | HTTP transport. Create with createFetchExecutor or createAxiosExecutor. |
router | RouterDef | RouterEndpoints | Router definition or inline endpoints record |
prefix | string (optional) | URL prefix when passing endpoints inline |
Request params
Request params map to { path?, query?, body? } matching the request schema shape:
// request: z.object({ path: z.object({ id: z.number() }) })
await api.getDetail({ path: { id: 1 } })
// request: z.object({ query: z.object({ page: z.number() }) })
await api.getList({ query: { page: 2 } })
// request: z.object({ body: z.object({ title: z.string() }) })
await api.create({ body: { title: 'buy milk' } })AbortSignal
Pass an AbortSignal as the second argument to cancel a request:
const controller = new AbortController()
const result = await todoApi.getList({}, controller.signal)
controller.abort() // cancels the requestSSR/CSR pattern
The typical pattern is to create two clients from the same router — one for CSR, one for SSR:
export const todoApi = createApi(clientExecutor, todoRouter) // CSR
export const todoServerApi = createApi(serverExecutor, todoRouter) // SSRSee the SSR/CSR guide for a complete example.
Last updated on