-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
content.ts
34 lines (29 loc) · 840 Bytes
/
content.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
import { ActionResult } from "../models/response.ts";
const textEncoder = new TextEncoder();
/** Render JSON or other content such as strings, numbers, booleans */
export function Content(
result?: string | number | boolean | Object,
status: number = 200,
headers: Headers = new Headers(),
): ActionResult {
let body;
switch (typeof result) {
case "object":
case "boolean":
case "number":
headers.set("content-type", "application/json; charset=utf-8");
body = textEncoder.encode(JSON.stringify(result));
break;
default:
// Not need because Response by default add text/html
// headers.set("content-type", "text/html; charset=UTF-8");
body = textEncoder.encode(result || "");
break;
}
return {
body,
status,
headers,
__isActionResult: true,
};
}