profilePage/src/routes/[id].ts
2025-05-03 07:07:26 -04:00

41 lines
1,012 B
TypeScript

import { resolve } from "node:path";
import { badgeApi, lanyardConfig, plausibleScript } from "@config/environment";
import { file } from "bun";
const routeDef: RouteDef = {
method: "GET",
accepts: "*/*",
returns: "text/html",
};
async function handler(request: ExtendedRequest): Promise<Response> {
const { id } = request.params;
const instance = lanyardConfig.instance
.replace(/^https?:\/\//, "")
.replace(/\/$/, "");
const path = resolve("src", "views", "index.html");
const bunFile = file(path);
const html = new HTMLRewriter()
.on("head", {
element(head) {
head.setAttribute("data-user-id", id || lanyardConfig.userId);
head.setAttribute("data-instance-uri", instance);
head.setAttribute("data-badge-url", badgeApi || "");
if (plausibleScript) {
head.append(plausibleScript, { html: true });
}
},
})
.transform(await bunFile.text());
return new Response(html, {
headers: {
"Content-Type": "text/html",
},
});
}
export { handler, routeDef };