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. |
For retry and timeout, use the options on createFetchExecutor. For axios and ky, configure them on the underlying instance.
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().
Last updated on