The best way to control Claude Code, Codex and more from any computer or phone

main
ts 155 lines 5.59 KB
Raw
1 import * as appsync from "@distilled.cloud/aws/appsync";
2 import { isResolved } from "alchemy/Diff";
3 import type { Input } from "alchemy/Input";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import type { Providers } from "../Providers.ts";
9 import { retryConcurrentModification } from "./common.ts";
10 import type { AppSyncDomainName } from "./DomainName.ts";
11 import type { GraphqlApi } from "./GraphqlApi.ts";
12
13 export interface ApiAssociationProps {
14 /**
15 * The custom domain name to associate. Changing it triggers a
16 * replacement.
17 */
18 domainName: string;
19 /**
20 * ID of the GraphQL API served at the domain. Changing it triggers a
21 * replacement (a domain serves exactly one API).
22 */
23 apiId: string;
24 }
25
26 export interface AppSyncApiAssociation extends Resource<
27 "AWS.AppSync.ApiAssociation",
28 ApiAssociationProps,
29 {
30 /** The associated domain name. */
31 domainName: string;
32 /** The associated API. */
33 apiId: string;
34 },
35 never,
36 Providers
37 > {}
38
39 /**
40 * Associates a GraphQL API with a custom {@link DomainName}
41 * (existence-only resource — a domain serves exactly one API).
42 * ### Associating an API
43 * **Example:** Serve an API at a custom domain
44 * ```typescript
45 * yield* AppSync.ApiAssociation("Assoc", { domain, api });
46 * ```
47 *
48 * @resource
49 */
50 export const ApiAssociationResource = Resource<AppSyncApiAssociation>("AWS.AppSync.ApiAssociation");
51
52 export interface ApiAssociationInputProps {
53 /** The `DomainName` resource (preferred). Alternatively pass `domainName`. */
54 domain?: AppSyncDomainName;
55 domainName?: Input<string>;
56 /** The `GraphqlApi` resource (preferred). Alternatively pass `apiId`. */
57 api?: GraphqlApi;
58 apiId?: Input<string>;
59 }
60
61 /**
62 * User-facing wrapper for the ApiAssociation resource. Accepts the
63 * `DomainName` and `GraphqlApi` resources directly.
64 */
65 export const ApiAssociation = (id: string, props: ApiAssociationInputProps) =>
66 Effect.gen(function* () {
67 const domainName = props.domainName ?? props.domain?.domainName;
68 const apiId = props.apiId ?? props.api?.apiId;
69 if (!domainName || !apiId) {
70 return yield* Effect.die("ApiAssociation requires `domain`/`domainName` and `api`/`apiId`.");
71 }
72 return yield* ApiAssociationResource(id, { domainName, apiId } as any);
73 });
74
75 export const ApiAssociationProvider = () =>
76 Provider.effect(
77 ApiAssociationResource,
78 Effect.gen(function* () {
79 const getAssociationSafe = (domainName: string) =>
80 appsync.getApiAssociation({ domainName }).pipe(
81 Effect.map((response) => response.apiAssociation),
82 Effect.catchTag("NotFoundException", () => Effect.undefined),
83 );
84
85 /** Associations transition PROCESSING → SUCCESS/FAILED (~seconds). */
86 const waitForSettled = (domainName: string) =>
87 getAssociationSafe(domainName).pipe(
88 Effect.repeat({
89 schedule: Schedule.fixed("2 seconds"),
90 until: (association) =>
91 association === undefined || association.associationStatus !== "PROCESSING",
92 times: 30,
93 }),
94 );
95
96 return ApiAssociationResource.Provider.of({
97 stables: ["domainName", "apiId"],
98
99 // Sub-resource keyed entirely by its custom domain (domainName) with no global
100 // enumeration API of its own — nuke reaches it through the parent's
101 // deletion, so enumeration returns empty per the ProviderService
102 // doctrine.
103 list: () => Effect.succeed([]),
104
105 read: Effect.fn(function* ({ olds, output }) {
106 const domainName = output?.domainName ?? olds?.domainName;
107 if (domainName === undefined) return undefined;
108 const association = yield* getAssociationSafe(domainName);
109 if (association?.apiId == null) return undefined;
110 return { domainName, apiId: association.apiId };
111 }),
112
113 diff: Effect.fn(function* ({ news, olds }) {
114 if (!isResolved(news)) return undefined;
115 if (news.domainName !== olds.domainName || news.apiId !== olds.apiId) {
116 return { action: "replace" } as const;
117 }
118 }),
119
120 reconcile: Effect.fn(function* ({ news, session }) {
121 // Existence-only: observe → associate when missing or pointing
122 // at a different API (associateApi is an upsert per domain).
123 const observed = yield* getAssociationSafe(news.domainName);
124 if (observed?.apiId !== news.apiId) {
125 yield* retryConcurrentModification(
126 appsync.associateApi({
127 domainName: news.domainName,
128 apiId: news.apiId,
129 }),
130 );
131 yield* waitForSettled(news.domainName);
132 yield* session.note(`Associated ${news.apiId} with ${news.domainName}`);
133 }
134 return { domainName: news.domainName, apiId: news.apiId };
135 }),
136
137 delete: Effect.fn(function* ({ output }) {
138 yield* retryConcurrentModification(
139 appsync
140 .disassociateApi({ domainName: output.domainName })
141 .pipe(Effect.catchTag("NotFoundException", () => Effect.void)),
142 );
143 // Wait until the association is gone so a dependent DomainName
144 // delete doesn't race the detach (bounded ~60s).
145 yield* getAssociationSafe(output.domainName).pipe(
146 Effect.repeat({
147 schedule: Schedule.fixed("2 seconds"),
148 until: (association) => association?.apiId == null,
149 times: 30,
150 }),
151 );
152 }),
153 });
154 }),
155 );