@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 50 lines 2.03 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { CloudControlBindingOptions } from "./BindingOptions.ts";
4
5 /**
6 * Shared scaffolding for Cloud Control HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
9 * thin `Layer.effect(Cap, makeCloudControlHttpBinding({ … }))` over the
10 * builder below. Everything except the operation and the IAM action is
11 * boilerplate.
12 *
13 * All Cloud Control bindings are account-level: the `cloudformation:*Resource`
14 * actions do not support resource-level scoping (the target is an arbitrary
15 * CloudFormation type name), so the grant is on `*`. Because Cloud Control
16 * invokes the resource type's handlers with the caller's credentials, callers
17 * of handler-invoking operations may pass extra
18 * {@link CloudControlBindingOptions.handlerPolicyStatements} that are attached
19 * to the host alongside the Cloud Control grant.
20 */
21 export const makeCloudControlHttpBinding = <I, A, E, R>(options: {
22 /** Fully-qualified binding tag, e.g. `AWS.CloudControl.GetResource`. */
23 tag: string;
24 /** The distilled operation, invoked with the caller's request as-is. */
25 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
26 /** IAM actions granted on `*` (Cloud Control has no resource-level scoping). */
27 actions: readonly string[];
28 }) =>
29 Effect.gen(function* () {
30 const bind = yield* AWSBindings.Bind;
31 const op = yield* AWSBindings.withCredentials(options.operation);
32
33 return Effect.fn(function* (bindOptions?: CloudControlBindingOptions) {
34 if (!globalThis.__ALCHEMY_RUNTIME__) {
35 yield* bind`${options.tag}()`({
36 policyStatements: [
37 {
38 Effect: "Allow",
39 Action: [...options.actions],
40 Resource: ["*"],
41 },
42 ...(bindOptions?.handlerPolicyStatements ?? []),
43 ],
44 });
45 }
46 return Effect.fn(options.tag)(function* (request?: I) {
47 return yield* op((request ?? {}) as I);
48 });
49 });
50 });