Skip to main content
Module

x/aleph/server/response.ts

The Full-stack Framework in Deno.
Go to Latest
File
export function json(data: unknown, init?: ResponseInit): Response { const headers = new Headers(init?.headers); headers.append("content-type", "application/json; charset=utf-8"); return new Response(JSON.stringify(data), { ...init, headers });}
export type CacheControlOptions = { maxAge?: number; sMaxAge?: number; public?: boolean; private?: boolean; immutable?: boolean; mustRevalidate?: boolean;};
export function content( body: BodyInit, init?: ResponseInit & { contentType?: string; cacheControl?: CacheControlOptions | "immutable" | "no-cache"; },): Response { const headers = new Headers(init?.headers);
const contentType = init?.contentType; if (contentType) { headers.set("Content-Type", contentType); }
const cacheControl = init?.cacheControl; if (cacheControl) { if (cacheControl === "no-cache") { headers.set("Cache-Control", "no-cache, no-store, must-revalidate"); } else if (cacheControl === "immutable") { headers.set("Cache-Control", "public, max-age=31536000, immutable"); } else { const { maxAge, sMaxAge, immutable, mustRevalidate } = cacheControl; headers.set( "Cache-Control", [ cacheControl.public && "public", cacheControl.private && "private", maxAge && `max-age=${maxAge}`, sMaxAge && `s-maxage=${sMaxAge}`, immutable && "immutable", mustRevalidate && "must-revalidate", ].filter(Boolean).join(", "), ); } }
return new Response(body, { ...init, headers });}