import { error } from "@sveltejs/kit"; import { callXrpc } from "$hatk/client"; import type { RequestHandler } from "./$types"; import { KINDS, STATIC_PATHS, escapeXml, xml } from "$lib/sitemap"; export const GET: RequestHandler = async ({ params, url }) => { const page = Number(params.page); if (!Number.isInteger(page) || page < 0) error(404, "Not found"); let entries: { path: string; lastmod?: string }[]; if (params.kind === "pages") { if (page !== 0) error(404, "Not found"); entries = STATIC_PATHS.map((path) => ({ path })); } else if ((KINDS as readonly string[]).includes(params.kind)) { const res = await callXrpc("social.grain.unspecced.getSitemap", { kind: params.kind as (typeof KINDS)[number], page, }); if (page >= res.pageCount) error(404, "Not found"); entries = res.entries; } else { error(404, "Not found"); } return xml( ` ${entries .map( (e) => ` ${escapeXml(url.origin + encodeURI(e.path))}${e.lastmod ? `${e.lastmod}` : ""}`, ) .join("\n")} `, ); };