2026-05-10 00:50:16 +00:00
|
|
|
/**
|
|
|
|
|
* 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" },
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-05-10 00:53:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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",
|
2026-05-10 00:57:31 +00:00
|
|
|
`${cookie.name}=${cookie.value}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${cookie.maxAge}`,
|
2026-05-10 00:53:06 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
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,
|
2026-05-10 00:57:31 +00:00
|
|
|
"set-cookie": `${cookieName}=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0`,
|
2026-05-10 00:53:06 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|