-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.ts
48 lines (41 loc) · 1.15 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
/**
* The current view of this file is very simplistic. Ideally, building up a
* Response with pipeable combinators is the goal.
*/
import type { VNode } from "preact";
import { contentType } from "@std/media-types";
import { render } from "preact-render-to-string";
import * as E from "fun/either";
import { pipe } from "fun/fn";
export function html(html: string): Response {
return new Response(`<!DOCTYPE html>${html}`, {
headers: { "content-type": contentType("html") },
});
}
export function jsx(vnode: VNode): Response {
return html(render(vnode));
}
export function error(message: string, status = 500): Response {
return new Response(message, { status });
}
const stringify = E.tryCatch(JSON.stringify, (err) => {
const error = new Error("Unable to stringify value as JSON");
error.cause = err;
return error;
});
export function json<O>(value: O): Response {
return pipe(
value,
stringify,
E.match(
(err) => {
console.error(err);
return error(err.toString());
},
(value) =>
new Response(value, {
headers: { "content-type": contentType("json") },
}),
),
);
}