-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.ts
116 lines (92 loc) · 3.49 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
116
import { STATUS_TEXT, } from 'https://deno.land/std/http/http_status.ts';
// import { extname } from 'https://deno.land/std/path/mod.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;
#message = '';
#body: BodyInit | Record<string, string> = '';
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 {
if (message) {
this.#message = message;
return this;
} else {
return this.#message;
}
}
body (body: BodyInit | Record<string, string>): Context | BodyInit | Record<string, string> {
if (body) {
this.#body = body;
return this;
} else {
return this.#body;
}
}
response (code?: number, message?: string, body?: BodyInit | any): Response {
code = code ?? this.#code;
message = message ?? this.#message ?? STATUS_TEXT.get(code);
body = body ?? this.#body ?? 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(body);
if (type === 'object' || type === 'array'
// typeof body === 'object' &&
// !(body === null || typeof body === 'string' || body instanceof Blob || body instanceof ArrayBuffer
// || body instanceof FormData || body instanceof ReadableStream || body instanceof URLSearchParams)
) {
body = JSON.stringify(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(body as BodyInit, { status: code, statusText: message, headers: this.headers });
}
// set (name: string, data: object) {
// if (name in this) throw new Error('Context - can not overwrite');
// else this[ name ] = data;
// }
}