Request context
RequestContext is one object per request. Handlers receive it as their second argument, guards and interceptors as their first. Everything on it is the same on every adapter except raw.
Most members are created on first access, so a request that never reads cookies or the query string pays nothing for them.
Request data
ctx.urlParams and ctx.query hold what the client sent, as strings. They are what a guard reads, because guards run before validation. In a handler, use the validated values in the first argument instead. The decoded body is not on the context: it is not read until guards have passed. Its bytes are, as ctx.rawBody().
Path, URL and address
ctx.path is made of the route and the URL parameters, so it is the path that was matched whatever the request target looked like: /%61dmin, http://any.host/admin and a request with an invalid Host header all have the path /admin, on every adapter. A guard may compare it. Comparing ctx.route.path, the declared path, is simpler still when parameters do not matter.
ctx.url.host is what the client says in its Host header, or localhost when that is not a valid host. Do not build links that you send to somebody else from it.
ctx.ip is the address of the connection. Behind a proxy that is the proxy's address, unless the adapter is told which proxies to trust: set trustProxy on Fastify; Bun has no such option, so read the header your proxy sets. It is undefined for app.inject() without a remoteAddress.
The raw body
await ctx.rawBody() gives the bytes of the request body exactly as they were sent. Use it where the bytes matter, such as the signature of a webhook:
Reading the bytes does not keep the body from being decoded: with this guard on it, the handler gets data as usual. The body is read from the connection once, and the adapter's limit holds: a body over it is a 413.
It works for any content type, also on an endpoint without a requestSchema, and before or after the body is decoded. The one exception is a multipart body, which is not kept in one piece: read its bytes before it is decoded, in a guard.
The abort signal
Pass ctx.signal to work that should stop when the client goes away:
Reply controls
ctx.reply sets response headers and cookies from a guard, an interceptor or a handler.
header(name, value): names are lower-cased, and setting a name twice keeps the last value. A value is a string or a number. A list of values is sent as one header line per value.append(name, value): adds a value next to those the header has. Use it to forward severalset-cookieorlinkheaders of another response.cookie(name, value, options?): adds aSet-Cookieheader, in place of an earlier one with the same name, domain and path. The value is URL-encoded. Options aredomain,path(defaults to/),expires,maxAge(seconds),httpOnly,secure,sameSite('strict','lax'or'none') andpartitioned.deleteCookie(name, { domain, path }): makes the browser delete a cookie. Pass the domain and path it was set with.- Names and values are checked when they are set: a line break in a header, a
;in a cookie's path,sameSite: 'none'withoutsecure, or a__Host-cookie with a domain throws there, in your code. - Headers and cookies set this way are sent on error responses too, except those that describe the response that failed:
content-type,content-encoding,content-dispositionand the like,etag,last-modified,expires, acontent-rangeother thanbytes */<size>, and acache-controlthat lets a cache keep the response. One that only restricts stays:no-store,no-cache,max-age=0, orprivatewithout a lifetime. A header that an error response sets itself wins.
There is no way to set the status here. The status comes from the declaration: return reply(status, body) for a declared status, or throw an HttpException.
When the response has finished
ctx.onFinish(callback) runs a callback once the response is done: the body has been sent, a stream has ended, or the client has gone away. By then ctx.responseStatus holds the final status, including the status an error was mapped to. An interceptor uses the pair to write an access log or end a span:
Callbacks run in reverse registration order, after a streamed body has been closed and before per-request state is disposed. A callback may register another one. One registered after the response has finished runs at once. What a callback throws is logged. ctx.responseStatus is undefined until the response is known.
When app.close() has to give up on a request, its callbacks run and its state is disposed before the application's own services are.
Per-request state
ctx.get, ctx.find, ctx.set and ctx.has read and write typed per-request slots, and ctx.use and ctx.resolve reach request-scoped services. Both are described in Per-request state.
Native objects
ctx.raw holds the adapter's own request objects.
Without further setup ctx.raw is unknown. Its type comes from one declaration in the application, which tells TypeScript which adapter the application runs on:
On Bun the type is BunEnvironment from @navios/adapter-bun.
- The declaration also types
app.getServer()and the options ofapp.listen().NaviosApplicationwithout an argument is then an application on that adapter; a helper that takes an application on any adapter, such as one for tests, takesNaviosApplication<any>. - An adapter does not register itself when imported, because a shared package may have both adapters installed. The application decides.
- Code that must compile without the declaration, such as a library used on several adapters, narrows explicitly:
const { request } = ctx.raw as FastifyEnvironment['raw'].
Code that uses ctx.raw runs on one adapter only. Prefer the adapter-neutral members when they cover the need. What each adapter exposes is described on the Fastify and Bun pages.
Reading the context without passing it
Code that cannot receive the context as an argument, such as a logger or a repository several calls deep, can read it with currentContext(). This relies on an AsyncLocalStorage, which costs time on every request, so it is off until the application asks for it:
It is the request in everything that runs for it: guards, interceptors, the handler, the body of a stream, error mappers, onFinish callbacks and dispose hooks. currentContext() returns undefined outside a request, and always when the application was created without asyncContext: true.