createExecutor(transport, options?)
Low-level factory used internally by createFetchExecutor, @routar/axios, and @routar/ky. Use this to integrate any HTTP client with routar’s plugin system.
import { createExecutor, definePlugin, logger } from '@routar/core'
const authPlugin = definePlugin({
name: 'auth',
onRequest: async (opts) => ({
...opts,
headers: { ...opts.headers, Authorization: `Bearer ${await getToken()}` },
}),
})
const executor = createExecutor(
async ({ method, url, params, body, headers, signal }) => {
const res = await myClient.request({ method, url, params, body, headers, signal })
return res.data
},
{ plugins: [authPlugin, logger()] },
)Parameters
| Parameter | Type | Description |
|---|---|---|
transport | (opts: ExecuteOptions) => Promise<unknown> | The actual HTTP call |
options.plugins | ExecutorPlugin[] | Plugins applied in declaration order (first is outermost). See Plugins. |
options.unwrap | (raw: unknown) => unknown | Transforms the raw response before schema validation. Use to unwrap envelope shapes like { data: T }. |
For retry and timeout, use the options on createFetchExecutor. For axios and ky, configure them on the underlying instance.
Unwrapping envelope responses
Many APIs wrap payloads in an envelope such as { data: T }. Pass unwrap to strip the envelope before your response schema runs — no boilerplate plugin needed:
const executor = createExecutor(transport, {
unwrap: (raw) => (raw as { data: unknown }).data,
})unwrap runs as the innermost onResponse hook: immediately after the transport returns, before any plugin onResponse hooks and before createApi validates the response. It is available on createExecutor, createFetchExecutor, @routar/axios, and @routar/ky.
ExecuteOptions
The object passed to your transport function:
interface ExecuteOptions {
method: 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE'
url: string
params?: Record<string, unknown> // query string parameters
body?: unknown
headers?: Record<string, string>
signal?: AbortSignal
}params contains the query fields from your endpoint’s request schema. Serialize them as a query string when constructing the request URL.
To select the transport dynamically at request time (e.g. SSR vs CSR), see dispatchExecutor().