The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import { AWSEnvironment } from "../Environment.ts"; |
| 5 | import type { Directory } from "./Directory.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for AWS Directory Service HTTP bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 11 | * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders |
| 12 | * below. Everything except the operation and the IAM action list is |
| 13 | * boilerplate. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * Build the impl Effect for an account-level Directory Service operation |
| 18 | * (enumerating directories, reading account limits). The deploy-time half |
| 19 | * grants `actions` on `*` — these actions do not support resource-level |
| 20 | * permissions. |
| 21 | */ |
| 22 | export const makeDirectoryServiceAccountHttpBinding = <I, A, E, R>(options: { |
| 23 | /** Fully-qualified binding tag, e.g. `AWS.DirectoryService.GetDirectoryLimits`. */ |
| 24 | tag: string; |
| 25 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 26 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 27 | /** IAM actions granted on `*`. */ |
| 28 | actions: readonly string[]; |
| 29 | }) => |
| 30 | Effect.gen(function* () { |
| 31 | const bind = yield* AWSBindings.Bind; |
| 32 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 33 | |
| 34 | return Effect.fn(function* () { |
| 35 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 36 | yield* bind`${options.tag}()`({ |
| 37 | policyStatements: [ |
| 38 | { |
| 39 | Effect: "Allow", |
| 40 | Action: [...options.actions], |
| 41 | Resource: ["*"], |
| 42 | }, |
| 43 | ], |
| 44 | }); |
| 45 | } |
| 46 | return Effect.fn(options.tag)(function* (request?: I) { |
| 47 | return yield* op((request ?? {}) as I); |
| 48 | }); |
| 49 | }); |
| 50 | }); |
| 51 | |
| 52 | /** |
| 53 | * Build the impl Effect for a Directory Service operation scoped to one |
| 54 | * {@link Directory}: the deploy-time half grants `actions` on the bound |
| 55 | * directory's ARN (`arn:aws:ds:{region}:{account}:directory/{id}`), and the |
| 56 | * runtime half injects the directory's `DirectoryId` into every request. |
| 57 | */ |
| 58 | export const makeDirectoryHttpBinding = <I extends { DirectoryId?: string }, A, E, R>(options: { |
| 59 | /** Fully-qualified binding tag, e.g. `AWS.DirectoryService.CreateSnapshot`. */ |
| 60 | tag: string; |
| 61 | /** The distilled operation; `DirectoryId` is injected from the directory. */ |
| 62 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 63 | /** IAM actions granted on the directory ARN. */ |
| 64 | actions: readonly string[]; |
| 65 | }) => |
| 66 | Effect.gen(function* () { |
| 67 | const bind = yield* AWSBindings.Bind; |
| 68 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 69 | |
| 70 | return Effect.fn(function* (directory: Directory) { |
| 71 | const DirectoryId = yield* directory.directoryId; |
| 72 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 73 | const { accountId, region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{ |
| 74 | accountId: string; |
| 75 | region: string; |
| 76 | }>; |
| 77 | yield* bind`${options.tag}(${directory})`({ |
| 78 | policyStatements: [ |
| 79 | { |
| 80 | Effect: "Allow", |
| 81 | Action: [...options.actions], |
| 82 | Resource: [ |
| 83 | Output.interpolate`arn:aws:ds:${region}:${accountId}:directory/${directory.directoryId}`, |
| 84 | ], |
| 85 | }, |
| 86 | ], |
| 87 | }); |
| 88 | } |
| 89 | return Effect.fn(`${options.tag}(${directory.LogicalId})`)(function* ( |
| 90 | request?: Omit<I, "DirectoryId">, |
| 91 | ) { |
| 92 | return yield* op({ |
| 93 | ...request, |
| 94 | DirectoryId: yield* DirectoryId, |
| 95 | } as I); |
| 96 | }); |
| 97 | }); |
| 98 | }); |