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 type { PlatformApplication } from "./PlatformApplication.ts"; |
| 4 | import type { Subscription } from "./Subscription.ts"; |
| 5 | import type { Topic } from "./Topic.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for AWS SNS HTTP bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every near-identical `{Op}Http.ts` in this |
| 11 | * service is a thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of |
| 12 | * the builders below. Everything except the operation, the IAM action list, |
| 13 | * and the injected identifier is boilerplate. Genuinely-different bindings |
| 14 | * (the batched `TopicSink`) stay bespoke. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * Build the impl Effect for an account-level operation (`ListTopics`, |
| 19 | * `ListSubscriptions`, the SMS/sandbox APIs): the runtime callable passes |
| 20 | * the caller's request through unchanged and the deploy-time half grants |
| 21 | * `actions` on `*` (these SNS actions do not support resource-level |
| 22 | * permissions — only topic-scoped actions do). |
| 23 | */ |
| 24 | export const makeSnsAccountHttpBinding = <I, A, E, R>(options: { |
| 25 | /** Fully-qualified binding tag, e.g. `AWS.SNS.ListTopics`. */ |
| 26 | tag: string; |
| 27 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 28 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 29 | /** IAM actions granted on `*`. */ |
| 30 | actions: readonly string[]; |
| 31 | }) => |
| 32 | Effect.gen(function* () { |
| 33 | const bind = yield* AWSBindings.Bind; |
| 34 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 35 | |
| 36 | return Effect.fn(function* () { |
| 37 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 38 | yield* bind`${options.tag}()`({ |
| 39 | policyStatements: [ |
| 40 | { |
| 41 | Effect: "Allow", |
| 42 | Action: [...options.actions], |
| 43 | Resource: ["*"], |
| 44 | }, |
| 45 | ], |
| 46 | }); |
| 47 | } |
| 48 | return Effect.fn(options.tag)(function* (request?: I) { |
| 49 | return yield* op((request ?? {}) as I); |
| 50 | }); |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | /** |
| 55 | * Build the impl Effect for a topic-scoped operation: the runtime callable |
| 56 | * injects the bound {@link Topic}'s ARN under `key` (`TopicArn` for the |
| 57 | * pub/sub and attribute APIs, `ResourceArn` for the tagging and |
| 58 | * data-protection-policy APIs) and the deploy-time half grants `actions` on |
| 59 | * the topic's ARN. |
| 60 | */ |
| 61 | export const makeSnsTopicHttpBinding = < |
| 62 | K extends "TopicArn" | "ResourceArn", |
| 63 | I extends { [P in K]?: string }, |
| 64 | A, |
| 65 | E, |
| 66 | R, |
| 67 | >(options: { |
| 68 | /** Fully-qualified binding tag, e.g. `AWS.SNS.Publish`. */ |
| 69 | tag: string; |
| 70 | /** The distilled operation; the topic ARN is injected under `key`. */ |
| 71 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 72 | /** IAM actions granted on the topic ARN. */ |
| 73 | actions: readonly string[]; |
| 74 | /** The request field the topic ARN is injected under. */ |
| 75 | key: K; |
| 76 | }) => |
| 77 | Effect.gen(function* () { |
| 78 | const bind = yield* AWSBindings.Bind; |
| 79 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 80 | |
| 81 | return Effect.fn(function* (topic: Topic) { |
| 82 | const TopicArn = yield* topic.topicArn; |
| 83 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 84 | yield* bind`${options.tag}(${topic})`({ |
| 85 | policyStatements: [ |
| 86 | { |
| 87 | Effect: "Allow", |
| 88 | Action: [...options.actions], |
| 89 | Resource: [topic.topicArn], |
| 90 | }, |
| 91 | ], |
| 92 | }); |
| 93 | } |
| 94 | return Effect.fn(`${options.tag}(${topic.LogicalId})`)(function* (request?: Omit<I, K>) { |
| 95 | return yield* op({ |
| 96 | ...request, |
| 97 | [options.key]: yield* TopicArn, |
| 98 | } as I); |
| 99 | }); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | /** |
| 104 | * Build the impl Effect for a subscription-scoped operation: the runtime |
| 105 | * callable injects the bound {@link Subscription}'s identity under `key` |
| 106 | * (`SubscriptionArn` for the attribute APIs, `TopicArn` for |
| 107 | * `ConfirmSubscription`) and the deploy-time half grants `actions` on the |
| 108 | * subscription's parent topic ARN (SNS checks subscription actions against |
| 109 | * the topic resource). |
| 110 | */ |
| 111 | export const makeSnsSubscriptionHttpBinding = < |
| 112 | K extends "SubscriptionArn" | "TopicArn", |
| 113 | I extends { [P in K]?: string }, |
| 114 | A, |
| 115 | E, |
| 116 | R, |
| 117 | >(options: { |
| 118 | /** Fully-qualified binding tag, e.g. `AWS.SNS.GetSubscriptionAttributes`. */ |
| 119 | tag: string; |
| 120 | /** The distilled operation; the subscription identity is injected under `key`. */ |
| 121 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 122 | /** IAM actions granted on the subscription's topic ARN. */ |
| 123 | actions: readonly string[]; |
| 124 | /** The request field the subscription identity is injected under. */ |
| 125 | key: K; |
| 126 | }) => |
| 127 | Effect.gen(function* () { |
| 128 | const bind = yield* AWSBindings.Bind; |
| 129 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 130 | |
| 131 | return Effect.fn(function* (subscription: Subscription) { |
| 132 | const identity = yield* options.key === "TopicArn" |
| 133 | ? subscription.topicArn |
| 134 | : subscription.subscriptionArn; |
| 135 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 136 | yield* bind`${options.tag}(${subscription})`({ |
| 137 | policyStatements: [ |
| 138 | { |
| 139 | Effect: "Allow", |
| 140 | Action: [...options.actions], |
| 141 | Resource: [subscription.topicArn], |
| 142 | }, |
| 143 | ], |
| 144 | }); |
| 145 | } |
| 146 | return Effect.fn(`${options.tag}(${subscription.LogicalId})`)(function* ( |
| 147 | request?: Omit<I, K>, |
| 148 | ) { |
| 149 | return yield* op({ |
| 150 | ...request, |
| 151 | [options.key]: yield* identity, |
| 152 | } as I); |
| 153 | }); |
| 154 | }); |
| 155 | }); |
| 156 | |
| 157 | /** |
| 158 | * Build the impl Effect for a platform-application-scoped mobile-push |
| 159 | * operation whose request accepts `PlatformApplicationArn` |
| 160 | * (`CreatePlatformEndpoint`, `ListEndpointsByPlatformApplication`): the |
| 161 | * runtime callable injects the bound {@link PlatformApplication}'s ARN and |
| 162 | * the deploy-time half grants `actions` on `*` (SNS mobile-push actions do |
| 163 | * not support resource-level permissions). |
| 164 | */ |
| 165 | export const makeSnsPlatformHttpBinding = < |
| 166 | I extends { PlatformApplicationArn?: string }, |
| 167 | A, |
| 168 | E, |
| 169 | R, |
| 170 | >(options: { |
| 171 | /** Fully-qualified binding tag, e.g. `AWS.SNS.CreatePlatformEndpoint`. */ |
| 172 | tag: string; |
| 173 | /** The distilled operation; the application ARN is injected. */ |
| 174 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 175 | /** IAM actions granted on `*` (no resource-level support). */ |
| 176 | actions: readonly string[]; |
| 177 | }) => |
| 178 | Effect.gen(function* () { |
| 179 | const bind = yield* AWSBindings.Bind; |
| 180 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 181 | |
| 182 | return Effect.fn(function* (application: PlatformApplication) { |
| 183 | const PlatformApplicationArn = yield* application.platformApplicationArn; |
| 184 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 185 | yield* bind`${options.tag}(${application})`({ |
| 186 | policyStatements: [ |
| 187 | { |
| 188 | Effect: "Allow", |
| 189 | Action: [...options.actions], |
| 190 | Resource: ["*"], |
| 191 | }, |
| 192 | ], |
| 193 | }); |
| 194 | } |
| 195 | return Effect.fn(`${options.tag}(${application.LogicalId})`)(function* ( |
| 196 | request?: Omit<I, "PlatformApplicationArn">, |
| 197 | ) { |
| 198 | return yield* op({ |
| 199 | ...request, |
| 200 | PlatformApplicationArn: yield* PlatformApplicationArn, |
| 201 | } as I); |
| 202 | }); |
| 203 | }); |
| 204 | }); |
| 205 | |
| 206 | /** |
| 207 | * Build the impl Effect for an endpoint-scoped mobile-push operation |
| 208 | * (`GetEndpointAttributes`, `SetEndpointAttributes`, `DeleteEndpoint`, |
| 209 | * publish-to-endpoint): the caller's request already carries the endpoint |
| 210 | * identity (`EndpointArn`/`TargetArn`) and is passed through unchanged; the |
| 211 | * binding is still scoped to a {@link PlatformApplication} for IAM naming |
| 212 | * and grants `actions` on `*` (SNS mobile-push actions do not support |
| 213 | * resource-level permissions). |
| 214 | */ |
| 215 | export const makeSnsEndpointHttpBinding = <I, A, E, R>(options: { |
| 216 | /** Fully-qualified binding tag, e.g. `AWS.SNS.DeleteEndpoint`. */ |
| 217 | tag: string; |
| 218 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 219 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 220 | /** IAM actions granted on `*` (no resource-level support). */ |
| 221 | actions: readonly string[]; |
| 222 | }) => |
| 223 | Effect.gen(function* () { |
| 224 | const bind = yield* AWSBindings.Bind; |
| 225 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 226 | |
| 227 | return Effect.fn(function* (application: PlatformApplication) { |
| 228 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 229 | yield* bind`${options.tag}(${application})`({ |
| 230 | policyStatements: [ |
| 231 | { |
| 232 | Effect: "Allow", |
| 233 | Action: [...options.actions], |
| 234 | Resource: ["*"], |
| 235 | }, |
| 236 | ], |
| 237 | }); |
| 238 | } |
| 239 | return Effect.fn(`${options.tag}(${application.LogicalId})`)(function* (request: I) { |
| 240 | return yield* op(request); |
| 241 | }); |
| 242 | }); |
| 243 | }); |