-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.ts
32 lines (30 loc) · 1.01 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/** Any object can be assigned to the property `state` of the `Context` object. */
type State = Record<string | number | symbol, unknown>;
// deno-lint-ignore no-explicit-any
type DefaultState = Record<string, any>;
/**
* An instance of the extendable `Context` is passed as only argument to your
* `Middleware`s. You can optionally extend the default `Context` object or pass
* a `State` type.
* ```ts
* export class Ctx extends Context<{ start: number }> {
* pathname = this.url.pathname;
* }
* ```
*/
export class Context<S extends State = DefaultState> {
connInfo: Deno.ServeHandlerInfo;
error: Error | null = null;
result: URLPatternResult = {} as URLPatternResult;
request: Request;
response: Response = new Response("Not Found", { status: 404 });
state: S;
url: URL;
startTime = NaN;
constructor(request: Request, connInfo: Deno.ServeHandlerInfo, state?: S) {
this.connInfo = connInfo;
this.request = request;
this.state = state || {} as S;
this.url = new URL(request.url);
}
}