deno.land / x / duck_web_framework@0.1.1 / 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
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
import { ServerRequest } from "https://deno.land/std@0.70.0/http/server.ts";import { CookieJar } from "./cookie_jar.ts";
export interface QueryOrParams { [name: string]: string | string[];}
export class DuckRequest { private request: ServerRequest; private _body?: any; private _query: QueryOrParams; private _params: QueryOrParams = {}; cookies?: CookieJar; error?: any;
constructor(request: ServerRequest) { this.request = request; this._query = {}; this.parseQuery(); }
/** * Returns request parameters. It is an object containing params in string format * Example: * route /api/user/:id * request /api/user/123 - req.params = { id: "123" } */ get params() { return this._params; }
/** * sets request params (/user/:id etc.) * Probably should be private and parameters should be set from withing request - TODO */ set params(value) { this._params = value; }
private parseQuery() { try { const parts = this.request.url.split("?"); if (parts[1]) { const queryString = parts[1]; const queryParams = queryString.split("&"); queryParams.forEach((param) => { const parts = param.split("="); const name = parts[0]; const value = parts[1]; if (this._query[name]) { if (Array.isArray(this._query[name])) return (this._query[name] as string[]).push(value); return this._query[name] = [(this.query[name] as string), value]; } this._query[name] = value; }); } } catch (e) { this.error = e.message; } }
/** * Returns query params. An object containing param names as keys and strings/array of strings as values * Example: * request - /api/furniture?color=red&color=blue&type=chair * req.query = { color: [ "red", "blue" ], type: "chair" } */ get query() { return this._query; }
/** * THIS METHOD IS AUTOMATICALLY CALLED ON EVERY REQUEST * Tries to parse request's body. * Currently supporting plain text and JSON. * Request must have Content-Type header present. * If it's not, or MIME is different than text/plain or application/json * request body will remain default, with is Deno.Reader */ async parseBody() { try { if (this.headers.has("Content-Type")) { const contentType = this.headers.get("Content-Type"); const bodyRaw = await Deno.readAll(this.request.body); const body = new TextDecoder("utf-8").decode(bodyRaw); if (contentType === "application/json") return this._body = JSON.parse(body.toString()); if (contentType === "text/plain") return this._body = body; } this._body = this.request.body; } catch (e) { this.error = e.message; } }
get headers() { return this.request.headers; }
get url() { return this.request.url; }
get method() { return this.request.method; }
get body() { return this._body; }
get contentLength() { return this.request.contentLength; }
get remoteAddr() { // I think it is safe to assume that it will always be NetAddr, not UnixAddr return this.request.conn.remoteAddr as Deno.NetAddr; }}
duck_web_framework

Version Info

Tagged at
3 years ago

External Dependencies

1 external dependency