| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Binding from "../../Binding.ts"; |
| 3 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 4 | import type { Rule } from "./Rule.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for the Recycle Bin (rbin) 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 and the IAM action list is |
| 12 | * boilerplate. |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Build the impl Effect for a Recycle Bin operation scoped to a retention |
| 17 | * {@link Rule}: the deploy-time half grants `actions` on the bound rule's |
| 18 | * ARN, and the runtime half injects the rule's `Identifier` into every |
| 19 | * request. |
| 20 | */ |
| 21 | export const makeRbinRuleHttpBinding = < |
| 22 | I extends { Identifier: string }, |
| 23 | A, |
| 24 | E, |
| 25 | R, |
| 26 | >(options: { |
| 27 | /** Fully-qualified binding tag, e.g. `AWS.Rbin.GetRule`. */ |
| 28 | tag: string; |
| 29 | /** The distilled operation. */ |
| 30 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 31 | /** IAM actions granted on the retention rule ARN. */ |
| 32 | actions: readonly string[]; |
| 33 | }) => |
| 34 | Effect.gen(function* () { |
| 35 | const op = yield* options.operation; |
| 36 | |
| 37 | return Effect.fn(function* (rule: Rule) { |
| 38 | const Identifier = yield* rule.identifier; |
| 39 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 40 | const host = yield* Binding.Host; |
| 41 | if (isBindingHost(host)) { |
| 42 | yield* host.bind`Allow(${host}, ${options.tag}(${rule}))`({ |
| 43 | policyStatements: [ |
| 44 | { |
| 45 | Effect: "Allow", |
| 46 | Action: [...options.actions], |
| 47 | Resource: [rule.ruleArn], |
| 48 | }, |
| 49 | ], |
| 50 | }); |
| 51 | } |
| 52 | } |
| 53 | return Effect.fn(`${options.tag}(${rule.LogicalId})`)(function* ( |
| 54 | request?: Omit<I, "Identifier">, |
| 55 | ) { |
| 56 | const identifier = yield* Identifier; |
| 57 | return yield* op({ ...request, Identifier: identifier } as I); |
| 58 | }); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | /** |
| 63 | * Build the impl Effect for an account-level Recycle Bin operation |
| 64 | * (enumerating the Region's retention rules). The deploy-time half grants |
| 65 | * `actions` on `*` — `rbin:ListRules` is a list action that is not scoped to |
| 66 | * a single rule resource. |
| 67 | */ |
| 68 | export const makeRbinAccountHttpBinding = <I, A, E, R>(options: { |
| 69 | /** Fully-qualified binding tag, e.g. `AWS.Rbin.ListRules`. */ |
| 70 | tag: string; |
| 71 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 72 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 73 | /** IAM actions granted on `*`. */ |
| 74 | actions: readonly string[]; |
| 75 | }) => |
| 76 | Effect.gen(function* () { |
| 77 | const op = yield* options.operation; |
| 78 | |
| 79 | return Effect.fn(function* () { |
| 80 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 81 | const host = yield* Binding.Host; |
| 82 | if (isBindingHost(host)) { |
| 83 | yield* host.bind`Allow(${host}, ${options.tag}())`({ |
| 84 | policyStatements: [ |
| 85 | { |
| 86 | Effect: "Allow", |
| 87 | Action: [...options.actions], |
| 88 | Resource: ["*"], |
| 89 | }, |
| 90 | ], |
| 91 | }); |
| 92 | } |
| 93 | } |
| 94 | return Effect.fn(options.tag)(function* (request: I) { |
| 95 | return yield* op(request); |
| 96 | }); |
| 97 | }); |
| 98 | }); |