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 { PolicyStatement } from "../IAM/Policy.ts"; |
| 4 | import type { Group } from "./Group.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for the AWS Resource Groups HTTP bindings. |
| 8 | * |
| 9 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 10 | * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders |
| 11 | * below. Everything except the operation, the IAM action list, and (for |
| 12 | * group-scoped operations) the injected group identifier is boilerplate. |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Build the impl Effect for a Resource Groups operation scoped to a |
| 17 | * {@link Group}: the deploy-time half grants `actions` on the bound group's |
| 18 | * ARN (plus any `supportingActions` on `*` — e.g. the Resource Groups |
| 19 | * Tagging API / CloudFormation read-through permissions that member |
| 20 | * enumeration fans out to), and the runtime half injects the group's name |
| 21 | * into every request as `Group`. |
| 22 | */ |
| 23 | export const makeResourceGroupsGroupHttpBinding = <I extends { Group?: string }, A, E, R>(options: { |
| 24 | /** Fully-qualified binding tag, e.g. `AWS.ResourceGroups.GroupResources`. */ |
| 25 | tag: string; |
| 26 | /** The distilled operation. */ |
| 27 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 28 | /** IAM actions granted on the group ARN. */ |
| 29 | actions: readonly string[]; |
| 30 | /** |
| 31 | * IAM actions granted on `*` that the operation fans out to — member |
| 32 | * resources are arbitrary ARNs unknowable at deploy time (e.g. |
| 33 | * `tag:GetResources` for `ListGroupResources`). |
| 34 | */ |
| 35 | supportingActions?: readonly string[]; |
| 36 | }) => |
| 37 | Effect.gen(function* () { |
| 38 | const bind = yield* AWSBindings.Bind; |
| 39 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 40 | |
| 41 | return Effect.fn(function* (group: Group) { |
| 42 | const GroupName = yield* group.groupName; |
| 43 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 44 | const supporting: PolicyStatement[] = |
| 45 | options.supportingActions !== undefined |
| 46 | ? [ |
| 47 | { |
| 48 | Effect: "Allow", |
| 49 | Action: [...options.supportingActions], |
| 50 | // Member resources are arbitrary ARNs chosen per request. |
| 51 | Resource: ["*"], |
| 52 | }, |
| 53 | ] |
| 54 | : []; |
| 55 | yield* bind`${options.tag}(${group})`({ |
| 56 | policyStatements: [ |
| 57 | { |
| 58 | Effect: "Allow", |
| 59 | Action: [...options.actions], |
| 60 | Resource: [group.groupArn], |
| 61 | }, |
| 62 | ...supporting, |
| 63 | ], |
| 64 | }); |
| 65 | } |
| 66 | return Effect.fn(`${options.tag}(${group.LogicalId})`)(function* ( |
| 67 | request?: Omit<I, "Group" | "GroupName">, |
| 68 | ) { |
| 69 | const groupName = yield* GroupName; |
| 70 | return yield* op({ ...request, Group: groupName } as I); |
| 71 | }); |
| 72 | }); |
| 73 | }); |
| 74 | |
| 75 | /** |
| 76 | * Build the impl Effect for an account-level Resource Groups operation |
| 77 | * (search, account settings, tag-sync task audit). The deploy-time half |
| 78 | * grants `actions` on `*` — the targets (queries, task ARNs) are chosen per |
| 79 | * request at runtime and unknowable at deploy time. |
| 80 | */ |
| 81 | export const makeResourceGroupsAccountHttpBinding = <I, A, E, R>(options: { |
| 82 | /** Fully-qualified binding tag, e.g. `AWS.ResourceGroups.SearchResources`. */ |
| 83 | tag: string; |
| 84 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 85 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 86 | /** IAM actions granted on `*`. */ |
| 87 | actions: readonly string[]; |
| 88 | }) => |
| 89 | Effect.gen(function* () { |
| 90 | const bind = yield* AWSBindings.Bind; |
| 91 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 92 | |
| 93 | return Effect.fn(function* () { |
| 94 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 95 | yield* bind`${options.tag}()`({ |
| 96 | policyStatements: [ |
| 97 | { |
| 98 | Effect: "Allow", |
| 99 | Action: [...options.actions], |
| 100 | Resource: ["*"], |
| 101 | }, |
| 102 | ], |
| 103 | }); |
| 104 | } |
| 105 | return Effect.fn(options.tag)(function* (request?: I) { |
| 106 | return yield* op((request ?? {}) as I); |
| 107 | }); |
| 108 | }); |
| 109 | }); |