-
Notifications
You must be signed in to change notification settings - Fork 0
/
respond.ts
77 lines (75 loc) · 2.97 KB
/
respond.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
import { HttpRequest, HttpResponse, TBody } from './types.ts';
const JSON_TYPE_CHARSET = "application/json; charset=utf-8";
export default function respond(req: HttpRequest, res: HttpResponse) {
res.return = [];
res.locals = {};
res.opts = {};
res.header = function (key?: { [k: string]: any } | string, value?: any) {
if (typeof key === 'object' && value === void 0) {
if (key instanceof Headers) {
this.opts.headers = key;
} else {
if (this.opts.headers) {
for (const k in key) {
this.opts.headers.set(k, key[k]);
}
} else {
this.opts.headers = new Headers(key);
}
}
return this;
}
if (typeof key === 'string' && value === void 0) {
return (this.opts.headers ? this.opts.headers.get(key) : null) as HttpResponse & string;
}
if (typeof key === 'string' && value !== void 0) {
this.opts.headers = this.opts.headers || new Headers();
this.opts.headers.set(key as string, value);
return this;
}
return (this.opts.headers || new Headers()) as HttpResponse & Headers;
};
res.type = (value: string) => res.header("Content-Type", value) as HttpResponse;
res.status = function (code?: number) {
if (code) {
this.opts.status = code;
return this;
}
return (this.opts.status || 200) as HttpResponse & number;
};
req.pond = function (body?: TBody | { [k: string]: any } | null, opts = res.opts) {
if (res.return.length) {
let len = res.return.length, i = 0, ret;
while (i < len) {
if (res.return[i](body)) {
ret = res.return[i](body);
break;
}
i++;
}
if (ret) return this.respond({ body: ret, ...opts });
}
if (typeof body === 'object') {
if (body instanceof Uint8Array || typeof (body as Deno.Reader).read === 'function') {
return this.respond({ body: body as TBody, ...opts });
}
body = JSON.stringify(body);
opts.headers = opts.headers || new Headers();
opts.headers.set("Content-Type", JSON_TYPE_CHARSET);
}
return this.respond({ body, ...opts });
};
req.getBaseUrl = function () {
let host = req.headers.get("host") || req.headers.get("Host");
let proto = this.isHttps ? 'https://' : 'http://';
if (host) return proto + host;
let obj = this.conn?.localAddr as any;
if (obj) {
let ip = obj.hostname || '127.0.0.1';
let port = (obj.port && obj.port === 80) ? '' : ':'+obj.port;
return proto + ip + port;
}
return '';
};
res.body = (body?: TBody | { [k: string]: any } | null) => req.pond(body, res.opts);
}