The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { Cluster } from "./Cluster.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for AWS DocDBElastic HTTP 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 and the IAM action list is |
| 12 | * boilerplate. |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Elastic-cluster snapshot ARNs embed a server-generated UUID that is only |
| 17 | * known at runtime, so snapshot-scoped grants use this wildcard. |
| 18 | */ |
| 19 | export const SNAPSHOT_ARN_WILDCARD = "arn:aws:docdb-elastic:*:*:cluster-snapshot/*"; |
| 20 | |
| 21 | /** |
| 22 | * Build the impl Effect for an account-level operation (snapshot management, |
| 23 | * restore, pending maintenance). The deploy-time half grants `actions` on |
| 24 | * `resources` (default `*`) — these operations address snapshots and |
| 25 | * clusters by ARNs that are runtime data. |
| 26 | */ |
| 27 | export const makeDocDBElasticAccountHttpBinding = <I, A, E, R>(options: { |
| 28 | /** Fully-qualified binding tag, e.g. `AWS.DocDBElastic.GetClusterSnapshot`. */ |
| 29 | tag: string; |
| 30 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 31 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 32 | /** IAM actions granted. */ |
| 33 | actions: readonly string[]; |
| 34 | /** |
| 35 | * IAM resources the actions are granted on. |
| 36 | * @default ["*"] |
| 37 | */ |
| 38 | resources?: readonly string[]; |
| 39 | }) => |
| 40 | Effect.gen(function* () { |
| 41 | const bind = yield* AWSBindings.Bind; |
| 42 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 43 | |
| 44 | return Effect.fn(function* () { |
| 45 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 46 | yield* bind`${options.tag}()`({ |
| 47 | policyStatements: [ |
| 48 | { |
| 49 | Effect: "Allow", |
| 50 | Action: [...options.actions], |
| 51 | Resource: [...(options.resources ?? ["*"])], |
| 52 | }, |
| 53 | ], |
| 54 | }); |
| 55 | } |
| 56 | return Effect.fn(options.tag)(function* (request?: I) { |
| 57 | return yield* op((request ?? {}) as I); |
| 58 | }); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | /** |
| 63 | * Build the impl Effect for a cluster-scoped operation: the runtime callable |
| 64 | * injects the bound {@link Cluster}'s ARN as `clusterArn` and the deploy-time |
| 65 | * half grants `actions` on the cluster ARN (plus any `extraResources`). |
| 66 | */ |
| 67 | export const makeDocDBElasticClusterHttpBinding = < |
| 68 | I extends { clusterArn: string }, |
| 69 | A, |
| 70 | E, |
| 71 | R, |
| 72 | >(options: { |
| 73 | /** Fully-qualified binding tag, e.g. `AWS.DocDBElastic.StopCluster`. */ |
| 74 | tag: string; |
| 75 | /** The distilled operation; `clusterArn` is injected from the cluster. */ |
| 76 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 77 | /** IAM actions granted on the cluster ARN. */ |
| 78 | actions: readonly string[]; |
| 79 | /** Static IAM resources granted in addition to the cluster ARN. */ |
| 80 | extraResources?: readonly string[]; |
| 81 | }) => |
| 82 | Effect.gen(function* () { |
| 83 | const bind = yield* AWSBindings.Bind; |
| 84 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 85 | |
| 86 | return Effect.fn(function* (cluster: Cluster) { |
| 87 | const clusterArn = yield* cluster.clusterArn; |
| 88 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 89 | yield* bind`${options.tag}(${cluster})`({ |
| 90 | policyStatements: [ |
| 91 | { |
| 92 | Effect: "Allow", |
| 93 | Action: [...options.actions], |
| 94 | Resource: [ |
| 95 | Output.interpolate`${cluster.clusterArn}`, |
| 96 | ...(options.extraResources ?? []), |
| 97 | ], |
| 98 | }, |
| 99 | ], |
| 100 | }); |
| 101 | } |
| 102 | return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* ( |
| 103 | request?: Omit<I, "clusterArn">, |
| 104 | ) { |
| 105 | return yield* op({ |
| 106 | ...request, |
| 107 | clusterArn: yield* clusterArn, |
| 108 | } as I); |
| 109 | }); |
| 110 | }); |
| 111 | }); |