/** * Custom-domain API (instance side). * * POST /_emdash/api/settings/custom-domain { domain?, action: "check" | "reset" } * * Delegates to the hosting control plane (which holds the Cloudflare * credentials) to create/check/reset a Cloudflare-for-SaaS custom hostname for * this instance, and returns the DNS records the owner must add plus the current * status. Only available on platform-provisioned instances (those seeded with a * `custom_domain:api_url` + `credits:project_id`). */ import type { APIRoute } from "astro"; import { z } from "zod"; import { requirePerm } from "#api/authorize.js"; import { apiError, apiSuccess, handleError } from "#api/error.js"; import { isParseError, parseBody } from "#api/parse.js"; import { NotManagedError, platformCustomDomain } from "../../../../dns/platform.js"; export const prerender = false; const BodySchema = z.object({ domain: z.string().trim().max(253).optional(), action: z.enum(["check", "reset"]).default("check"), }); 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; const body = await parseBody(request, BodySchema); if (isParseError(body)) return body; try { const result = await platformCustomDomain(emdash.db, body); if (!result.success) { return apiError( "CUSTOM_DOMAIN_ERROR", result.error ?? "The hosting platform rejected the request.", 502, ); } return apiSuccess(result); } catch (error) { if (error instanceof NotManagedError) return apiError("NOT_MANAGED", error.message, 400); return handleError(error, "Custom domain request failed", "CUSTOM_DOMAIN_ERROR"); } };