UNPKG

edge-master

Version:
93 lines (92 loc) 2.12 kB
/** * Response helper utilities to reduce boilerplate */ /** * Creates a JSON response */ export function json(data, init) { return new Response(JSON.stringify(data), { ...init, headers: { 'Content-Type': 'application/json', ...(init?.headers || {}), }, }); } /** * Creates a text response */ export function text(content, init) { return new Response(content, { ...init, headers: { 'Content-Type': 'text/plain; charset=utf-8', ...(init?.headers || {}), }, }); } /** * Creates an HTML response */ export function html(content, init) { return new Response(content, { ...init, headers: { 'Content-Type': 'text/html; charset=utf-8', ...(init?.headers || {}), }, }); } /** * Creates a redirect response */ export function redirect(url, status = 302) { return new Response(null, { status, headers: { 'Location': url, }, }); } /** * Creates a 404 Not Found response */ export function notFound(message = 'Not Found') { return new Response(message, { status: 404 }); } /** * Creates a 400 Bad Request response */ export function badRequest(message = 'Bad Request') { return json({ error: message }, { status: 400 }); } /** * Creates a 401 Unauthorized response */ export function unauthorized(message = 'Unauthorized') { return json({ error: message }, { status: 401 }); } /** * Creates a 403 Forbidden response */ export function forbidden(message = 'Forbidden') { return json({ error: message }, { status: 403 }); } /** * Creates a 500 Internal Server Error response */ export function serverError(message = 'Internal Server Error') { return json({ error: message }, { status: 500 }); } /** * Creates a 204 No Content response */ export function noContent() { return new Response(null, { status: 204 }); } /** * Creates a response with custom status and JSON body */ export function jsonWithStatus(data, status, init) { return json(data, { ...init, status }); }