-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
response.ts
60 lines (47 loc) · 1.21 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
import { Content } from "../renderer/content.ts";
type ResponseBody = Uint8Array | Deno.Reader | string | any;
export interface ActionResult {
headers: Headers;
body?: ResponseBody;
status?: number;
__isActionResult: boolean;
}
export class Response {
public readonly headers: Headers = new Headers();
public status?: number;
public body?: ResponseBody;
public result?: ActionResult | any;
public error?: Error;
private immediately: boolean = false;
public setImmediately(): void {
this.immediately = true;
}
public isImmediately(): boolean {
return this.immediately;
}
public getRaw() {
return {
headers: this.headers,
body: this.body,
status: this.status,
};
}
public getMergedResult() {
if (this.body !== undefined) {
return this.getRaw();
}
const result = this.result;
let response: any;
if (result !== undefined && (result as ActionResult).__isActionResult) {
response = result;
} else {
response = Content(result);
}
// merge headers
for (const pair of this.headers.entries()) {
response.headers.set(pair[0], pair[1]);
}
delete response.__isActionResult;
return response;
}
}