-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.ts
115 lines (90 loc) · 3.35 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
import { STATUS_TEXT, } from './deps.ts';
import Mime from './mime.ts';
import Type from './type.ts';
export default class Context {
tool: Record<string, any> = {};
url: URL;
method: string;
headers: Headers;
request: Request;
redirect = Response.redirect;
#code = 200;
#ended = false;
#message?: string;
#body?: BodyInit | Record<string, unknown> | Array<unknown>;
constructor (request: Request) {
this.request = request;
this.headers = new Headers();
this.url = new URL(request.url);
this.method = request.method.toLowerCase();
}
get (name: string) {
return this.tool[ name ];
}
set (name: string, value: any) {
if (name in this.tool) return;
const enumerable = true;
const property = { enumerable, value };
Object.defineProperty(this.tool, name, property);
}
code (code?: number): Context | number {
if (code) {
this.#code = code;
return this;
} else {
return this.#code;
}
}
message (message?: string): Context | string | undefined {
if (message) {
this.#message = message;
return this;
} else {
return this.#message;
}
}
body (body: BodyInit | Record<string, unknown> | Array<unknown>): Context | BodyInit | Record<string, unknown> | Array<unknown> | undefined {
if (body) {
this.#body = body;
return this;
} else {
return this.#body;
}
}
end (code?: number, body?: BodyInit | Record<string, unknown> | Array<unknown>): Response {
// if (this.#ended) return;
this.#ended = true;
this.#code = code ?? this.#code;
this.#message = this.#message ?? STATUS_TEXT.get(this.#code) ?? '';
this.#body = body ?? this.#body ?? this.#message ?? '';
// if (!this.headers.get('content-type')) {
// const path = this.url.pathname;
// const extension = extname(path).slice(1) as keyof typeof Mime;
// if (extension) {
// const mime = Mime[ extension ] ?? Mime[ 'default' ];
// this.headers.set('content-type', `${mime};charset=utf8`);
// }
// }
// if (
// typeof body === 'string' || body instanceof Blob || body instanceof ArrayBuffer
// || body instanceof FormData || body instanceof ReadableStream || body instanceof URLSearchParams
// ) {
// if (!this.headers.has('content-type')) {
// this.headers.set('content-type', `${Mime[ 'default' ]};charset=utf8`);
// }
// } else
const type = Type(this.#body);
if (type === 'object' || type === 'array') {
this.#body = JSON.stringify(this.#body);
if (!this.headers.has('content-type')) {
this.headers.set('content-type', `${Mime[ 'json' ]};charset=utf8`);
}
}
// this.headers.set('content-length', Buffer.byteLength(body))
return new Response(this.#body as BodyInit, { status: this.#code, statusText: this.#message, headers: this.headers });
}
// set (name: string, data: object) {
// if (name in this) throw new Error('Context - can not overwrite');
// else this[ name ] = data;
// }
}