forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.ts
46 lines (39 loc) · 1.38 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2018-2019 the oak authors. All rights reserved. MIT license.
import { Application } from "./application.ts";
import { ServerRequest } from "./deps.ts";
import { createHttpError } from "./httpError.ts";
import { Request } from "./request.ts";
import { Response } from "./response.ts";
import { ErrorStatus } from "./types.ts";
export class Context<S extends object = { [key: string]: any }> {
/** A reference to the current application */
app: Application<any>;
/** The request object */
request: Request;
/** The response object */
response = new Response();
/** 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 }>();
*
*/
state: S;
constructor(app: Application<S>, serverRequest: ServerRequest) {
this.app = app;
this.state = app.state;
this.request = new Request(serverRequest);
}
/** 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.
*/
throw(errorStatus: ErrorStatus, message?: string, props?: object): never {
const err = createHttpError(errorStatus, message);
if (props) {
Object.assign(err, props);
}
throw err;
}
}