Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/cloudflare

Cloudflare helpers: API v4, static site publishing to Pages, analytics, WAF rules, logs, and docs.

FollowFork

Files

custom-domain.ts

58 lines · 3.3 KB · TypeScript
import {
	setupCustomDomain,
	type CustomDomainSetupInput,
	type CustomDomainSetupResult,
} from './custom-domain-core.ts'
import { authFrom, liveDeps } from './custom-domain-runtime.ts'

export type { CustomDomainSetupInput, CustomDomainSetupResult } from './custom-domain-core.ts'

/**
 * Point a custom hostname (for example `app.example.com`) at a Kody package app
 * in one call. Use it instead of hand-running packageAppCustomHostnameAdd, the
 * Cloudflare DNS edits, and packageAppCustomHostnameSync.
 *
 * Steps: validate the hostname (Kody's own kody.codes / kody.run are refused on
 * purpose), find the owning zone in the connected Cloudflare account (longest
 * suffix match), refuse if the hostname already has other DNS records (nothing
 * is changed), register the Kody app hostname (or reuse the row already bound
 * to this package), create a DNS-only CNAME to `domains.kody.run`, publish the
 * ownership and certificate validation records Kody reports, then sync with
 * backoff until hostname and SSL are both `active` or `waitMinutes` runs out.
 *
 * Validation types: `txt` gets a TXT record, `cname` gets a DNS-only CNAME,
 * `http` needs no record (Cloudflare checks it through the routing CNAME), and
 * `email` or any unknown type becomes a `manual` record with a clear next step
 * while everything else still finishes. A blocked name or a Cloudflare error is
 * reported the same way instead of throwing partway.
 *
 * Safe to re-run: existing records with the right values are reused, records
 * without the helper's `kody-custom-domain` comment are never changed or
 * deleted, and other hostnames are never touched. An active setup returns
 * `already_configured` with no writes.
 *
 * Must run from `execute` as a signed-in org admin (the packageAppCustomHostname*
 * capabilities need a person with org:write on a paid plan). Not for jobs,
 * webhooks, or app requests.
 *
 * @param params.hostname - Bare hostname, a subdomain of a zone in Cloudflare.
 * @param params.package - Package id (UUID), scoped name (`@scope/leaf`), or name leaf. The package must have an app.
 * @param params.dryRun - Report planned actions and per-record status with no writes (no Kody add/sync, no DNS changes).
 * @param params.waitMinutes - How long to keep syncing (0 to 15, default 1 so it fits the ~90s execute budget). Running out returns `pending`, not an error.
 * @param params.account - Cloudflare token alias (`default`, `kody`, `pages`); defaults to `default`.
 * @param params.apiTokenSecret - Explicit Kody secret name for the Cloudflare token instead of an alias.
 * @returns `{ ok, status: 'already_configured' | 'planned' | 'active' | 'pending', hostnameStatus, sslStatus, kodyStatus, records, needsManualAction, nextSteps, nextStep, zone, package, kodyHostname, actions, syncAttempts, warnings }`.
 * @example
 * import setupCustomDomain from 'kody:@kentcdodds/cloudflare/custom-domain'
 * const plan = await setupCustomDomain({
 *   hostname: 'app.example.com',
 *   package: '@scope/web-app',
 *   dryRun: true,
 * })
 * // => { ok: true, status: 'planned', records: [{ purpose: 'routing', outcome: 'planned', ... }, ...], nextSteps: [...] }
 */
export default async function setupKodyCustomDomain(
	params: CustomDomainSetupInput,
): Promise<CustomDomainSetupResult> {
	return setupCustomDomain(params, liveDeps(authFrom(params)))
}