BetterFrame/server/src/plugins/service-admin-http/html-response.ts

44 lines
1.3 KiB
TypeScript
Raw Normal View History

/**
* Return an HTML response from JSX-rendered markup.
*
* h3 v2's html() is a tagged template literal only — can't pass
* a string/object directly. This helper wraps JSX output in a
* proper Response with text/html content type.
*/
export function htmlPage(markup: unknown): Response {
return new Response(String(markup), {
headers: { "content-type": "text/html; charset=utf-8" },
});
}
/**
* Build a redirect Response with optional Set-Cookie header.
* Avoids h3's setCookie which doesn't play well with returning
* a raw Response object.
*/
export function redirectWithCookie(
location: string,
cookie?: { name: string; value: string; maxAge: number },
status = 302,
): Response {
const headers = new Headers({ location });
if (cookie) {
headers.set(
"set-cookie",
`${cookie.name}=${cookie.value}; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=${cookie.maxAge}`,
);
}
return new Response(null, { status, headers });
}
/** Build a redirect that clears a cookie. */
export function redirectClearCookie(location: string, cookieName: string): Response {
return new Response(null, {
status: 302,
headers: {
location,
"set-cookie": `${cookieName}=; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=0`,
},
});
}