The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as AWSBindings from "../AWSBindings.ts"; |
| 3 | import { Region } from "../Region.ts"; |
| 4 | import type { EmailContact } from "./EmailContact.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for the AWS User Notifications Contacts HTTP bindings. |
| 8 | * |
| 9 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 10 | * a `Layer.effect(Cap, makeEmailContactHttpBinding({ … }))` over the builder |
| 11 | * below. Everything except the operation and the IAM action list is |
| 12 | * boilerplate. |
| 13 | * |
| 14 | * User Notifications Contacts is a global service managed from a single |
| 15 | * control-plane region (`us-east-1`) — like its sibling `notifications` |
| 16 | * service — so every operation is resolved with the Region pinned. |
| 17 | */ |
| 18 | const US_EAST_1 = "us-east-1"; |
| 19 | const pinContactsRegion = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 20 | effect.pipe(Effect.provideService(Region, Effect.succeed(US_EAST_1))); |
| 21 | |
| 22 | /** |
| 23 | * Build the impl Effect for a User Notifications Contacts operation scoped |
| 24 | * to one {@link EmailContact}: the deploy-time half grants `actions` on the |
| 25 | * bound contact's ARN, and the runtime half injects the contact's ARN as |
| 26 | * the `arn` of every request. |
| 27 | */ |
| 28 | export const makeEmailContactHttpBinding = < |
| 29 | I extends { arn: string }, |
| 30 | A, |
| 31 | E, |
| 32 | R, |
| 33 | Req = Omit<I, "arn">, |
| 34 | >(options: { |
| 35 | /** Fully-qualified binding tag, e.g. `AWS.NotificationsContacts.SendActivationCode`. */ |
| 36 | tag: string; |
| 37 | /** The distilled operation. */ |
| 38 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 39 | /** IAM actions granted on the email contact ARN. */ |
| 40 | actions: readonly string[]; |
| 41 | }) => |
| 42 | Effect.gen(function* () { |
| 43 | const bind = yield* AWSBindings.Bind; |
| 44 | const op = yield* options.operation.pipe(pinContactsRegion, AWSBindings.withCredentials); |
| 45 | |
| 46 | return Effect.fn(function* (contact: EmailContact) { |
| 47 | // Output yields a DEFERRED effect — resolve again per invocation below. |
| 48 | const ContactArn = yield* contact.emailContactArn; |
| 49 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 50 | yield* bind`${options.tag}(${contact})`({ |
| 51 | policyStatements: [ |
| 52 | { |
| 53 | Effect: "Allow", |
| 54 | Action: [...options.actions], |
| 55 | Resource: [contact.emailContactArn], |
| 56 | }, |
| 57 | ], |
| 58 | }); |
| 59 | } |
| 60 | return Effect.fn(`${options.tag}(${contact.LogicalId})`)(function* (request?: Req) { |
| 61 | const arn = yield* ContactArn; |
| 62 | // The region must also be pinned at the call site: the yield-time |
| 63 | // snapshot is only a fallback — the calling fiber's ambient Region |
| 64 | // (the host Function's own region) wins over it. |
| 65 | return yield* pinContactsRegion(op({ ...(request as object), arn } as I)); |
| 66 | }); |
| 67 | }); |
| 68 | }); |