@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 87 lines 3.01 KB
Raw
1 import * as ses from "@distilled.cloud/aws/ses";
2 import * as Provider from "alchemy/Provider";
3 import { Resource } from "alchemy/Resource";
4 import * as Effect from "effect/Effect";
5 import type { Providers } from "../Providers.ts";
6
7 export interface ActiveReceiptRuleSetProps {
8 /**
9 * Name of the receipt rule set to make active for the account in the current
10 * region. Typically the `ruleSetName` output of a `SES.ReceiptRuleSet`.
11 * Re-pointing to a different rule set updates the active pointer in place.
12 */
13 ruleSetName: string;
14 }
15
16 export interface ActiveReceiptRuleSet extends Resource<
17 "AWS.SES.ActiveReceiptRuleSet",
18 ActiveReceiptRuleSetProps,
19 {
20 /** Name of the rule set that is active for the account. */
21 ruleSetName: string;
22 },
23 never,
24 Providers
25 > {}
26
27 /**
28 * The account's active Amazon SES receipt rule set — the single rule set (per
29 * account, per region) that SES actually evaluates against inbound mail.
30 *
31 * This is an account-level singleton pointer, not a container: only one rule
32 * set can be active at a time. Deleting this resource deactivates email
33 * receiving (clears the pointer) only when the account is still pointed at the
34 * rule set this resource set; if something else has since become active, the
35 * delete is a no-op.
36 * ### Activating a Rule Set
37 * **Example:** Make a Rule Set Active
38 * ```typescript
39 * import * as SES from "@alchemy.run/aws/SES";
40 *
41 * const ruleSet = yield* SES.ReceiptRuleSet("Inbound", {});
42 * const active = yield* SES.ActiveReceiptRuleSet("Active", {
43 * ruleSetName: ruleSet.ruleSetName,
44 * });
45 * ```
46 *
47 * @resource
48 */
49 export const ActiveReceiptRuleSet = Resource<ActiveReceiptRuleSet>("AWS.SES.ActiveReceiptRuleSet");
50
51 const activeRuleSetName = Effect.fn(function* () {
52 const response = yield* ses.describeActiveReceiptRuleSet({});
53 return response.Metadata?.Name;
54 });
55
56 export const ActiveReceiptRuleSetProvider = () =>
57 Provider.succeed(ActiveReceiptRuleSet, {
58 // Account/region singleton setting: "deleting" it only clears the active
59 // pointer, so nuke skips it (and would otherwise disable email receiving
60 // account-wide).
61 nuke: { singleton: true },
62
63 list: () => Effect.succeed([]),
64
65 read: Effect.fn(function* () {
66 const name = yield* activeRuleSetName();
67 return name ? { ruleSetName: name } : undefined;
68 }),
69
70 reconcile: Effect.fn(function* ({ news }) {
71 // OBSERVE the current active pointer; only re-point on a mismatch.
72 const observed = yield* activeRuleSetName();
73 if (observed !== news.ruleSetName) {
74 yield* ses.setActiveReceiptRuleSet({ RuleSetName: news.ruleSetName });
75 }
76 return { ruleSetName: news.ruleSetName };
77 }),
78
79 delete: Effect.fn(function* ({ output }) {
80 // Only deactivate if the account is still pointed at the rule set we
81 // activated; otherwise another owner has taken over and we leave it be.
82 const observed = yield* activeRuleSetName();
83 if (observed === output.ruleSetName) {
84 yield* ses.setActiveReceiptRuleSet({});
85 }
86 }),
87 });