-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponses.ts
More file actions
44 lines (38 loc) · 1.42 KB
/
Responses.ts
File metadata and controls
44 lines (38 loc) · 1.42 KB
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
import { renderToString } from "react-dom/server";
export function json(body?: BodyInit | null, init?: ResponseInit) {
const headers = new Headers(init?.headers);
if (!headers.has("content-type")) {
headers.set("content-type", "application/json");
}
return new Response(body, { ...init, headers });
}
export function html(body?: BodyInit | null, init?: ResponseInit) {
const headers = new Headers(init?.headers);
if (!headers.has("content-type")) {
headers.set("content-type", "text/html");
}
return new Response(body, { ...init, headers });
}
export function javascript(body?: BodyInit | null, init?: ResponseInit) {
const headers = new Headers(init?.headers);
if (!headers.has("content-type")) {
headers.set("content-type", "text/javascript");
}
return new Response(body, { ...init, headers });
}
export function page(children: React.ReactElement) {
// todo: use `renderToReadableStream` once Safari supports `ReadableByteStreamController`
return html(renderToString(children));
}
export function notFound(
body: BodyInit | null = "Not Found",
{ status = 404, statusText = "Not Found", ...rest }: ResponseInit = {},
) {
return new Response(body, { status, statusText, ...rest });
}
export function notAcceptable(
body: BodyInit | null = "Not Acceptable",
{ status = 406, statusText = "Not Acceptable", ...rest }: ResponseInit = {},
) {
return new Response(body, { status, statusText, ...rest });
}