Provides an API for understanding information about the request being processed by the router.
a type which allows strongly typing the environment variables that are made available on the context used within handlers.
a type which is typically inferred from the route path selector which represents the shape of route parameters that are parsed from the route
QueryParams extends InferOutput<QSSchema>
a type which represents the shape of query parameters that are parsed from the search parameters of the request
RequestBody extends InferOutput<BSchema> | undefined
the shape of the parsed (and potentially validated) body
addr: Addr
The address information of the remote connection making the request as presented to the server.
cookies: SecureCookieMap
Provides a unified API to get and set cookies related to a request and
response. If the keys
property has been set when the router was created,
these cookies will be cryptographically signed and verified to prevent
tampering with their value.
Access to the environment variables in a runtime independent way.
In some runtimes, like Cloudflare Workers, the environment variables are
supplied on each request, where in some cases they are available from the
runtime environment via specific APIs. This always conforms the variables
into a Record<string, string>
which can be strongly typed when creating
the router instance if desired.
A globally unique identifier for the request event.
This can be used for logging and debugging purposes.
For automatically generated error responses, this identifier will be added
to the response as the X-Request-ID
header.
The parameters that have been parsed from the path following the syntax of path-to-regexp.
responseHeaders: Headers
The Headers
object which will be used to set headers on the
response.
userAgent: UserAgent
A representation of the parsed value of the
User-Agent
header if associated with the request. This can provide information about
the browser and device making the request.
[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string): string
Custom inspect method under Deno.
Custom inspect method under Node.js.
body(): Promise<RequestBody | undefined>
Attempts to read the body as JSON. If a schema was associated with the
route, the schema will be used to validate the body. If the body is invalid
and a invalid handler was specified, that will be called. If the body is
invalid and no invalid handler was specified, the method will throw as a
BadRequest
HTTP error, with the validation error as the cause
.
If the body is valid, it will be resolved. If there was no body, or the
body was already consumed, undefined
will be resolved. Requests which
have a method of GET
or HEAD
will always resolved with undefined
.
If more direct control of the body is required, use the methods directly
on the Request
on the .request
property of the context.
Will throw an HTTP error with the status of 409 Conflict
and the message
provided. If a cause
is provided, it will be included in the error as the
cause
property.
This is an appropriate response when a PUT
request is made to a resource
that cannot be updated because it is in a state that conflicts with the
request.
created<Location extends string,LocationParams extends ParamsDictionary = RouteParameters<Location>,>(): Response
Returns a Response
with the status of 201 Created
and the
body provided. If a location
is provided in the respond init, the
response will include a Location
header with the value of the location
.
If locationParams
is provided, the location
will be compiled with the
params
and the resulting value will be used as the value of the
Location
header. For example, if the location
is /book/:id
and the
params
is { id: "123" }
, the Location
header will be set to
/book/123
.
This is an appropriate response when a POST
request is made to create a
new resource.
Will throw an HTTP error with the status of 404 Not Found
and the message
provided. If a cause
is provided, it will be included in the error as the
cause
property.
This is an appropriate response when a resource is requested that does not exist.
queryParams(): Promise<QueryParams | undefined>
In addition to the value of .url.searchParams
, acorn can parse and
validate the search part of the requesting URL with the
qs library and any supplied query string
schema, which provides a more advanced way of parsing the search part of a
URL.
redirect<Location extends string,LocationParams extends ParamsDictionary = RouteParameters<Location>,>(location: Location,init?: RedirectInit<LocationParams>,): Response
Redirect the client to a new location. The location
can be a relative
path or an absolute URL. If the location
string includes parameters, the
params
should be provided in the init to interpolate the values into the
path.
For example if the location
is /book/:id
and the params
is { id: "123" }
, the resulting URL will be /book/123
.
The status defaults to 302 Found
, but can be set to any of the redirect
statuses via passing it in the init
.
sendEvents(options?: ServerSentEventTargetOptions & ResponseInit): ServerSentEventTarget
Initiate server sent events, returning a ServerSentEventTarget
which can be used to dispatch events to the client.
This will immediately finalize the response and send it to the client, which means that any value returned from the handler will be ignored. Any additional information to initialize the response should be passed as options to the method.
Throw an HTTP error with the specified status and message, along with any
options. If the status is not provided, it will default to 500 Internal Server Error
.
Upgrade the current connection to a web socket and return the
WebSocket
object to be able to communicate with the remote
client.
This is not supported in all runtimes and will throw if not supported.
This will immediately respond to the client to initiate the web socket connection meaning any value returned from the handler will be ignored.