Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

Context

import { Context } from "https://raw.githubusercontent.com/mandarineorg/mandarinets/master/deps.ts";

Provides context about the current request and response to middleware functions.

class Context<S extends State = Record<string, any>> {
constructor(
app: Application<S>,
serverRequest: ServerRequest | NativeRequest,
secure?,
);
app: Application<State>;
cookies: Cookies;
get isUpgradable(): boolean;
request: Request;
respond: boolean;
response: Response;
get socket(): WebSocket | undefined;
state: S;
 
assert(
condition: any,
errorStatus?: ErrorStatus,
message?: string,
props?: Record<string, unknown>,
): asserts condition;
send(options: ContextSendOptions): Promise<string | undefined>;
sendEvents(options?: ServerSentEventTargetOptions): ServerSentEventTarget;
throw(
errorStatus: ErrorStatus,
message?: string,
props?: Record<string, unknown>,
): never;
async upgrade(): Promise<WebSocket>;
}

§Type Parameters

§
S extends State = Record<string, any>
[src]

§Constructors

§
new Context(app: Application<S>, serverRequest: ServerRequest | NativeRequest, secure?)
[src]

§Properties

§
app: Application<State>
[src]

A reference to the current application.

§
cookies: Cookies
[src]

An object which allows access to cookies, mediating both the request and response.

§
isUpgradable: boolean readonly
[src]

Is true if the current connection is upgradeable to a web socket. Otherwise the value is false. Use .upgrade() to upgrade the connection and return the web socket.

§
request: Request
[src]

An object which contains information about the current request.

§
respond: boolean
[src]

Determines if the request should be responded to. If false when the middleware completes processing, the response will not be sent back to the requestor. Typically this is used if the middleware will take over low level processing of requests and responses, for example if using web sockets. This automatically gets set to false when the context is upgraded to a web socket via the .upgrade() method.

The default is true.

§
response: Response
[src]

An object which contains information about the response that will be sent when the middleware finishes processing.

§
socket: WebSocket | undefined readonly
[src]

If the the current context has been upgraded, then this will be set to with the web socket, otherwise it is undefined.

§
state: S
[src]

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

§
assert(condition: any, errorStatus?: ErrorStatus, message?: string, props?: Record<string, unknown>): asserts condition
[src]

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.

§
send(options: ContextSendOptions): Promise<string | undefined>
[src]

Asynchronously fulfill a response with a file from the local file system.

If the options.path is not supplied, the file to be sent will default to this .request.url.pathname.

Requires Deno read permission.

§
sendEvents(options?: ServerSentEventTargetOptions): ServerSentEventTarget
[src]

Convert the connection to stream events, returning an event target for sending server sent events. Events dispatched on the returned target will be sent to the client and be available in the client's EventSource that initiated the connection.

This will set .respond to false.

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

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.

§
upgrade(): Promise<WebSocket>
[src]

Take the current request and upgrade it to a web socket, resolving with the web socket object. This will set .respond to false.