Context

The small c in Corpus. The Context class is the request context object passed to route handlers. It provides typed access to the request data, headers, cookies, parsed body, URL parameters, search parameters, and response manipulation. The four generics (B, S, P, R) are resolved from the route's model parameter.

Contents
  1. Usage
  2. Constructor Parameters
  3. Properties
  4. Static Methods

Usage

The context is automatically created and passed to your route handlers. Access request data and build responses through its properties.

Basic context access

import { C } from "@ozanarslan/corpus";

new C.Route("/users/:id", (c) => {
	// Access URL parts
	console.log(c.url.pathname); // /users/123

	// Access headers
	const auth = c.headers.get("authorization");

	// Access cookies
	const session = c.cookies.get("sessionId");

	// Access parsed params (typed if model/generic provided)
	const userId = c.params.id;

	// Access parsed search params
	const page = c.search.page;

	// Modify response headers
	c.res.headers.set("x-custom", "value");

	return { userId };
});

Setting response data

new C.Route("/data", (c) => {
	// Set status code
	c.res.status = 201;

	// Set headers
	c.res.headers.set("content-type", "application/json");

	// Return data (will be serialized)
	return { created: true };
});

Attaching custom data

import { C } from "@ozanarslan/corpus";

// Extend ContextDataInterface via module augmentation
declare module "@ozanarslan/corpus" {
	interface ContextDataInterface {
		user?: { id: number; name: string };
		requestId?: string;
	}
}

// Middleware sets data
const authMiddleware = new C.Middleware({
	variant: "inbound",
	useOn: "*",
	handler: async (c) => {
		c.data.user = await verifyToken(c.headers.get("authorization"));
		c.data.requestId = crypto.randomUUID();
	},
});

// Handler accesses typed data
new C.Route("/profile", (c) => {
	// you can also assign here but I'm not sure what that would accomplish
	// c.data.user is typed as { id: number; name: string } | undefined
	return { user: c.data.user, requestId: c.data.requestId };
});

See Extensibility for other extendable interfaces.

Constructor Parameters

Context is typically not instantiated directly — it is created by the framework. The constructor parameters are:

ParameterTypeDescription
reqReqThe incoming request instance
resResOptional response instance (defaults to new Res)

Properties

PropertyTypeDescription
reqReqThe request instance with raw access
urlURLStandard Web API URL object
headersCHeadersRequest headers
cookiesCookiesRequest cookies
bodyBParsed and validated request body
searchSParsed and validated URL search parameters
paramsPParsed and validated URL path parameters
resResResponse builder for setting status, headers, etc.
dataContextDataInterfaceCustom data storage for middleware communication

Static Methods

appendParsedData

static async appendParsedData(ctx, req, data): Promise<void>

Parses and validates body, params, and search data using the route's model, then attaches them to the context. Called internally before the route handler executes.