-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
request.ts
70 lines (68 loc) · 1.65 KB
/
request.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
import { cookie } from '../dependencies.ts';
import ServerResponse from './response.ts';
import Server from './server.ts';
import { MatchedRoute, RequestState } from './types.ts';
interface RequestOptions {
raw: Request,
route: MatchedRoute,
server: Server
}
/**
* A request represents an incoming message that your server should respond to.
* Pogo passes a request instance as the first argument to route handlers.
*/
export default class ServerRequest {
raw: Request;
route: MatchedRoute;
response: ServerResponse;
server: Server;
state: RequestState;
url: URL;
constructor(options: RequestOptions) {
this.raw = options.raw;
this.route = options.route;
this.server = options.server;
this.response = new ServerResponse();
this.state = cookie.getCookies(this.headers);
this.url = new URL(this.raw.url);
}
get body() {
return this.raw.body;
}
get headers() {
return this.raw.headers;
}
get host() {
return this.url.host;
}
get hostname() {
return this.url.hostname;
}
get href() {
return this.url.href;
}
get method() {
return this.raw.method;
}
get origin() {
return this.url.origin;
}
get params() {
return this.route.params;
}
get path() {
return this.url.pathname;
}
get referrer() {
return this.headers.get('referer') || '';
}
get search() {
return this.url.search;
}
get searchParams() {
return this.url.searchParams;
}
toString() {
return this.method + ' ' + this.url.href;
}
}