-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.ts
217 lines (193 loc) · 5.28 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { App } from "./app.ts";
import {
ServerRequest,
STATUS_TEXT,
createError,
} from "./deps.ts";
import { Request } from "./request.ts";
import { Response } from "./response.ts";
import { Cookies } from "./cookies.ts";
import { delegates } from "./utils/delegates.ts";
export class Context {
[key: string]: any
cookies: Cookies;
public app: App;
public request: Request;
public response: Response;
public path: string = "";
public req: ServerRequest;
public res: Response;
constructor(
app: App,
req: ServerRequest,
request: Request,
response: Response,
) {
this.request = request;
this.response = response;
this.app = app;
this.req = req;
this.res = response;
this.cookies = new Cookies(this.req, this.res, {
keys: this.app.keys,
secure: this.request.secure,
});
delegates(this, "response")
.method("attachment")
.method("redirect")
.method("remove")
.method("vary")
.method("has")
.method("set")
.method("append")
.method("flushHeaders")
.access("status")
.access("message")
.access("body")
.access("length")
.access("type")
.access("lastModified")
.access("etag")
.getter("writable");
delegates(this, "request").method("acceptsLanguages")
.method("acceptsEncodings")
.method("acceptsCharsets")
.method("accepts")
.method("get")
.method("is")
.access("querystring")
.access("idempotent")
.access("socket")
.access("search")
.access("method")
.access("query")
.access("path")
.access("url")
.access("accept")
.getter("origin")
.getter("originalUrl")
.getter("href")
.getter("subdomains")
.getter("protocol")
.getter("host")
.getter("hostname")
.getter("URL")
.getter("header")
.getter("headers")
.getter("secure")
.getter("stale")
.getter("fresh")
.getter("ips")
.getter("ip");
}
/**
* inspect() implementation, which
* just returns the JSON output.
*/
inspect() {
return this.toJSON();
}
toJSON() {
return {
request: this.request.toJSON(),
response: this.response.toJSON(),
app: this.app,
originalUrl: this.originalUrl,
req: "<original Deno req>",
res: "<original Deno res>",
socket: "<original Deno Conn>",
};
}
/**
* Assert condition,creates an HTTP error while provide http status
*/
assert(value: any, status: number, message?: string, props?: object) {
if (value) {
return;
}
throw createError(status, message, props);
}
/**
* Throw an error with `msg` and optional `status`
* defaulting to 500. Note that these are user-level
* errors, and the message may be exposed to the client.
*
* this.throw(403)
* this.throw(400, 'name required')
* this.throw('something exploded')
* this.throw(new Error('invalid'))
* this.throw(400, new Error('invalid'))
* * See: https://github.com/jshttp/http-errors
*
* Note: `status` should only be passed as the first parameter.
*
* @param {String|Number|Error} err, msg or status
* @param {String|Number|Error} [err, msg or status]
* @param {Object} [props]
*/
throw(...args: any[]): never {
let status = 500, message = "Bad Request", props;
args.forEach((value) => {
switch (true) {
case typeof value == "object" && value instanceof Error:
message = value.message;
break;
case typeof value == "object" && !(value instanceof Error):
props = value;
break;
case typeof value === "string":
message = value;
break;
case typeof value === "number":
status = value;
break;
case typeof value === "number":
status = value;
break;
}
});
throw createError(status, message, props);
}
onerror(err?: any) {
if (null == err) return;
if (!(err instanceof Error)) {
err = typeof err === "object" ? JSON.stringify(err) : err;
err = new Error(`non-error thrown: ${err}`);
}
this.app.emit("error", err, this);
if (!this.writable) {
return;
}
const headerKeys: string[] = [];
this.response.headers.forEach((value, key) => {
headerKeys.push(key);
});
headerKeys.forEach((key) => {
this.remove(key);
});
// then set those specified
if (err.headers) {
Object.keys(err.headers).forEach((key) => {
this.set(key, err.headers[key]);
});
}
// force text/plain
this.type = "text";
const getStatusCode = (err: any) => {
switch (true) {
case err instanceof Deno.errors.NotFound || "ENOENT" === err.code:
return 404;
case err.status && typeof err.status === "number" && err.status <= 500:
return err.status;
case err.statusCode && typeof err.statusCode === "number" &&
err.status <= 500:
return err.statusCode;
default:
return 500;
}
};
const statusCode = this.status = getStatusCode(err);
this.body = err.expose ? err.message : STATUS_TEXT.get(statusCode);
this.req.respond(this.response.toServerResponse());
}
}