{"version":3,"file":"apply.mjs","names":[],"sources":["../../../../../../src/astro/routes/api/settings/seed/apply.ts"],"sourcesContent":["/**\n * Apply a seed file to this site.\n *\n * POST /_emdash/api/settings/seed/apply   (body: a seed file)\n *\n * Update-on-conflict, content included; anything the site added on its own is\n * left alone, and site identity settings (title, tagline, url) are skipped.\n * Used by the hosting platform to give a new project its theme's seed, and\n * to re-sync a project with its theme.\n */\n\nimport type { APIRoute } from \"astro\";\n\nimport { requirePerm } from \"#api/authorize.js\";\nimport { apiError, apiSuccess, handleError } from \"#api/error.js\";\nimport { applySeed } from \"#seed/apply.js\";\nimport type { SeedFile } from \"#seed/types.js\";\nimport { validateSeed } from \"#seed/validate.js\";\n\nexport const prerender = false;\n\nconst IDENTITY_KEYS = [\"title\", \"tagline\", \"url\"] as const;\nconst MAX_BYTES = 8 * 1024 * 1024;\n\nexport const POST: APIRoute = async ({ locals, request }) => {\n\tconst { emdash, user } = locals;\n\tif (!emdash?.db) return apiError(\"NOT_CONFIGURED\", \"EmDash is not initialized\", 500);\n\n\tconst denied = requirePerm(user, \"settings:manage\");\n\tif (denied) return denied;\n\n\tconst text = await request.text();\n\tif (text.length > MAX_BYTES) return apiError(\"PAYLOAD_TOO_LARGE\", \"Seed is too large\", 413);\n\tlet seed: SeedFile;\n\ttry {\n\t\tseed = JSON.parse(text) as SeedFile;\n\t} catch {\n\t\treturn apiError(\"INVALID_JSON\", \"Body must be a seed file (JSON)\", 400);\n\t}\n\tconst validation = validateSeed(seed);\n\tif (!validation.valid) {\n\t\treturn apiError(\"INVALID_SEED\", `Invalid seed file: ${validation.errors.join(\", \")}`, 400);\n\t}\n\n\ttry {\n\t\tif (seed.settings) {\n\t\t\tconst settings = { ...seed.settings } as Record<string, unknown>;\n\t\t\tfor (const k of IDENTITY_KEYS) delete settings[k];\n\t\t\tseed.settings = settings as typeof seed.settings;\n\t\t}\n\t\tconst result = await applySeed(emdash.db, seed, {\n\t\t\tincludeContent: true,\n\t\t\tonConflict: \"update\",\n\t\t\tstorage: emdash.storage ?? undefined,\n\t\t});\n\t\treturn apiSuccess(result);\n\t} catch (error) {\n\t\treturn handleError(error, \"Failed to apply seed\", \"SEED_ERROR\");\n\t}\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBA,MAAa,YAAY;AAEzB,MAAM,gBAAgB;CAAC;CAAS;CAAW;CAAM;AACjD,MAAM,YAAY,IAAI,OAAO;AAE7B,MAAa,OAAiB,OAAO,EAAE,QAAQ,cAAc;CAC5D,MAAM,EAAE,QAAQ,SAAS;AACzB,KAAI,CAAC,QAAQ,GAAI,QAAO,SAAS,kBAAkB,6BAA6B,IAAI;CAEpF,MAAM,SAAS,YAAY,MAAM,kBAAkB;AACnD,KAAI,OAAQ,QAAO;CAEnB,MAAM,OAAO,MAAM,QAAQ,MAAM;AACjC,KAAI,KAAK,SAAS,UAAW,QAAO,SAAS,qBAAqB,qBAAqB,IAAI;CAC3F,IAAI;AACJ,KAAI;AACH,SAAO,KAAK,MAAM,KAAK;SAChB;AACP,SAAO,SAAS,gBAAgB,mCAAmC,IAAI;;CAExE,MAAM,aAAa,aAAa,KAAK;AACrC,KAAI,CAAC,WAAW,MACf,QAAO,SAAS,gBAAgB,sBAAsB,WAAW,OAAO,KAAK,KAAK,IAAI,IAAI;AAG3F,KAAI;AACH,MAAI,KAAK,UAAU;GAClB,MAAM,WAAW,EAAE,GAAG,KAAK,UAAU;AACrC,QAAK,MAAM,KAAK,cAAe,QAAO,SAAS;AAC/C,QAAK,WAAW;;AAOjB,SAAO,WALQ,MAAM,UAAU,OAAO,IAAI,MAAM;GAC/C,gBAAgB;GAChB,YAAY;GACZ,SAAS,OAAO,WAAW;GAC3B,CAAC,CACuB;UACjB,OAAO;AACf,SAAO,YAAY,OAAO,wBAAwB,aAAa"}