@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 152 lines 5.31 KB
Raw
1 import * as ses from "@distilled.cloud/aws/ses";
2 import { isResolved } from "alchemy/Diff";
3 import { createPhysicalName } from "alchemy/PhysicalName";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Effect from "effect/Effect";
7 import type { Providers } from "../Providers.ts";
8
9 export interface ReceiptRuleSetProps {
10 /**
11 * Name of the receipt rule set. May contain letters, numbers, dashes,
12 * underscores and periods, up to 64 characters, and must start and end with
13 * a letter or number. If omitted, a deterministic physical name is generated
14 * from the app, stage, and logical ID. Changing the name replaces the rule
15 * set.
16 */
17 ruleSetName?: string;
18 }
19
20 export interface ReceiptRuleSet extends Resource<
21 "AWS.SES.ReceiptRuleSet",
22 ReceiptRuleSetProps,
23 {
24 /** Name of the receipt rule set. */
25 ruleSetName: string;
26 },
27 never,
28 Providers
29 > {}
30
31 /**
32 * An Amazon SES receipt rule set — the ordered container for the receipt rules
33 * that decide what happens to inbound email (deliver to S3, invoke a Lambda,
34 * publish to SNS, bounce, etc.).
35 *
36 * A rule set is an empty container on creation; add `SES.ReceiptRule`s to it
37 * and point the account at it with `SES.ActiveReceiptRuleSet` to start
38 * processing mail. Email receiving is only available in a subset of regions
39 * (e.g. `us-east-1`, `us-west-2`, `eu-west-1`).
40 * ### Creating Rule Sets
41 * **Example:** Basic Rule Set
42 * ```typescript
43 * import * as SES from "@alchemy.run/aws/SES";
44 *
45 * const ruleSet = yield* SES.ReceiptRuleSet("Inbound", {});
46 * ```
47 *
48 * **Example:** Named Rule Set
49 * ```typescript
50 * const ruleSet = yield* SES.ReceiptRuleSet("Inbound", {
51 * ruleSetName: "my-inbound-rules",
52 * });
53 * ```
54 *
55 * ### Activating a Rule Set
56 * **Example:** Point the Account at a Rule Set
57 * ```typescript
58 * const ruleSet = yield* SES.ReceiptRuleSet("Inbound", {});
59 * yield* SES.ActiveReceiptRuleSet("Active", {
60 * ruleSetName: ruleSet.ruleSetName,
61 * });
62 * ```
63 *
64 * @resource
65 */
66 export const ReceiptRuleSet = Resource<ReceiptRuleSet>("AWS.SES.ReceiptRuleSet");
67
68 export const ReceiptRuleSetProvider = () =>
69 Provider.effect(
70 ReceiptRuleSet,
71 Effect.gen(function* () {
72 const createName = Effect.fn(function* (
73 id: string,
74 props: Pick<ReceiptRuleSetProps, "ruleSetName">,
75 ) {
76 return props.ruleSetName ?? (yield* createPhysicalName({ id, maxLength: 64 }));
77 });
78
79 const describe = Effect.fn(function* (name: string) {
80 return yield* ses
81 .describeReceiptRuleSet({ RuleSetName: name })
82 .pipe(Effect.catchTag("RuleSetDoesNotExistException", () => Effect.undefined));
83 });
84
85 return ReceiptRuleSet.Provider.of({
86 stables: ["ruleSetName"],
87
88 // Account/region-scoped: enumerate every rule set so leaked test
89 // resources are cleaned by nuke. Classic receipt rule sets carry no
90 // tags, so there is no ownership signal to filter on.
91 list: () =>
92 Effect.gen(function* () {
93 const rows: { ruleSetName: string }[] = [];
94 let nextToken: string | undefined;
95 do {
96 const page = yield* ses.listReceiptRuleSets({
97 NextToken: nextToken,
98 });
99 for (const set of page.RuleSets ?? []) {
100 if (set.Name) rows.push({ ruleSetName: set.Name });
101 }
102 nextToken = page.NextToken;
103 } while (nextToken);
104 return rows;
105 }),
106
107 read: Effect.fn(function* ({ id, olds, output }) {
108 const name = output?.ruleSetName ?? (yield* createName(id, olds));
109 const found = yield* describe(name);
110 // Receipt rule sets are untagged in the classic API, so there is no
111 // way to distinguish a foreign rule set from one we own. Existence
112 // at our deterministic name is treated as ownership.
113 return found ? { ruleSetName: name } : undefined;
114 }),
115
116 diff: Effect.fn(function* ({ id, news, olds }) {
117 if (!isResolved(news)) return undefined;
118 const oldName = yield* createName(id, olds);
119 const newName = yield* createName(id, news);
120 if (oldName !== newName) {
121 return { action: "replace" } as const;
122 }
123 }),
124
125 reconcile: Effect.fn(function* ({ id, news, output }) {
126 const name = output?.ruleSetName ?? (yield* createName(id, news));
127
128 // OBSERVE — cloud state is authoritative.
129 const observed = yield* describe(name);
130
131 // ENSURE — create if missing; AlreadyExists is a race, not a failure.
132 if (observed === undefined) {
133 yield* ses
134 .createReceiptRuleSet({ RuleSetName: name })
135 .pipe(Effect.catchTag("AlreadyExistsException", () => Effect.succeed({})));
136 }
137
138 // A rule set has no mutable aspects of its own; rules are separate
139 // `SES.ReceiptRule` resources.
140 return { ruleSetName: name };
141 }),
142
143 delete: Effect.fn(function* ({ output }) {
144 // deleteReceiptRuleSet is idempotent for a missing set; the active
145 // set cannot be deleted and surfaces as a typed CannotDeleteException.
146 yield* ses.deleteReceiptRuleSet({
147 RuleSetName: output.ruleSetName,
148 });
149 }),
150 });
151 }),
152 );