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 type { Application } from "./Application.ts"; |
| 5 | import type { DeploymentGroup } from "./DeploymentGroup.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for AWS CodeDeploy 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 `applicationName` / |
| 14 | * `deploymentGroupName` is boilerplate. |
| 15 | * |
| 16 | * CodeDeploy authorizes deployment-addressed operations (get/stop/continue a |
| 17 | * deployment, lifecycle-hook results, deployment targets) against the |
| 18 | * *deployment group* the deployment belongs to, and revision operations |
| 19 | * against the *application* ARN — so every builder grants on the bound |
| 20 | * resource's ARN. |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * Build the impl Effect for an operation whose input carries |
| 25 | * `applicationName` + `deploymentGroupName` fields: the runtime callable |
| 26 | * injects both from the bound {@link DeploymentGroup} and the deploy-time |
| 27 | * half grants `actions` on the deployment-group ARN. |
| 28 | */ |
| 29 | export const makeCodeDeployGroupNameHttpBinding = < |
| 30 | I extends { applicationName?: string; deploymentGroupName?: string }, |
| 31 | A, |
| 32 | E, |
| 33 | R, |
| 34 | >(options: { |
| 35 | /** Fully-qualified binding tag, e.g. `AWS.CodeDeploy.ListDeployments`. */ |
| 36 | tag: string; |
| 37 | /** |
| 38 | * The distilled operation; `applicationName` and `deploymentGroupName` |
| 39 | * are injected from the group. |
| 40 | */ |
| 41 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 42 | /** IAM actions granted on the deployment-group ARN. */ |
| 43 | actions: readonly string[]; |
| 44 | }) => |
| 45 | Effect.gen(function* () { |
| 46 | const bind = yield* AWSBindings.Bind; |
| 47 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 48 | |
| 49 | return Effect.fn(function* <G extends DeploymentGroup>(group: G) { |
| 50 | // Outputs yield a DEFERRED effect — resolve again per invocation below. |
| 51 | const ApplicationName = yield* group.applicationName; |
| 52 | const DeploymentGroupName = yield* group.deploymentGroupName; |
| 53 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 54 | yield* bind`${options.tag}(${group})`({ |
| 55 | policyStatements: [ |
| 56 | { |
| 57 | Effect: "Allow", |
| 58 | Action: [...options.actions], |
| 59 | Resource: [Output.interpolate`${group.deploymentGroupArn}`], |
| 60 | }, |
| 61 | ], |
| 62 | }); |
| 63 | } |
| 64 | return Effect.fn(`${options.tag}(${group.LogicalId})`)(function* ( |
| 65 | request?: Omit<I, "applicationName" | "deploymentGroupName">, |
| 66 | ) { |
| 67 | return yield* op({ |
| 68 | ...request, |
| 69 | applicationName: yield* ApplicationName, |
| 70 | deploymentGroupName: yield* DeploymentGroupName, |
| 71 | } as I); |
| 72 | }); |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | /** |
| 77 | * Build the impl Effect for a group-anchored operation whose input |
| 78 | * addresses deployments by id (get/stop/continue, lifecycle-hook results, |
| 79 | * deployment targets): the request passes through as-is and the deploy-time |
| 80 | * half grants `actions` on the deployment-group ARN (CodeDeploy authorizes |
| 81 | * deployment-addressed operations against the group the deployment belongs |
| 82 | * to). |
| 83 | */ |
| 84 | export const makeCodeDeployGroupHttpBinding = <I, A, E, R>(options: { |
| 85 | /** Fully-qualified binding tag, e.g. `AWS.CodeDeploy.GetDeployment`. */ |
| 86 | tag: string; |
| 87 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 88 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 89 | /** IAM actions granted on the deployment-group ARN. */ |
| 90 | actions: readonly string[]; |
| 91 | }) => |
| 92 | Effect.gen(function* () { |
| 93 | const bind = yield* AWSBindings.Bind; |
| 94 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 95 | |
| 96 | return Effect.fn(function* <G extends DeploymentGroup>(group: G) { |
| 97 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 98 | yield* bind`${options.tag}(${group})`({ |
| 99 | policyStatements: [ |
| 100 | { |
| 101 | Effect: "Allow", |
| 102 | Action: [...options.actions], |
| 103 | Resource: [Output.interpolate`${group.deploymentGroupArn}`], |
| 104 | }, |
| 105 | ], |
| 106 | }); |
| 107 | } |
| 108 | return Effect.fn(`${options.tag}(${group.LogicalId})`)(function* (request: I) { |
| 109 | return yield* op(request); |
| 110 | }); |
| 111 | }); |
| 112 | }); |
| 113 | |
| 114 | /** |
| 115 | * Build the impl Effect for an operation whose input carries an |
| 116 | * `applicationName` field (revision management): the runtime callable |
| 117 | * injects the bound {@link Application}'s name and the deploy-time half |
| 118 | * grants `actions` on the application ARN. |
| 119 | */ |
| 120 | export const makeCodeDeployApplicationHttpBinding = < |
| 121 | I extends { applicationName?: string }, |
| 122 | A, |
| 123 | E, |
| 124 | R, |
| 125 | >(options: { |
| 126 | /** Fully-qualified binding tag, e.g. `AWS.CodeDeploy.GetApplicationRevision`. */ |
| 127 | tag: string; |
| 128 | /** The distilled operation; `applicationName` is injected from the app. */ |
| 129 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 130 | /** IAM actions granted on the application ARN. */ |
| 131 | actions: readonly string[]; |
| 132 | }) => |
| 133 | Effect.gen(function* () { |
| 134 | const bind = yield* AWSBindings.Bind; |
| 135 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 136 | |
| 137 | return Effect.fn(function* <P extends Application>(application: P) { |
| 138 | const ApplicationName = yield* application.applicationName; |
| 139 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 140 | yield* bind`${options.tag}(${application})`({ |
| 141 | policyStatements: [ |
| 142 | { |
| 143 | Effect: "Allow", |
| 144 | Action: [...options.actions], |
| 145 | Resource: [Output.interpolate`${application.applicationArn}`], |
| 146 | }, |
| 147 | ], |
| 148 | }); |
| 149 | } |
| 150 | return Effect.fn(`${options.tag}(${application.LogicalId})`)(function* ( |
| 151 | request?: Omit<I, "applicationName">, |
| 152 | ) { |
| 153 | return yield* op({ |
| 154 | ...request, |
| 155 | applicationName: yield* ApplicationName, |
| 156 | } as I); |
| 157 | }); |
| 158 | }); |
| 159 | }); |