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

main
ts 188 lines 6.58 KB
Raw
1 import * as appconfig from "@distilled.cloud/aws/appconfig";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import type { Providers } from "../Providers.ts";
7
8 export interface HostedConfigurationVersionProps {
9 /**
10 * ID of the application. Changing it replaces the version.
11 */
12 applicationId: string;
13 /**
14 * ID of the (hosted) configuration profile. Changing it replaces the
15 * version.
16 */
17 configurationProfileId: string;
18 /**
19 * The configuration content itself — the YAML/JSON/text document served to
20 * clients. Hosted configuration versions are immutable; changing the
21 * content creates a new version (a replacement).
22 */
23 content: string;
24 /**
25 * MIME type of the content, e.g. `application/json`, `application/x-yaml`,
26 * or `text/plain`.
27 */
28 contentType: string;
29 /**
30 * Description of the configuration version.
31 */
32 description?: string;
33 /**
34 * A user-defined label for the version (e.g. a git SHA). Must be unique per
35 * profile.
36 */
37 versionLabel?: string;
38 }
39
40 export interface HostedConfigurationVersion extends Resource<
41 "AWS.AppConfig.HostedConfigurationVersion",
42 HostedConfigurationVersionProps,
43 {
44 applicationId: string;
45 configurationProfileId: string;
46 versionNumber: number;
47 contentType: string;
48 versionLabel: string | undefined;
49 },
50 never,
51 Providers
52 > {}
53
54 /**
55 * An AWS AppConfig hosted configuration version — the actual configuration
56 * content stored in the AppConfig hosted store. Versions are immutable: each
57 * change to the content produces a new version (a replacement), and its
58 * `versionNumber` is what you deploy through a {@link Deployment}.
59 *
60 * ### Creating a Hosted Configuration Version
61 * **Example:** JSON Configuration
62 * ```typescript
63 * const version = yield* AppConfig.HostedConfigurationVersion("V1", {
64 * applicationId: app.applicationId,
65 * configurationProfileId: profile.configurationProfileId,
66 * content: JSON.stringify({ featureX: true }),
67 * contentType: "application/json",
68 * });
69 * // version.versionNumber -> 1
70 * ```
71 *
72 * @resource
73 */
74 export const HostedConfigurationVersion = Resource<HostedConfigurationVersion>(
75 "AWS.AppConfig.HostedConfigurationVersion",
76 );
77
78 export const HostedConfigurationVersionProvider = () =>
79 Provider.effect(
80 HostedConfigurationVersion,
81 Effect.gen(function* () {
82 const readVersion = Effect.fn(function* (
83 applicationId: string,
84 configurationProfileId: string,
85 versionNumber: number,
86 ) {
87 return yield* appconfig
88 .getHostedConfigurationVersion({
89 ApplicationId: applicationId,
90 ConfigurationProfileId: configurationProfileId,
91 VersionNumber: versionNumber,
92 })
93 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
94 });
95
96 return {
97 stables: ["applicationId", "configurationProfileId", "versionNumber"],
98
99 // Hosted configuration versions are immutable and content-addressed —
100 // any change produces a fresh version.
101 diff: Effect.fn(function* ({ olds, news }) {
102 if (!isResolved(news)) return undefined;
103 if (
104 olds !== undefined &&
105 (olds.applicationId !== news.applicationId ||
106 olds.configurationProfileId !== news.configurationProfileId ||
107 olds.content !== news.content ||
108 olds.contentType !== news.contentType ||
109 (olds.description ?? undefined) !== (news.description ?? undefined) ||
110 (olds.versionLabel ?? undefined) !== (news.versionLabel ?? undefined))
111 ) {
112 return { action: "replace" } as const;
113 }
114 }),
115
116 read: Effect.fn(function* ({ output }) {
117 if (output?.versionNumber === undefined) return undefined;
118 const version = yield* readVersion(
119 output.applicationId,
120 output.configurationProfileId,
121 output.versionNumber,
122 );
123 if (version?.VersionNumber === undefined) return undefined;
124 return {
125 applicationId: output.applicationId,
126 configurationProfileId: output.configurationProfileId,
127 versionNumber: version.VersionNumber,
128 contentType: version.ContentType ?? output.contentType,
129 versionLabel: version.VersionLabel,
130 };
131 }),
132
133 reconcile: Effect.fn(function* ({ news, output, session }) {
134 // Observe — the version may already exist (crash-resumed create).
135 const existing =
136 output?.versionNumber !== undefined
137 ? yield* readVersion(
138 output.applicationId,
139 output.configurationProfileId,
140 output.versionNumber,
141 )
142 : undefined;
143
144 if (existing?.VersionNumber !== undefined) {
145 yield* session.note(`version-${existing.VersionNumber}`);
146 return {
147 applicationId: news.applicationId,
148 configurationProfileId: news.configurationProfileId,
149 versionNumber: existing.VersionNumber,
150 contentType: existing.ContentType ?? news.contentType,
151 versionLabel: existing.VersionLabel,
152 };
153 }
154
155 const created = yield* appconfig.createHostedConfigurationVersion({
156 ApplicationId: news.applicationId,
157 ConfigurationProfileId: news.configurationProfileId,
158 Content: news.content,
159 ContentType: news.contentType,
160 Description: news.description,
161 VersionLabel: news.versionLabel,
162 });
163
164 yield* session.note(`version-${created.VersionNumber}`);
165 return {
166 applicationId: news.applicationId,
167 configurationProfileId: news.configurationProfileId,
168 versionNumber: created.VersionNumber!,
169 contentType: created.ContentType ?? news.contentType,
170 versionLabel: created.VersionLabel,
171 };
172 }),
173
174 delete: Effect.fn(function* ({ output }) {
175 yield* appconfig
176 .deleteHostedConfigurationVersion({
177 ApplicationId: output.applicationId,
178 ConfigurationProfileId: output.configurationProfileId,
179 VersionNumber: output.versionNumber,
180 })
181 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
182 }),
183
184 // Hosted configuration versions are keyed under their parent profile.
185 list: () => Effect.succeed([]),
186 };
187 }),
188 );