-
Notifications
You must be signed in to change notification settings - Fork 0
/
_request.ts
42 lines (37 loc) · 939 Bytes
/
_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
import { Route } from "./_route.ts";
import { http } from "./deps.ts";
export interface ServaRequest {
readonly httpRequest: http.ServerRequest;
// http request
readonly url: URL;
readonly method: string;
readonly params: ReadonlyMap<string, string>;
readonly headers: Headers;
// response
readonly response: http.Response;
}
/**
* Creates a Serva request object.
*
* @param {ServerRequest} req
* @param {Route} route
* @returns {ServaRequest}
*/
export default function create(
req: http.ServerRequest,
route: Route,
): ServaRequest {
const response: http.Response = {};
const proto = req.proto.split("/")[0].toLowerCase();
const url = new URL(req.url, `${proto}://${req.headers.get("host")}`);
return {
url,
httpRequest: req,
method: req.method,
params: route.params(url.pathname),
headers: req.headers,
get response(): http.Response {
return response;
},
};
}