railyard/apps/console/lib/ship/cf-domains.d.mts
caelum0x 90f6ba3b78 feat(ship): custom domains for shipped apps on the customer's own Cloudflare account (QY1.5, I6)
- packages/cli/src/domains.mjs: a dependency-free client of Cloudflare's
  public Workers Custom Domains API (zones lookup, PUT/GET/DELETE
  /accounts/{id}/workers/domains, certificate pack state). Cloudflare
  issues the TLS certificate; a domain reads active once it is. The token
  goes in the Authorization header only and is scrubbed from every error.
  Byte copy at apps/console/lib/ship/cf-domains.mjs (drift test).
- railyard domains [add|remove HOSTNAME] [--app]: the console attaches the
  hostname to the app's production Worker with the org's Cloudflare grant
  (the QY1.1 OAuth step; the access token is opened and used inside the
  console only). Without a grant (409 no_cloudflare_grant) the CLI does the
  same with the local login (CLOUDFLARE_API_TOKEN or the wrangler login
  file after `wrangler whoami`) and the console records the outcome. Adding
  an attached hostname keeps it; one on another Worker is refused; remove
  is idempotent; a hostname outside the account's zones is refused plainly.
- Console: migration 0010 railyard.app_domains (one org per hostname,
  lower case, cascade, RLS, no anon); GET/POST/DELETE /api/v1/domains and
  POST /api/v1/domains/record (CLI bearer); states refreshed from
  Cloudflare when the org has a grant. Cloudflare OAuth default scopes add
  zone:read and ssl_certs:write (orgs linked before need to run
  railyard link once more for console-side domains).
- Scribase bridge: new signed route domains {scribase_org_id, app?} ->
  {available, domains:[{app, hostname, state, checked_at, created_at}],
  next} for the Deploy tab; contract fixture v2 adds it (additive).

Tests: CLI 23/23 (new domains.test.mjs 5: grant path with the real console
handlers and a fake Cloudflare, local-login path, wrangler config file,
refusals, idempotent remove, no token printed or sent to the console);
console 147/147 (new lib/ship/domains.test.ts 4: drift, worker naming
equal to the CLI's, attach/detach and cross-org refusal, record and
refresh, bridge listing; 0010 migration on a scratch PG); contract 6/6;
console tsc clean.

Owner-only: one real-domain proof (railyard domains add on a domain in the
owner's Cloudflare account) is not run here; record it at the deploy gate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 13:45:57 +03:00

30 lines
1.5 KiB
TypeScript

// Types of cf-domains.mjs (a byte copy of packages/cli/src/domains.mjs, QY1.5).
export declare const CF_API: string;
export declare class DomainError extends Error {
status: number;
constructor(message: string, status: number);
}
export interface CfDomain {
id: string;
hostname: string;
service: string;
zone_id: string;
zone_name: string;
}
export type DomainState = "active" | "pending" | "error";
export interface CloudflareDomains {
zoneFor(hostname: string): Promise<{ id: string; name: string }>;
list(service?: string): Promise<CfDomain[]>;
attach(hostname: string, service: string, zoneId: string): Promise<CfDomain>;
detach(id: string): Promise<void>;
certStatus(zoneId: string, hostname: string): Promise<string | null>;
}
export declare function cleanHostname(raw: unknown): string | null;
export declare function zoneCandidates(hostname: string): string[];
export declare function domainState(certStatus: string | null | undefined): DomainState;
export declare function cloudflareDomains(opts: { token: string; accountId: string; fetchImpl?: typeof fetch; apiBase?: string }): CloudflareDomains;
export declare function addDomain(cf: CloudflareDomains, hostname: string, service: string): Promise<CfDomain & { state: DomainState; created: boolean }>;
export declare function removeDomain(cf: CloudflareDomains, hostname: string): Promise<{ hostname: string; removed: boolean }>;
export declare function listDomains(cf: CloudflareDomains, service?: string): Promise<(CfDomain & { state: DomainState })[]>;