1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { accepts } from "https://esm.town/v/vladimyr/accepts";
// @see: https://github.com/jshttp/accepts#simple-type-negotiation
export default async function(req: Request): Promise<Response> {
const accept = accepts(req);
const headers = new Headers();
// the order of this list is significant; should be server preferred order
switch (accept.types("json", "html")) {
case "json":
return Response.json({ hello: "world" });
case "html":
headers.set("content-type", "text/html");
return new Response("<b>hello, world!</b>", { headers });
default:
// the fallback is text/plain, so no need to specify it above
headers.set("content-type", "text/plain");
return new Response("hello, world!", { headers });
}
}