Middleware Configuration
Middleware functions are defined on the source. They run in order for every request, on the server: for a call through the proxy route as for one made on the server.
import { defineApi } from "@noverax/courier/define"
import { authMiddleware, logMiddleware } from "@noverax/courier/middleware"
import { defineMiddleware, isHTTPError } from "@noverax/courier/server"
// `log` takes each line: here, the docs' request inspector; in an app, your logger
const middlewareApi = defineApi({
baseURL: "http://localhost:4000",
middleware: [
logMiddleware({ logger: log }),
// A made-up token: the backend reads none, and the inspector names its header
authMiddleware({ getToken: () => "made-up-docs-token" }),
// Custom: stamp every answer that is a record
defineMiddleware("stamp", "after", (_ctx, response) => ({
...response,
data: { ...(response.data as object), _meta: { timestamp: new Date().toISOString() } },
})),
// Custom: tell a failure, then pass it on
defineMiddleware("failures", "wrap", async (ctx, next) => {
try {
return await next()
} catch (error) {
log(`[${ctx.source}] ${ctx.group}.${ctx.method} failed: ${isHTTPError(error) ? error.status : "error"}`)
throw error
}
}),
],
groups: { users, misc },
})This is the chain the sandboxes below call through. Under each, its requests show the chain link by link — each with its phase and the time it took, the header the auth link added, by name alone — and the lines the chain logged.
Response Transformation
Middleware can transform responses: the stamp adds _meta to every answer that is a record.
// The stamp adds _meta to what the API answered
const data = await middlewareCaller.users.getAll()
// data now holds:
// {
// data: [...users],
// total: 3,
// page: 1,
// limit: 10,
// _meta: { timestamp: "2026-11-07T12:00:00.000Z" }
// }// run the request to see the responseRequests
as the browser recorded them and the site's server told of them
Error Handling
Wrap next() in try/catch to handle errors: the failures link tells the failure, then passes it on, and the caller still catches the HTTPError.
// The failures link tells a failure, then passes it on to the caller
defineMiddleware("failures", "wrap", async (ctx, next) => {
try {
return await next()
} catch (error) {
log(`[${ctx.source}] ${ctx.group}.${ctx.method} failed: ${isHTTPError(error) ? error.status : "error"}`)
throw error // the caller still gets the HTTPError
}
})// run the request to see the responseRequests
as the browser recorded them and the site's server told of them
Built-in Log Middleware
Use logMiddleware() for automatic request logging with timing.
// The built-in log middleware: one line a call, with its status and time
import { logMiddleware } from "@noverax/courier/middleware"
logMiddleware({ logger: (line) => log(line) })
// middlewareApi:users:getAll → 200 (5ms)logMiddleware writes one line a call: its source, group and endpoint, its status and how long it took. Here its logger hands each line to the request inspector — under each sandbox above, beside the call it is about — rather than to the server's own log, which a visitor cannot read.