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 { pinNotificationsRegion } from "./internal.ts"; |
| 4 | import type { NotificationConfiguration } from "./NotificationConfiguration.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared HTTP scaffolding for the AWS User Notifications runtime bindings. |
| 8 | * |
| 9 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 10 | * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders |
| 11 | * below. Everything except the operation, the IAM action, and (for the |
| 12 | * configuration-scoped builder) the injected ARN is boilerplate. |
| 13 | * |
| 14 | * User Notifications is a global service managed from `us-east-1` (other |
| 15 | * regions reject management calls), so every operation is resolved with the |
| 16 | * Region pinned via {@link pinNotificationsRegion} — exactly like the |
| 17 | * resource providers in `internal.ts`. |
| 18 | * |
| 19 | * Per the `notifications` service authorization reference, the event/read |
| 20 | * query actions authorize on event/configuration ARN patterns that are not |
| 21 | * known until runtime, so the account-level builder grants on |
| 22 | * `Resource: ["*"]`. `notifications:ListChannels` authorizes on the |
| 23 | * notification configuration, so the configuration-scoped builder grants on |
| 24 | * the bound configuration's ARN. |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * Build the impl Effect for an account-level User Notifications operation |
| 29 | * (the notification-event and managed-notification read APIs — none of |
| 30 | * which are scoped to a caller-owned resource). |
| 31 | */ |
| 32 | export const makeNotificationsHttpBinding = <I extends object, A, E, R>(options: { |
| 33 | /** |
| 34 | * Short capability name used in the binding sid and runtime span, e.g. |
| 35 | * `"ListNotificationEvents"`. |
| 36 | */ |
| 37 | capability: string; |
| 38 | /** IAM actions granted on `Resource: ["*"]`. */ |
| 39 | iamActions: readonly string[]; |
| 40 | /** The distilled operation implementing the capability. */ |
| 41 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 42 | }) => |
| 43 | Effect.gen(function* () { |
| 44 | const bind = yield* AWSBindings.Bind; |
| 45 | const op = yield* options.operation.pipe(pinNotificationsRegion, AWSBindings.withCredentials); |
| 46 | |
| 47 | return Effect.fn(function* () { |
| 48 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 49 | yield* bind`AWS.Notifications.${options.capability}()`({ |
| 50 | policyStatements: [ |
| 51 | { |
| 52 | Effect: "Allow", |
| 53 | Action: [...options.iamActions], |
| 54 | Resource: ["*"], |
| 55 | }, |
| 56 | ], |
| 57 | }); |
| 58 | } |
| 59 | return Effect.fn(`AWS.Notifications.${options.capability}`)(function* (request?: I) { |
| 60 | // The region must also be pinned at the call site: the yield-time |
| 61 | // snapshot is only a fallback — the calling fiber's ambient Region |
| 62 | // (the host Function's own region) wins over it. |
| 63 | return yield* pinNotificationsRegion(op((request ?? {}) as I)); |
| 64 | }); |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | /** |
| 69 | * Build the impl Effect for an operation scoped to one |
| 70 | * {@link NotificationConfiguration}. The runtime callable injects the bound |
| 71 | * configuration's ARN as the request's `notificationConfigurationArn`; the |
| 72 | * deploy-time half grants `iamActions` on the configuration's ARN. |
| 73 | */ |
| 74 | export const makeNotificationConfigurationHttpBinding = < |
| 75 | I extends { notificationConfigurationArn?: string }, |
| 76 | A, |
| 77 | E, |
| 78 | R, |
| 79 | >(options: { |
| 80 | /** |
| 81 | * Short capability name used in the binding sid and runtime span, e.g. |
| 82 | * `"ListChannels"`. |
| 83 | */ |
| 84 | capability: string; |
| 85 | /** IAM actions granted on the configuration ARN. */ |
| 86 | iamActions: readonly string[]; |
| 87 | /** The distilled operation; `notificationConfigurationArn` is injected. */ |
| 88 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 89 | }) => |
| 90 | Effect.gen(function* () { |
| 91 | const bind = yield* AWSBindings.Bind; |
| 92 | const op = yield* options.operation.pipe(pinNotificationsRegion, AWSBindings.withCredentials); |
| 93 | |
| 94 | return Effect.fn(function* (configuration: NotificationConfiguration) { |
| 95 | const configurationArn = yield* configuration.notificationConfigurationArn; |
| 96 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 97 | yield* bind`AWS.Notifications.${options.capability}(${configuration})`({ |
| 98 | policyStatements: [ |
| 99 | { |
| 100 | Effect: "Allow", |
| 101 | Action: [...options.iamActions], |
| 102 | Resource: [configuration.notificationConfigurationArn], |
| 103 | }, |
| 104 | ], |
| 105 | }); |
| 106 | } |
| 107 | return Effect.fn(`AWS.Notifications.${options.capability}(${configuration.LogicalId})`)( |
| 108 | function* (request?: Omit<I, "notificationConfigurationArn">) { |
| 109 | // Call-site region pin — see makeNotificationsHttpBinding above. |
| 110 | return yield* pinNotificationsRegion( |
| 111 | op({ |
| 112 | ...request, |
| 113 | notificationConfigurationArn: yield* configurationArn, |
| 114 | } as I), |
| 115 | ); |
| 116 | }, |
| 117 | ); |
| 118 | }); |
| 119 | }); |