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 { Workspace } from "./Workspace.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for AWS Managed Grafana 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 | * Build the impl Effect for an account-level operation (e.g. listing the |
| 17 | * available Grafana versions). The deploy-time half grants `actions` on `*`. |
| 18 | */ |
| 19 | export const makeGrafanaAccountHttpBinding = <I, A, E, R>(options: { |
| 20 | /** Fully-qualified binding tag, e.g. `AWS.Grafana.ListVersions`. */ |
| 21 | tag: string; |
| 22 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 23 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 24 | /** IAM actions granted on `*`. */ |
| 25 | actions: readonly string[]; |
| 26 | }) => |
| 27 | Effect.gen(function* () { |
| 28 | const bind = yield* AWSBindings.Bind; |
| 29 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 30 | |
| 31 | return Effect.fn(function* () { |
| 32 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 33 | yield* bind`${options.tag}()`({ |
| 34 | policyStatements: [ |
| 35 | { |
| 36 | Effect: "Allow", |
| 37 | Action: [...options.actions], |
| 38 | Resource: ["*"], |
| 39 | }, |
| 40 | ], |
| 41 | }); |
| 42 | } |
| 43 | return Effect.fn(options.tag)(function* (request?: I) { |
| 44 | return yield* op((request ?? {}) as I); |
| 45 | }); |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | /** |
| 50 | * Build the impl Effect for a workspace-scoped operation: the runtime |
| 51 | * callable injects the bound {@link Workspace}'s id as `workspaceId` and the |
| 52 | * deploy-time half grants `actions` on the workspace ARN. |
| 53 | */ |
| 54 | export const makeGrafanaWorkspaceHttpBinding = < |
| 55 | I extends { workspaceId: string }, |
| 56 | A, |
| 57 | E, |
| 58 | R, |
| 59 | >(options: { |
| 60 | /** Fully-qualified binding tag, e.g. `AWS.Grafana.ListPermissions`. */ |
| 61 | tag: string; |
| 62 | /** The distilled operation; `workspaceId` is injected from the workspace. */ |
| 63 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 64 | /** IAM actions granted on the workspace ARN. */ |
| 65 | actions: readonly string[]; |
| 66 | }) => |
| 67 | Effect.gen(function* () { |
| 68 | const bind = yield* AWSBindings.Bind; |
| 69 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 70 | |
| 71 | return Effect.fn(function* (workspace: Workspace) { |
| 72 | const workspaceId = yield* workspace.workspaceId; |
| 73 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 74 | yield* bind`${options.tag}(${workspace})`({ |
| 75 | policyStatements: [ |
| 76 | { |
| 77 | Effect: "Allow", |
| 78 | Action: [...options.actions], |
| 79 | Resource: [Output.interpolate`${workspace.workspaceArn}`], |
| 80 | }, |
| 81 | ], |
| 82 | }); |
| 83 | } |
| 84 | return Effect.fn(`${options.tag}(${workspace.LogicalId})`)(function* ( |
| 85 | request?: Omit<I, "workspaceId">, |
| 86 | ) { |
| 87 | return yield* op({ |
| 88 | ...request, |
| 89 | workspaceId: yield* workspaceId, |
| 90 | } as I); |
| 91 | }); |
| 92 | }); |
| 93 | }); |