import { Application, State } from "./application.ts";import { Cookies } from "./cookies.ts";import { ServerRequest } from "./deps.ts";import { createHttpError } from "./httpError.ts";import { KeyStack } from "./keyStack.ts";import { Request } from "./request.ts";import { Response } from "./response.ts";import { ErrorStatus } from "./types.ts";
export class Context<S extends State = Record<string, any>> { app: Application<any>;
cookies: Cookies;
request: Request;
response: Response = new Response();
state: S;
constructor(app: Application<S>, serverRequest: ServerRequest) { this.app = app; this.state = app.state; this.request = new Request(serverRequest); this.cookies = new Cookies(this.request, this.response, { keys: this.app.keys as KeyStack | undefined, secure: this.request.secure, }); }
assert( condition: any, errorStatus: ErrorStatus = 500, message?: string, props?: object, ): asserts condition { if (condition) { return; } const err = createHttpError(errorStatus, message); if (props) { Object.assign(err, props); } throw err; }
throw(errorStatus: ErrorStatus, message?: string, props?: object): never { const err = createHttpError(errorStatus, message); if (props) { Object.assign(err, props); } throw err; }}