{"version":3,"file":"platform-Dyt4HYQg.mjs","names":[],"sources":["../src/dns/platform.ts"],"sourcesContent":["/**\n * Talk to the hosting control plane about this instance's custom domain.\n * Only platform-provisioned instances (seeded `custom_domain:api_url` +\n * `credits:project_id`) can; the parent holds the Cloudflare credentials.\n */\n\nimport type { Kysely } from \"kysely\";\n\nimport { OptionsRepository } from \"#db/repositories/options.js\";\nimport type { Database } from \"#db/types.js\";\n\nexport interface PlatformDomainRecord {\n\ttype: string;\n\tname: string;\n\tvalue: string;\n\tnote?: string;\n}\n\nexport interface PlatformDomainResult {\n\tsuccess: boolean;\n\terror?: string;\n\tdomain?: string;\n\tstatus?: string;\n\tsslStatus?: string | null;\n\tactive?: boolean;\n\trecords?: PlatformDomainRecord[];\n\treset?: boolean;\n\tdefaultUrl?: string;\n}\n\nexport class NotManagedError extends Error {}\n\nexport async function platformCustomDomain(\n\tdb: Kysely<Database>,\n\tbody: { domain?: string; action: \"check\" | \"reset\" },\n): Promise<PlatformDomainResult> {\n\tconst options = new OptionsRepository(db);\n\tconst map = await options.getMany<string>([\"custom_domain:api_url\", \"credits:project_id\"]);\n\tconst apiUrl = map.get(\"custom_domain:api_url\") ?? \"\";\n\tconst project = map.get(\"credits:project_id\") ?? \"\";\n\tif (!apiUrl || !project) {\n\t\tthrow new NotManagedError(\n\t\t\t\"Custom domains are managed by your hosting platform, which isn't configured for this instance.\",\n\t\t);\n\t}\n\tconst res = await fetch(apiUrl, {\n\t\tmethod: \"POST\",\n\t\theaders: { \"Content-Type\": \"application/json\", \"X-EmDash-Request\": \"1\" },\n\t\tbody: JSON.stringify({ project, domain: body.domain ?? \"\", action: body.action }),\n\t});\n\tlet parsed: unknown = null;\n\ttry {\n\t\tparsed = JSON.parse(await res.text());\n\t} catch {\n\t\tparsed = null;\n\t}\n\t// The plugin route wraps its result as { success, data: { success, ... } }.\n\tconst inner =\n\t\tparsed && typeof parsed === \"object\" && \"data\" in parsed\n\t\t\t? (parsed as { data?: unknown }).data\n\t\t\t: parsed;\n\tconst result = (inner && typeof inner === \"object\" ? inner : {}) as PlatformDomainResult;\n\tif (!res.ok || result.success === false) {\n\t\treturn { success: false, error: result.error ?? \"The hosting platform rejected the request.\" };\n\t}\n\treturn { ...result, success: true };\n}\n"],"mappings":";;;AA8BA,IAAa,kBAAb,cAAqC,MAAM;AAE3C,eAAsB,qBACrB,IACA,MACgC;CAEhC,MAAM,MAAM,MADI,IAAI,kBAAkB,GAAG,CACf,QAAgB,CAAC,yBAAyB,qBAAqB,CAAC;CAC1F,MAAM,SAAS,IAAI,IAAI,wBAAwB,IAAI;CACnD,MAAM,UAAU,IAAI,IAAI,qBAAqB,IAAI;AACjD,KAAI,CAAC,UAAU,CAAC,QACf,OAAM,IAAI,gBACT,iGACA;CAEF,MAAM,MAAM,MAAM,MAAM,QAAQ;EAC/B,QAAQ;EACR,SAAS;GAAE,gBAAgB;GAAoB,oBAAoB;GAAK;EACxE,MAAM,KAAK,UAAU;GAAE;GAAS,QAAQ,KAAK,UAAU;GAAI,QAAQ,KAAK;GAAQ,CAAC;EACjF,CAAC;CACF,IAAI,SAAkB;AACtB,KAAI;AACH,WAAS,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC;SAC9B;AACP,WAAS;;CAGV,MAAM,QACL,UAAU,OAAO,WAAW,YAAY,UAAU,SAC9C,OAA8B,OAC/B;CACJ,MAAM,SAAU,SAAS,OAAO,UAAU,WAAW,QAAQ,EAAE;AAC/D,KAAI,CAAC,IAAI,MAAM,OAAO,YAAY,MACjC,QAAO;EAAE,SAAS;EAAO,OAAO,OAAO,SAAS;EAA8C;AAE/F,QAAO;EAAE,GAAG;EAAQ,SAAS;EAAM"}