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

main
ts 42 lines 1.46 KB
Raw
1 import * as frauddetector from "@distilled.cloud/aws/frauddetector";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * Coerce a FraudDetector wire tag list (`{ key, value }[]`) into a plain
7 * `Record<string, string>`.
8 */
9 export const toTagRecord = (tags: frauddetector.Tag[] | undefined): Record<string, string> =>
10 Object.fromEntries((tags ?? []).map((t) => [t.key, t.value] as const));
11
12 /**
13 * Read the observed tags of a FraudDetector resource by ARN. Tag reads are
14 * best-effort — a failure (e.g. a race with deletion) reports no tags.
15 */
16 export const readFraudDetectorTags = Effect.fn(function* (arn: string) {
17 const response = yield* frauddetector
18 .listTagsForResource({ resourceARN: arn })
19 .pipe(Effect.orElseSucceed(() => undefined));
20 return toTagRecord(response?.tags);
21 });
22
23 /**
24 * Sync tags on a FraudDetector resource: diff the OBSERVED cloud tags against
25 * the desired set and apply only the delta.
26 */
27 export const syncFraudDetectorTags = Effect.fn(function* (
28 arn: string,
29 desiredTags: Record<string, string>,
30 ) {
31 const observedTags = yield* readFraudDetectorTags(arn);
32 const { removed, upsert } = diffTags(observedTags, desiredTags);
33 if (upsert.length > 0) {
34 yield* frauddetector.tagResource({
35 resourceARN: arn,
36 tags: upsert.map((t) => ({ key: t.Key, value: t.Value })),
37 });
38 }
39 if (removed.length > 0) {
40 yield* frauddetector.untagResource({ resourceARN: arn, tagKeys: removed });
41 }
42 });