The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as appsync from "@distilled.cloud/aws/appsync"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { createInternalTags, hasAlchemyTags } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { retryConcurrentModification, syncAppSyncTags, tagRecord } from "./common.ts"; |
| 11 | |
| 12 | export interface DomainNameProps { |
| 13 | /** |
| 14 | * The custom domain name, e.g. `api.example.com`. Changing it triggers |
| 15 | * a replacement. |
| 16 | */ |
| 17 | domainName: string; |
| 18 | /** |
| 19 | * ARN of an ACM certificate covering the domain. AppSync custom domains |
| 20 | * are CloudFront-backed, so the certificate **must live in us-east-1**. |
| 21 | * Changing it triggers a replacement. |
| 22 | */ |
| 23 | certificateArn: string; |
| 24 | /** Description of the domain. */ |
| 25 | description?: string; |
| 26 | /** Tags to apply. Merged with internal Alchemy tags. */ |
| 27 | tags?: Record<string, string>; |
| 28 | } |
| 29 | |
| 30 | export interface AppSyncDomainName extends Resource< |
| 31 | "AWS.AppSync.DomainName", |
| 32 | DomainNameProps, |
| 33 | { |
| 34 | /** The custom domain name. */ |
| 35 | domainName: string; |
| 36 | /** The domain name ARN. */ |
| 37 | domainNameArn: string | undefined; |
| 38 | /** The certificate ARN. */ |
| 39 | certificateArn: string; |
| 40 | /** |
| 41 | * The CloudFront target (`dxxxx.cloudfront.net`) to point a CNAME / |
| 42 | * Route53 alias at. |
| 43 | */ |
| 44 | appsyncDomainName: string | undefined; |
| 45 | /** The Route53 hosted zone ID for alias records. */ |
| 46 | hostedZoneId: string | undefined; |
| 47 | }, |
| 48 | never, |
| 49 | Providers |
| 50 | > {} |
| 51 | |
| 52 | /** |
| 53 | * A custom domain name for AppSync GraphQL APIs. |
| 54 | * |
| 55 | * Requires an ACM certificate **in us-east-1** (the domain is |
| 56 | * CloudFront-backed). Attach an API with {@link ApiAssociation} and point |
| 57 | * DNS at the `appsyncDomainName` attribute. |
| 58 | * ### Creating Custom Domains |
| 59 | * **Example:** Custom domain + API association |
| 60 | * ```typescript |
| 61 | * const domain = yield* AppSync.DomainName("Domain", { |
| 62 | * domainName: "api.example.com", |
| 63 | * certificateArn: usEast1Cert.certificateArn, |
| 64 | * }); |
| 65 | * yield* AppSync.ApiAssociation("Assoc", { domain, api }); |
| 66 | * // CNAME api.example.com → domain.appsyncDomainName |
| 67 | * ``` |
| 68 | * |
| 69 | * @resource |
| 70 | */ |
| 71 | export const DomainName = Resource<AppSyncDomainName>("AWS.AppSync.DomainName"); |
| 72 | |
| 73 | export const DomainNameProvider = () => |
| 74 | Provider.effect( |
| 75 | DomainName, |
| 76 | Effect.gen(function* () { |
| 77 | const getDomainSafe = (domainName: string) => |
| 78 | appsync.getDomainName({ domainName }).pipe( |
| 79 | Effect.map((response) => response.domainNameConfig), |
| 80 | Effect.catchTag("NotFoundException", () => Effect.undefined), |
| 81 | ); |
| 82 | |
| 83 | const toAttributes = (config: appsync.DomainNameConfig): AppSyncDomainName["Attributes"] => ({ |
| 84 | domainName: config.domainName!, |
| 85 | domainNameArn: config.domainNameArn, |
| 86 | certificateArn: config.certificateArn!, |
| 87 | appsyncDomainName: config.appsyncDomainName, |
| 88 | hostedZoneId: config.hostedZoneId, |
| 89 | }); |
| 90 | |
| 91 | return DomainName.Provider.of({ |
| 92 | stables: ["domainName", "domainNameArn", "appsyncDomainName", "hostedZoneId"], |
| 93 | |
| 94 | list: () => |
| 95 | Effect.gen(function* () { |
| 96 | const pages = yield* appsync.listDomainNames.pages({}).pipe(Stream.runCollect); |
| 97 | return Array.from(pages) |
| 98 | .flatMap((page) => page.domainNameConfigs ?? []) |
| 99 | .filter((config) => config.domainName != null) |
| 100 | .map(toAttributes); |
| 101 | }), |
| 102 | |
| 103 | read: Effect.fn(function* ({ id, olds, output }) { |
| 104 | const domainName = output?.domainName ?? olds?.domainName; |
| 105 | if (domainName === undefined) return undefined; |
| 106 | const config = yield* getDomainSafe(domainName); |
| 107 | if (config?.domainName == null) return undefined; |
| 108 | const attrs = toAttributes(config); |
| 109 | return (yield* hasAlchemyTags(id, tagRecord(config.tags))) ? attrs : Unowned(attrs); |
| 110 | }), |
| 111 | |
| 112 | diff: Effect.fn(function* ({ news, olds }) { |
| 113 | if (!isResolved(news)) return undefined; |
| 114 | if (news.domainName !== olds.domainName || news.certificateArn !== olds.certificateArn) { |
| 115 | return { action: "replace" } as const; |
| 116 | } |
| 117 | // description/tags converge via update |
| 118 | }), |
| 119 | |
| 120 | reconcile: Effect.fn(function* ({ id, news, session }) { |
| 121 | const internalTags = yield* createInternalTags(id); |
| 122 | const desiredTags = { ...news.tags, ...internalTags }; |
| 123 | |
| 124 | // 1. OBSERVE |
| 125 | let observed = yield* getDomainSafe(news.domainName); |
| 126 | |
| 127 | if (observed?.domainName == null) { |
| 128 | // 2. ENSURE |
| 129 | const created = yield* retryConcurrentModification( |
| 130 | appsync.createDomainName({ |
| 131 | domainName: news.domainName, |
| 132 | certificateArn: news.certificateArn, |
| 133 | description: news.description, |
| 134 | tags: desiredTags, |
| 135 | }), |
| 136 | ); |
| 137 | observed = created.domainNameConfig!; |
| 138 | yield* session.note(`Created domain ${news.domainName}`); |
| 139 | } else if (news.description !== undefined && observed.description !== news.description) { |
| 140 | // 3. SYNC |
| 141 | const updated = yield* retryConcurrentModification( |
| 142 | appsync.updateDomainName({ |
| 143 | domainName: news.domainName, |
| 144 | description: news.description, |
| 145 | }), |
| 146 | ); |
| 147 | observed = updated.domainNameConfig ?? observed; |
| 148 | yield* session.note(`Updated domain ${news.domainName}`); |
| 149 | } |
| 150 | |
| 151 | // 3b. SYNC TAGS — against OBSERVED cloud tags. |
| 152 | if (observed.domainNameArn !== undefined) { |
| 153 | yield* syncAppSyncTags({ |
| 154 | resourceArn: observed.domainNameArn, |
| 155 | oldTags: tagRecord(observed.tags), |
| 156 | newTags: desiredTags, |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | yield* session.note(news.domainName); |
| 161 | return toAttributes(observed); |
| 162 | }), |
| 163 | |
| 164 | delete: Effect.fn(function* ({ output }) { |
| 165 | // A domain with a live association cannot be deleted; the |
| 166 | // association resource is destroyed first (dependency order), |
| 167 | // but the detach itself is eventually consistent — the |
| 168 | // ConcurrentModification retry rides that out. |
| 169 | yield* retryConcurrentModification( |
| 170 | appsync |
| 171 | .deleteDomainName({ domainName: output.domainName }) |
| 172 | .pipe(Effect.catchTag("NotFoundException", () => Effect.void)), |
| 173 | ); |
| 174 | }), |
| 175 | }); |
| 176 | }), |
| 177 | ); |