-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.ts
99 lines (95 loc) · 2.46 KB
/
response.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
import { contentType, Status } from './deps.ts';
import { isHtml } from './util.ts';
/**
* 服务响应对象
*/
interface ServerResponse {
status?: number,
headers?: Headers;
body?:Uint8Array;
}
/**
* body 类型
*/
const BODY_TYPES = ['string', 'number', 'bigint', 'boolean', 'symbol'];
/**
* body 编码对象
*/
const encoder = new TextEncoder();
export class Response {
/**
* 响应对象 body
*
* @type {*}
* @memberof Response
*/
body?: any;
/**
* 响应头
*/
headers = new Headers();
/**
* 响应状态码
*
* @type {Status}
* @memberof Response
*/
status?: Status;
/**
* 响应对象 mime 类型
*
* @type {string}
* @memberof Response
*/
type: string;
toServerResponse(): ServerResponse {
const body = this._getBody();
this._setContentType();
// 如果没有正文且没有内容类型且没有设置长度,则设置内容长度为0
if(!(
body ||
this.headers.has('Content-Type') ||
this.headers.has('Content-Length')
)) {
this.headers.append('Content-Length', '0');
}
return {
status: this.status || (body ? Status.OK : Status.NotFound),
body,
headers: this.headers
}
}
/**
* 获取响应对象
* @private
* @memberof Response
*/
private _getBody(): Uint8Array | undefined {
const typeofBody = typeof this.body;
let result: Uint8Array | undefined;
if (BODY_TYPES.includes(typeofBody)) {
const bodyText = String(this.body);
result = encoder.encode(bodyText);
this.type = this.type || isHtml(bodyText) ? "html" : "text/plain";
} else if(this.body instanceof Uint8Array) {
result = this.body;
} else if(typeofBody === 'object' && this.body !== null) {
result = encoder.encode(JSON.stringify(this.body));
this.type = this.type || 'json';
}
return result;
}
/**
* 设置响应对象内容类型字段
* @private
* @memberof Response
*/
private _setContentType() {
if(this.type) {
const contentTypeString = contentType(this.type);
if(contentTypeString && !this.headers.has("Content-Type")) {
this.headers.append("Content-Type", contentTypeString);
}
}
}
}