{"version":3,"file":"custom-domain.mjs","names":[],"sources":["../../../../../src/astro/routes/api/settings/custom-domain.ts"],"sourcesContent":["/**\n * Custom-domain API (instance side).\n *\n * POST /_emdash/api/settings/custom-domain  { domain?, action: \"check\" | \"reset\" }\n *\n * Delegates to the hosting control plane (which holds the Cloudflare\n * credentials) to create/check/reset a Cloudflare-for-SaaS custom hostname for\n * this instance, and returns the DNS records the owner must add plus the current\n * status. Only available on platform-provisioned instances (those seeded with a\n * `custom_domain:api_url` + `credits:project_id`).\n */\n\nimport type { APIRoute } from \"astro\";\nimport { z } from \"zod\";\n\nimport { requirePerm } from \"#api/authorize.js\";\nimport { apiError, apiSuccess, handleError } from \"#api/error.js\";\nimport { isParseError, parseBody } from \"#api/parse.js\";\nimport { NotManagedError, platformCustomDomain } from \"../../../../dns/platform.js\";\n\nexport const prerender = false;\n\nconst BodySchema = z.object({\n\tdomain: z.string().trim().max(253).optional(),\n\taction: z.enum([\"check\", \"reset\"]).default(\"check\"),\n});\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 body = await parseBody(request, BodySchema);\n\tif (isParseError(body)) return body;\n\n\ttry {\n\t\tconst result = await platformCustomDomain(emdash.db, body);\n\t\tif (!result.success) {\n\t\t\treturn apiError(\n\t\t\t\t\"CUSTOM_DOMAIN_ERROR\",\n\t\t\t\tresult.error ?? \"The hosting platform rejected the request.\",\n\t\t\t\t502,\n\t\t\t);\n\t\t}\n\t\treturn apiSuccess(result);\n\t} catch (error) {\n\t\tif (error instanceof NotManagedError) return apiError(\"NOT_MANAGED\", error.message, 400);\n\t\treturn handleError(error, \"Custom domain request failed\", \"CUSTOM_DOMAIN_ERROR\");\n\t}\n};\n"],"mappings":";;;;;;;;;;AAoBA,MAAa,YAAY;AAEzB,MAAM,aAAa,EAAE,OAAO;CAC3B,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,UAAU;CAC7C,QAAQ,EAAE,KAAK,CAAC,SAAS,QAAQ,CAAC,CAAC,QAAQ,QAAQ;CACnD,CAAC;AAEF,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,UAAU,SAAS,WAAW;AACjD,KAAI,aAAa,KAAK,CAAE,QAAO;AAE/B,KAAI;EACH,MAAM,SAAS,MAAM,qBAAqB,OAAO,IAAI,KAAK;AAC1D,MAAI,CAAC,OAAO,QACX,QAAO,SACN,uBACA,OAAO,SAAS,8CAChB,IACA;AAEF,SAAO,WAAW,OAAO;UACjB,OAAO;AACf,MAAI,iBAAiB,gBAAiB,QAAO,SAAS,eAAe,MAAM,SAAS,IAAI;AACxF,SAAO,YAAY,OAAO,gCAAgC,sBAAsB"}