/** * Export this site as a seed file. * * GET /_emdash/api/settings/seed/export[?format=tree] * * Schema, taxonomies, menus, widget areas, sections and content (all * collections) — the same format the setup wizard and `reseed` apply. Site * identity (title, tagline, url) is omitted: it belongs to whichever site * applies the seed. Used by the hosting platform to publish a project as a * theme (the seed is committed into the project's repo for copies to apply). */ import type { APIRoute } from "astro"; import { requirePerm } from "#api/authorize.js"; import { apiError, apiSuccess, handleError } from "#api/error.js"; import { exportSeed } from "#seed/export.js"; import { splitSeed, withSchemas } from "#seed/fs.js"; export const prerender = false; const IDENTITY_KEYS = ["title", "tagline", "url"] as const; export const GET: APIRoute = async ({ locals, url }) => { const { emdash, user } = locals; if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500); const denied = requirePerm(user, "settings:manage"); if (denied) return denied; try { const seed = await exportSeed(emdash.db, "all"); if (seed.settings) { const settings = { ...seed.settings } as Record; for (const k of IDENTITY_KEYS) delete settings[k]; seed.settings = settings as typeof seed.settings; } if (url.searchParams.get("format") === "tree") { return apiSuccess({ files: Object.fromEntries(withSchemas(splitSeed(seed))) }); } return apiSuccess(seed); } catch (error) { return handleError(error, "Failed to export seed", "SEED_EXPORT_ERROR"); } };