The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as mm from "@distilled.cloud/aws/mailmanager"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { createInternalTags, createTagsList, hasAlchemyTags } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { readMailManagerTags, syncMailManagerTags } from "./internal.ts"; |
| 12 | |
| 13 | export class AddonSubscriptionUnexpectedResponse extends Data.TaggedError( |
| 14 | "AddonSubscriptionUnexpectedResponse", |
| 15 | )<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | export interface AddonSubscriptionProps { |
| 21 | /** |
| 22 | * Name of the Add On product to subscribe to (e.g. `TRENDMICRO_VSAPI`, |
| 23 | * `SPAMHAUS_DBL`, `ABUSIX_MAIL_INTELLIGENCE`). Subscribing accepts the Add |
| 24 | * On's terms of use and additional pricing. Immutable — changing the name |
| 25 | * replaces the subscription. |
| 26 | */ |
| 27 | addonName: string; |
| 28 | /** |
| 29 | * Tags applied to the subscription. Alchemy ownership tags are merged in |
| 30 | * automatically. |
| 31 | */ |
| 32 | tags?: Record<string, string>; |
| 33 | } |
| 34 | |
| 35 | export interface AddonSubscription extends Resource< |
| 36 | "AWS.MailManager.AddonSubscription", |
| 37 | AddonSubscriptionProps, |
| 38 | { |
| 39 | /** Server-assigned ID of the Add On subscription. */ |
| 40 | addonSubscriptionId: string; |
| 41 | /** ARN of the Add On subscription. */ |
| 42 | addonSubscriptionArn: string; |
| 43 | /** Name of the subscribed Add On product. */ |
| 44 | addonName: string; |
| 45 | }, |
| 46 | never, |
| 47 | Providers |
| 48 | > {} |
| 49 | |
| 50 | /** |
| 51 | * An SES Mail Manager Add On subscription — the acceptance of a third-party |
| 52 | * Add On's terms of use and additional pricing. An |
| 53 | * {@link AddonInstance} created from the subscription is what rule sets and |
| 54 | * traffic policies actually reference. |
| 55 | * |
| 56 | * Subscriptions are immutable after creation (only tags update in place). |
| 57 | * |
| 58 | * :::warning |
| 59 | * Creating a subscription accepts the Add On's **additional pricing**. |
| 60 | * ::: |
| 61 | * ### Subscribing to an Add On |
| 62 | * **Example:** Spamhaus DBL |
| 63 | * ```typescript |
| 64 | * import * as MailManager from "@alchemy.run/aws/MailManager"; |
| 65 | * |
| 66 | * const subscription = yield* MailManager.AddonSubscription("Spamhaus", { |
| 67 | * addonName: "SPAMHAUS_DBL", |
| 68 | * }); |
| 69 | * const instance = yield* MailManager.AddonInstance("SpamhausInstance", { |
| 70 | * addonSubscriptionId: subscription.addonSubscriptionId, |
| 71 | * }); |
| 72 | * ``` |
| 73 | * |
| 74 | * @resource |
| 75 | */ |
| 76 | export const AddonSubscription = Resource<AddonSubscription>("AWS.MailManager.AddonSubscription"); |
| 77 | |
| 78 | export const AddonSubscriptionProvider = () => |
| 79 | Provider.effect( |
| 80 | AddonSubscription, |
| 81 | Effect.gen(function* () { |
| 82 | const getById = (addonSubscriptionId: string) => |
| 83 | mm |
| 84 | .getAddonSubscription({ AddonSubscriptionId: addonSubscriptionId }) |
| 85 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 86 | |
| 87 | const listAll = mm.listAddonSubscriptions.pages({}).pipe( |
| 88 | Stream.runCollect, |
| 89 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.AddonSubscriptions ?? [])), |
| 90 | ); |
| 91 | |
| 92 | // Subscriptions have no user-supplied name — recover identity after a |
| 93 | // lost state write by searching for our ownership tags. |
| 94 | const findByAlchemyTags = Effect.fn(function* (id: string) { |
| 95 | const subscriptions = yield* listAll; |
| 96 | for (const subscription of subscriptions) { |
| 97 | if (subscription.AddonSubscriptionArn === undefined) continue; |
| 98 | const tags = yield* readMailManagerTags(subscription.AddonSubscriptionArn); |
| 99 | if (yield* hasAlchemyTags(id, tags)) return subscription; |
| 100 | } |
| 101 | return undefined; |
| 102 | }); |
| 103 | |
| 104 | const toAttrs = (subscription: mm.AddonSubscription) => |
| 105 | subscription.AddonSubscriptionId !== undefined && |
| 106 | subscription.AddonSubscriptionArn !== undefined && |
| 107 | subscription.AddonName !== undefined |
| 108 | ? { |
| 109 | addonSubscriptionId: subscription.AddonSubscriptionId, |
| 110 | addonSubscriptionArn: subscription.AddonSubscriptionArn, |
| 111 | addonName: subscription.AddonName, |
| 112 | } |
| 113 | : undefined; |
| 114 | |
| 115 | const observe = Effect.fn(function* ( |
| 116 | id: string, |
| 117 | output: AddonSubscription["Attributes"] | undefined, |
| 118 | ) { |
| 119 | if (output?.addonSubscriptionId !== undefined) { |
| 120 | const found = yield* getById(output.addonSubscriptionId); |
| 121 | if (found !== undefined) { |
| 122 | return { |
| 123 | AddonSubscriptionId: output.addonSubscriptionId, |
| 124 | AddonSubscriptionArn: found.AddonSubscriptionArn, |
| 125 | AddonName: found.AddonName, |
| 126 | } satisfies mm.AddonSubscription; |
| 127 | } |
| 128 | } |
| 129 | return yield* findByAlchemyTags(id); |
| 130 | }); |
| 131 | |
| 132 | return AddonSubscription.Provider.of({ |
| 133 | stables: ["addonSubscriptionId", "addonSubscriptionArn", "addonName"], |
| 134 | |
| 135 | list: () => |
| 136 | listAll.pipe( |
| 137 | Effect.map((subscriptions) => |
| 138 | subscriptions.flatMap((s) => { |
| 139 | const attrs = toAttrs(s); |
| 140 | return attrs === undefined ? [] : [attrs]; |
| 141 | }), |
| 142 | ), |
| 143 | ), |
| 144 | |
| 145 | read: Effect.fn(function* ({ id, output }) { |
| 146 | const subscription = yield* observe(id, output); |
| 147 | if (subscription === undefined) return undefined; |
| 148 | const attrs = toAttrs(subscription); |
| 149 | if (attrs === undefined) return undefined; |
| 150 | const tags = yield* readMailManagerTags(attrs.addonSubscriptionArn); |
| 151 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 152 | }), |
| 153 | |
| 154 | // The Add On product is create-only — changing it replaces the |
| 155 | // subscription. |
| 156 | diff: Effect.fn(function* ({ news, olds }) { |
| 157 | if (!isResolved(news)) return undefined; |
| 158 | if (olds !== undefined && olds.addonName !== news.addonName) { |
| 159 | return { action: "replace" } as const; |
| 160 | } |
| 161 | }), |
| 162 | |
| 163 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 164 | const internalTags = yield* createInternalTags(id); |
| 165 | const desiredTags = { ...news.tags, ...internalTags }; |
| 166 | |
| 167 | // 1. OBSERVE — by cached id, falling back to an ownership-tag |
| 168 | // search. |
| 169 | let subscription = yield* observe(id, output); |
| 170 | |
| 171 | // 2. ENSURE — create if missing; a Conflict (already subscribed) |
| 172 | // re-observes via the tag search. |
| 173 | if (subscription === undefined) { |
| 174 | yield* session.note(`subscribing to Add On ${news.addonName}`); |
| 175 | const created = yield* mm |
| 176 | .createAddonSubscription({ |
| 177 | AddonName: news.addonName, |
| 178 | Tags: createTagsList(desiredTags), |
| 179 | }) |
| 180 | .pipe(Effect.catchTag("ConflictException", () => Effect.undefined)); |
| 181 | if (created !== undefined) { |
| 182 | const found = yield* getById(created.AddonSubscriptionId); |
| 183 | subscription = { |
| 184 | AddonSubscriptionId: created.AddonSubscriptionId, |
| 185 | AddonSubscriptionArn: found?.AddonSubscriptionArn, |
| 186 | AddonName: found?.AddonName ?? news.addonName, |
| 187 | }; |
| 188 | } else { |
| 189 | subscription = yield* findByAlchemyTags(id); |
| 190 | } |
| 191 | } |
| 192 | const attrs = subscription !== undefined ? toAttrs(subscription) : undefined; |
| 193 | if (attrs === undefined) { |
| 194 | return yield* new AddonSubscriptionUnexpectedResponse({ |
| 195 | message: `Mail Manager Add On subscription for '${news.addonName}' not found after create`, |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | // 3. SYNC — only tags are mutable. |
| 200 | yield* syncMailManagerTags(attrs.addonSubscriptionArn, desiredTags); |
| 201 | |
| 202 | yield* session.note(attrs.addonSubscriptionId); |
| 203 | return attrs; |
| 204 | }), |
| 205 | |
| 206 | delete: Effect.fn(function* ({ output }) { |
| 207 | // DeleteAddonSubscription is natively idempotent — deleting a |
| 208 | // nonexistent subscription succeeds (verified live). |
| 209 | yield* mm |
| 210 | .deleteAddonSubscription({ |
| 211 | AddonSubscriptionId: output.addonSubscriptionId, |
| 212 | }) |
| 213 | .pipe(Effect.asVoid); |
| 214 | }), |
| 215 | }); |
| 216 | }), |
| 217 | ); |