/** * Apply a color scheme to this site. * POST /_emdash/api/color-schemes/apply { id } — fetch + persist; recolors live. */ import type { APIRoute } from "astro"; import { requirePerm } from "#api/authorize.js"; import { apiError, handleError, unwrapResult } from "#api/error.js"; import { applyColorScheme } from "#api/handlers/color-schemes.js"; export const prerender = false; export const POST: APIRoute = async ({ locals, request }) => { 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; let id = ""; try { const body = (await request.json()) as { id?: unknown }; id = typeof body?.id === "string" ? body.id : ""; } catch { return apiError("BAD_REQUEST", "Expected a JSON body { id }", 400); } if (!id) return apiError("BAD_REQUEST", "id is required", 400); try { return unwrapResult(await applyColorScheme(emdash.db, emdash.config.marketplace, id)); } catch (error) { return handleError(error, "Failed to apply color scheme", "COLOR_SCHEME_ERROR"); } };