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 { Project } from "./Project.ts"; |
| 5 | import type { ReportGroup } from "./ReportGroup.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for AWS CodeBuild HTTP bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 11 | * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders |
| 12 | * below. Everything except the operation, the IAM action, and (for |
| 13 | * name-injecting operations) the injected `projectName`/`reportGroupArn` is |
| 14 | * boilerplate. |
| 15 | * |
| 16 | * CodeBuild authorizes builds, batch builds, sandboxes, and cache operations |
| 17 | * against the *project* ARN, and report operations against the |
| 18 | * *report-group* ARN — even when the request addresses a build/report id — |
| 19 | * so every builder grants on the bound resource's ARN. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Build the impl Effect for an operation whose input carries a |
| 24 | * `projectName` field: the runtime callable injects the bound |
| 25 | * {@link Project}'s name and the deploy-time half grants `actions` on the |
| 26 | * project ARN. |
| 27 | */ |
| 28 | export const makeCodeBuildProjectNameHttpBinding = < |
| 29 | I extends { projectName?: string }, |
| 30 | A, |
| 31 | E, |
| 32 | R, |
| 33 | >(options: { |
| 34 | /** Fully-qualified binding tag, e.g. `AWS.CodeBuild.StartBuildBatch`. */ |
| 35 | tag: string; |
| 36 | /** The distilled operation; `projectName` is injected from the project. */ |
| 37 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 38 | /** IAM actions granted on the project ARN. */ |
| 39 | actions: readonly string[]; |
| 40 | }) => |
| 41 | Effect.gen(function* () { |
| 42 | const bind = yield* AWSBindings.Bind; |
| 43 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 44 | |
| 45 | return Effect.fn(function* <P extends Project>(project: P) { |
| 46 | // Outputs yield a DEFERRED effect — resolve again per invocation below. |
| 47 | const ProjectName = yield* project.projectName; |
| 48 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 49 | yield* bind`${options.tag}(${project})`({ |
| 50 | policyStatements: [ |
| 51 | { |
| 52 | Effect: "Allow", |
| 53 | Action: [...options.actions], |
| 54 | Resource: [Output.interpolate`${project.projectArn}`], |
| 55 | }, |
| 56 | ], |
| 57 | }); |
| 58 | } |
| 59 | return Effect.fn(`${options.tag}(${project.LogicalId})`)(function* ( |
| 60 | request?: Omit<I, "projectName">, |
| 61 | ) { |
| 62 | return yield* op({ |
| 63 | ...request, |
| 64 | projectName: yield* ProjectName, |
| 65 | } as I); |
| 66 | }); |
| 67 | }); |
| 68 | }); |
| 69 | |
| 70 | /** |
| 71 | * Build the impl Effect for a project-anchored operation whose input |
| 72 | * addresses builds/batches/sandboxes by id: the request passes through |
| 73 | * as-is and the deploy-time half grants `actions` on the project ARN |
| 74 | * (CodeBuild authorizes build-/batch-addressed operations against the |
| 75 | * project the id belongs to). Sandbox-addressed actions authorize against |
| 76 | * the *sandbox* ARN (`…:sandbox/{project-name}:{uuid}`) — set |
| 77 | * `sandboxScoped` to additionally grant on the project's sandbox pattern. |
| 78 | */ |
| 79 | export const makeCodeBuildProjectHttpBinding = <I, A, E, R>(options: { |
| 80 | /** Fully-qualified binding tag, e.g. `AWS.CodeBuild.StopBuild`. */ |
| 81 | tag: string; |
| 82 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 83 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 84 | /** IAM actions granted on the project ARN. */ |
| 85 | actions: readonly string[]; |
| 86 | /** Also grant on `…:sandbox/{project-name}:*` (sandbox-addressed actions). */ |
| 87 | sandboxScoped?: boolean; |
| 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* <P extends Project>(project: P) { |
| 94 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 95 | yield* bind`${options.tag}(${project})`({ |
| 96 | policyStatements: [ |
| 97 | { |
| 98 | Effect: "Allow", |
| 99 | Action: [...options.actions], |
| 100 | Resource: [ |
| 101 | Output.interpolate`${project.projectArn}`, |
| 102 | ...(options.sandboxScoped |
| 103 | ? [ |
| 104 | Output.map( |
| 105 | project.projectArn, |
| 106 | (arn) => `${arn.replace(":project/", ":sandbox/")}:*`, |
| 107 | ), |
| 108 | ] |
| 109 | : []), |
| 110 | ], |
| 111 | }, |
| 112 | ], |
| 113 | }); |
| 114 | } |
| 115 | return Effect.fn(`${options.tag}(${project.LogicalId})`)(function* (request: I) { |
| 116 | return yield* op(request); |
| 117 | }); |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | /** |
| 122 | * Build the impl Effect for an operation whose input carries a |
| 123 | * `reportGroupArn` field: the runtime callable injects the bound |
| 124 | * {@link ReportGroup}'s ARN and the deploy-time half grants `actions` on |
| 125 | * the report-group ARN. |
| 126 | */ |
| 127 | export const makeCodeBuildReportGroupArnHttpBinding = < |
| 128 | I extends { reportGroupArn?: string }, |
| 129 | A, |
| 130 | E, |
| 131 | R, |
| 132 | >(options: { |
| 133 | /** Fully-qualified binding tag, e.g. `AWS.CodeBuild.GetReportGroupTrend`. */ |
| 134 | tag: string; |
| 135 | /** The distilled operation; `reportGroupArn` is injected from the group. */ |
| 136 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 137 | /** IAM actions granted on the report-group ARN. */ |
| 138 | actions: readonly string[]; |
| 139 | }) => |
| 140 | Effect.gen(function* () { |
| 141 | const bind = yield* AWSBindings.Bind; |
| 142 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 143 | |
| 144 | return Effect.fn(function* <G extends ReportGroup>(reportGroup: G) { |
| 145 | const ReportGroupArn = yield* reportGroup.reportGroupArn; |
| 146 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 147 | yield* bind`${options.tag}(${reportGroup})`({ |
| 148 | policyStatements: [ |
| 149 | { |
| 150 | Effect: "Allow", |
| 151 | Action: [...options.actions], |
| 152 | Resource: [Output.interpolate`${reportGroup.reportGroupArn}`], |
| 153 | }, |
| 154 | ], |
| 155 | }); |
| 156 | } |
| 157 | return Effect.fn(`${options.tag}(${reportGroup.LogicalId})`)(function* ( |
| 158 | request?: Omit<I, "reportGroupArn">, |
| 159 | ) { |
| 160 | return yield* op({ |
| 161 | ...request, |
| 162 | reportGroupArn: yield* ReportGroupArn, |
| 163 | } as I); |
| 164 | }); |
| 165 | }); |
| 166 | }); |
| 167 | |
| 168 | /** |
| 169 | * Build the impl Effect for a report-group-anchored operation whose input |
| 170 | * addresses reports by ARN: the request passes through as-is and the |
| 171 | * deploy-time half grants `actions` on the report-group ARN (CodeBuild |
| 172 | * authorizes report operations against the report group the report belongs |
| 173 | * to). |
| 174 | */ |
| 175 | export const makeCodeBuildReportGroupHttpBinding = <I, A, E, R>(options: { |
| 176 | /** Fully-qualified binding tag, e.g. `AWS.CodeBuild.DescribeTestCases`. */ |
| 177 | tag: string; |
| 178 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 179 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 180 | /** IAM actions granted on the report-group ARN. */ |
| 181 | actions: readonly string[]; |
| 182 | }) => |
| 183 | Effect.gen(function* () { |
| 184 | const bind = yield* AWSBindings.Bind; |
| 185 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 186 | |
| 187 | return Effect.fn(function* <G extends ReportGroup>(reportGroup: G) { |
| 188 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 189 | yield* bind`${options.tag}(${reportGroup})`({ |
| 190 | policyStatements: [ |
| 191 | { |
| 192 | Effect: "Allow", |
| 193 | Action: [...options.actions], |
| 194 | Resource: [Output.interpolate`${reportGroup.reportGroupArn}`], |
| 195 | }, |
| 196 | ], |
| 197 | }); |
| 198 | } |
| 199 | return Effect.fn(`${options.tag}(${reportGroup.LogicalId})`)(function* (request: I) { |
| 200 | return yield* op(request); |
| 201 | }); |
| 202 | }); |
| 203 | }); |