Skip to main content
This release is 6 versions behind 17.1.3 — the latest version of @oak/oak. Jump to latest

@oak/oak@16.0.0
Built and signed on GitHub Actions

A middleware framework for handling HTTP with Deno, Node.js, Bun and Cloudflare Workers 🐿️🦕🥟⚙️

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
8 months ago (16.0.0)
class ServeContext

The context associated when dealing with serve middleware requests on an application.

Constructors

new
ServeContext(context: Context<S>)

Type Parameters

S extends State = State

Properties

readonly
app: Application<S>

A reference to the current application.

readonly
ip: string

Request remote address. When the application's .proxy is true, the X-Forwarded-For will be used to determine the requesting remote address.

readonly
ips: string[]

When the application's .proxy is true, this will be set to an array of IPs, ordered from upstream to downstream, based on the value of the header X-Forwarded-For. When false an empty array is returned.

readonly
state: S

The object to pass state to front-end views. This can be typed by supplying the generic state argument when creating a new app. For example:

const app = new Application<{ foo: string }>();

Or can be contextually inferred based on setting an initial state object:

const app = new Application({ state: { foo: "bar" } });

On each request/response cycle, the context's state is cloned from the application state. This means changes to the context's .state will be dropped when the request drops, but "defaults" can be applied to the application's state. Changes to the application's state though won't be reflected until the next request in the context's state.

Methods

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string): string
[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
options: any,
inspect: (
value: unknown,
options?: unknown,
) => string
,
): any
assert(
condition: unknown,
status?: ErrorStatus,
message?: string,
props?: Record<string, unknown> & Omit<HttpErrorOptions, "status">,
): asserts condition

Asserts the condition and if the condition fails, creates an HTTP error with the provided status (which defaults to 500). The error status by default will be set on the .response.status.

Because of limitation of TypeScript, any assertion type function requires specific type annotations, so the ServeContext type should be used even if it can be inferred from the context.

throw(
errorStatus: ErrorStatus,
message?: string,
props?: Record<string, unknown>,
): never

Create and throw an HTTP Error, which can be used to pass status information which can be caught by other middleware to send more meaningful error messages back to the client. The passed error status will be set on the .response.status by default as well.

Add Package

deno add jsr:@oak/oak

Import symbol

import { ServeContext } from "@oak/oak/serve";

---- OR ----

Import directly with a jsr specifier

import { ServeContext } from "jsr:@oak/oak/serve";

Add Package

npx jsr add @oak/oak

Import symbol

import { ServeContext } from "@oak/oak/serve";

Add Package

yarn dlx jsr add @oak/oak

Import symbol

import { ServeContext } from "@oak/oak/serve";

Add Package

pnpm dlx jsr add @oak/oak

Import symbol

import { ServeContext } from "@oak/oak/serve";

Add Package

bunx jsr add @oak/oak

Import symbol

import { ServeContext } from "@oak/oak/serve";