import type { APIRoute } from "astro"; import { readdir, readFile } from "node:fs/promises"; import { join } from "node:path"; const DATA_ROOT = join(process.env.REPO_ROOT ?? "/Users/ace/palacering", "palaces", "manglasabang", "palaceappsdata", "palacetravel"); export const GET: APIRoute = async () => { try { const entries = await readdir(DATA_ROOT, { withFileTypes: true }); const trips = []; for (const entry of entries) { if (!entry.isDirectory()) continue; try { const meta = await readFile(join(DATA_ROOT, entry.name, "meta.json"), "utf-8"); trips.push({ id: entry.name, ...JSON.parse(meta) }); } catch { /* skip dirs without meta.json */ } } // Sort by first date descending (newest first) trips.sort((a, b) => (b.dates?.[0] ?? "").localeCompare(a.dates?.[0] ?? "")); return new Response(JSON.stringify(trips), { headers: { "Content-Type": "application/json" }, }); } catch { return new Response("[]", { headers: { "Content-Type": "application/json" } }); } };