Skip to content
File

Blob: src/worker/common/response.ts

typescript21 lines
1/**
2 * Lightweight response helpers to keep handlers concise and consistent.
3 * Prefer these over ad-hoc new Response(...) in endpoint code.
4 */
5 
6export function json(data: unknown, status = 200, headers: HeadersInit = {}) {
7 const h = new Headers(headers);
8 if (!h.has("Content-Type")) h.set("Content-Type", "application/json");
9 return new Response(JSON.stringify(data), { status, headers: h });
10}
11 
12export function text(body: string, status = 200, headers: HeadersInit = {}) {
13 const h = new Headers(headers);
14 if (!h.has("Content-Type")) h.set("Content-Type", "text/plain; charset=utf-8");
15 return new Response(body, { status, headers: h });
16}
17 
18export function clientAbortedResponse(headers: HeadersInit = {}): Response {
19 return text("client aborted\n", 499, headers);
20}