The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Credentials } from "@distilled.cloud/cloudflare/Credentials"; |
| 2 | import * as zones from "@distilled.cloud/cloudflare/zones"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import type * as HttpClient from "effect/http/HttpClient"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | |
| 8 | export class ZoneLookupInvalidProps extends Data.TaggedError("ZoneLookupInvalidProps")<{ |
| 9 | readonly message: string; |
| 10 | readonly cause?: unknown; |
| 11 | }> {} |
| 12 | |
| 13 | /** |
| 14 | * Reference to an existing Cloudflare Zone. Accepts: |
| 15 | * - a zone id (32 hex characters), |
| 16 | * - a zone name (`example.com`), or |
| 17 | * - a `{ zoneId, name? }` object (e.g. the output of a `Zone` resource or |
| 18 | * {@link importZone}). |
| 19 | */ |
| 20 | export type Reference = string | { zoneId: string; name?: string }; |
| 21 | |
| 22 | export const isId = (zone: string): boolean => /^[a-f0-9]{32}$/i.test(zone); |
| 23 | |
| 24 | export const matchesZoneHostname = (zoneName: string, hostname: string): boolean => |
| 25 | hostname === zoneName || hostname.endsWith(`.${zoneName}`); |
| 26 | |
| 27 | export const resolveZoneId = ({ |
| 28 | accountId, |
| 29 | zone, |
| 30 | hostname, |
| 31 | }: { |
| 32 | accountId: string; |
| 33 | zone: Reference | undefined; |
| 34 | hostname: string; |
| 35 | }) => |
| 36 | Effect.gen(function* () { |
| 37 | if (typeof zone === "object") return zone.zoneId; |
| 38 | if (typeof zone === "string" && isId(zone)) return zone; |
| 39 | |
| 40 | const lookup = zone ?? hostname; |
| 41 | for (const candidate of zoneNameCandidates(lookup)) { |
| 42 | const match = yield* findZoneByName({ accountId, name: candidate }); |
| 43 | if (match) return match.id; |
| 44 | } |
| 45 | return yield* new ZoneLookupInvalidProps({ |
| 46 | message: `Cloudflare zone not found for ${lookup}`, |
| 47 | }); |
| 48 | }); |
| 49 | |
| 50 | type ZoneListItem = { |
| 51 | id: string; |
| 52 | name: string; |
| 53 | account: { id?: string | null }; |
| 54 | }; |
| 55 | |
| 56 | export const findZoneByName = ({ |
| 57 | accountId, |
| 58 | name, |
| 59 | }: { |
| 60 | accountId: string; |
| 61 | name: string; |
| 62 | }): Effect.Effect< |
| 63 | ZoneListItem | undefined, |
| 64 | zones.ListZonesError, |
| 65 | Credentials | HttpClient.HttpClient |
| 66 | > => |
| 67 | Effect.gen(function* () { |
| 68 | // Distilled `listZones` rides `Retry.makeDefault` (5xx / throttling). |
| 69 | // The previous raw `fetch` failed the first time Cloudflare answered |
| 70 | // `{ success: false, errors: [{ message: "unhandled server error" }] }`. |
| 71 | const page = yield* zones.listZones({ |
| 72 | account: { id: accountId }, |
| 73 | name, |
| 74 | perPage: 1, |
| 75 | }); |
| 76 | const match = (page.result ?? []).find( |
| 77 | (candidate) => candidate.name === name && candidate.account.id === accountId, |
| 78 | ); |
| 79 | if (match === undefined) return undefined; |
| 80 | return { |
| 81 | id: match.id, |
| 82 | name: match.name, |
| 83 | account: { id: match.account.id }, |
| 84 | }; |
| 85 | }); |
| 86 | |
| 87 | /** |
| 88 | * Exhaustively enumerate every zone in an account. Used by `list()` lifecycle |
| 89 | * operations on zone-scoped resources to fan out across all zones. Returns only |
| 90 | * the stable `{ id, name }` pair each caller needs to drive a per-zone list. |
| 91 | */ |
| 92 | export const listAllZones = ( |
| 93 | accountId: string, |
| 94 | ): Effect.Effect< |
| 95 | { id: string; name: string }[], |
| 96 | zones.ListZonesError, |
| 97 | Credentials | HttpClient.HttpClient |
| 98 | > => |
| 99 | zones.listZones.pages({ account: { id: accountId } }).pipe( |
| 100 | Stream.runCollect, |
| 101 | Effect.map((chunk) => |
| 102 | Array.from(chunk).flatMap((page) => |
| 103 | (page.result ?? []).map((zone) => ({ id: zone.id, name: zone.name })), |
| 104 | ), |
| 105 | ), |
| 106 | ); |
| 107 | |
| 108 | /** Hostname plus each parent label, longest first — used to infer a zone. */ |
| 109 | export const zoneNameCandidates = (hostname: string): string[] => { |
| 110 | const parts = hostname.split("."); |
| 111 | return parts.slice(0, -1).map((_, index) => parts.slice(index).join(".")); |
| 112 | }; |