Skip to main content

@oak/acorn@1.1.1
Built and signed on GitHub Actions

A focused RESTful server framework 🌰

This package works with Cloudflare Workers, Node.js, Deno, Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
JSR Score
100%
Published
4 days ago (1.1.1)
class Context

Provides an API for understanding information about the request being processed by the router.

Constructors

new
Context(
requestEvent: RequestEvent<Env>,
responseHeaders: Headers,
secure: boolean,
params: Params,
schema: Schema<QSSchema, BSchema, ResSchema>,
keys: KeyRing | undefined,
expose: boolean,
)

Type Parameters

Env extends Record<string, string>

a type which allows strongly typing the environment variables that are made available on the context used within handlers.

Params extends ParamsDictionary | undefined

a type which is typically inferred from the route path selector which represents the shape of route parameters that are parsed from the route

QSSchema extends QueryStringSchema
QueryParams extends InferOutput<QSSchema>

a type which represents the shape of query parameters that are parsed from the search parameters of the request

BSchema extends BodySchema
RequestBody extends InferOutput<BSchema> | undefined

the shape of the parsed (and potentially validated) body

ResSchema extends BodySchema

Properties

readonly
addr: Addr

The address information of the remote connection making the request as presented to the server.

readonly
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.

readonly
env: Env

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.

readonly
id: string

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.

readonly
params: Params

The parameters that have been parsed from the path following the syntax of path-to-regexp.

readonly
request: Request

The Request object associated with the request.

The Headers object which will be used to set headers on the response.

readonly
url: URL

The parsed form of the Request's URL.

readonly
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.

Methods

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string): string

Custom inspect method under Deno.

[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
options: any,
inspect: (
value: unknown,
options?: unknown,
) => string
,
): any

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.

conflict(
message?: string,
cause?: unknown,
): never

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>,
>
(
body: InferOutput<ResSchema>,
init?: RespondInit<Location, LocationParams>,
): 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.

notFound(
message?: string,
cause?: unknown,
): never

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.

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(
status?: ErrorStatus,
message?: string,
options?: HttpErrorOptions,
): never

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(options?: UpgradeWebSocketOptions): WebSocket

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.

Add Package

deno add jsr:@oak/acorn

Import symbol

import { Context } from "@oak/acorn";

---- OR ----

Import directly with a jsr specifier

import { Context } from "jsr:@oak/acorn";

Add Package

npx jsr add @oak/acorn

Import symbol

import { Context } from "@oak/acorn";

Add Package

yarn dlx jsr add @oak/acorn

Import symbol

import { Context } from "@oak/acorn";

Add Package

pnpm dlx jsr add @oak/acorn

Import symbol

import { Context } from "@oak/acorn";

Add Package

bunx jsr add @oak/acorn

Import symbol

import { Context } from "@oak/acorn";