@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, 06:00 UTC

main
ts 154 lines 5.55 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { ComponentVersion } from "./ComponentVersion.ts";
4 import type { Deployment } from "./Deployment.ts";
5
6 /**
7 * Shared scaffolding for IoT Greengrass V2 HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
10 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the three
11 * builders below. Everything except the operation and the IAM action list is
12 * boilerplate.
13 */
14
15 /**
16 * Build the impl Effect for a Greengrass V2 operation scoped to a
17 * {@link ComponentVersion}: the deploy-time half grants `actions` on the
18 * bound component version's ARN, and the runtime half injects the component
19 * version's `arn` into every request.
20 */
21 export const makeGreengrassComponentHttpBinding = <I extends { arn: string }, A, E, R>(options: {
22 /** Fully-qualified binding tag, e.g. `AWS.GreengrassV2.GetComponent`. */
23 tag: string;
24 /** The distilled operation. */
25 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
26 /** IAM actions granted on the component version ARN. */
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* (component: ComponentVersion) {
34 const Arn = yield* component.arn;
35 if (!globalThis.__ALCHEMY_RUNTIME__) {
36 yield* bind`${options.tag}(${component})`({
37 policyStatements: [
38 {
39 Effect: "Allow",
40 Action: [...options.actions],
41 Resource: [component.arn],
42 },
43 ],
44 });
45 }
46 return Effect.fn(`${options.tag}(${component.LogicalId})`)(function* (
47 request?: Omit<I, "arn">,
48 ) {
49 const arn = yield* Arn;
50 return yield* op({ ...request, arn } as I);
51 });
52 });
53 });
54
55 /**
56 * Build the impl Effect for a Greengrass V2 operation scoped to a
57 * {@link Deployment}: the deploy-time half grants `actions` on the bound
58 * deployment's ARN, and the runtime half injects the `deploymentId` into
59 * every request.
60 */
61 export const makeGreengrassDeploymentHttpBinding = <
62 I extends { deploymentId: string },
63 A,
64 E,
65 R,
66 >(options: {
67 /** Fully-qualified binding tag, e.g. `AWS.GreengrassV2.GetDeployment`. */
68 tag: string;
69 /** The distilled operation. */
70 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
71 /** IAM actions granted on the deployment ARN. */
72 actions: readonly string[];
73 /**
74 * Dependent IAM actions granted on `*`. Greengrass V2 deployments are
75 * implemented on IoT Jobs and resolve their thing/thing-group targets with
76 * the caller's credentials, so deployment-plane operations require
77 * `iot:DescribeJob`/`iot:DescribeThing`-family permissions (see the
78 * "dependent actions" column of the service authorization reference).
79 * These cannot be scoped to the deployment ARN — the backing job and
80 * target thing have their own ARNs.
81 */
82 dependentActions?: readonly string[];
83 }) =>
84 Effect.gen(function* () {
85 const bind = yield* AWSBindings.Bind;
86 const op = yield* AWSBindings.withCredentials(options.operation);
87
88 return Effect.fn(function* (deployment: Deployment) {
89 const DeploymentId = yield* deployment.deploymentId;
90 if (!globalThis.__ALCHEMY_RUNTIME__) {
91 yield* bind`${options.tag}(${deployment})`({
92 policyStatements: [
93 {
94 Effect: "Allow",
95 Action: [...options.actions],
96 Resource: [deployment.deploymentArn],
97 },
98 ...(options.dependentActions !== undefined && options.dependentActions.length > 0
99 ? [
100 {
101 Effect: "Allow" as const,
102 Action: [...options.dependentActions],
103 Resource: ["*"],
104 },
105 ]
106 : []),
107 ],
108 });
109 }
110 return Effect.fn(`${options.tag}(${deployment.LogicalId})`)(function* (
111 request?: Omit<I, "deploymentId">,
112 ) {
113 const deploymentId = yield* DeploymentId;
114 return yield* op({ ...request, deploymentId } as I);
115 });
116 });
117 });
118
119 /**
120 * Build the impl Effect for an account-level Greengrass V2 operation (listing
121 * components/deployments/core devices, core-device data-plane calls keyed by
122 * a thing name the caller supplies at runtime). The deploy-time half grants
123 * `actions` on `*` — core devices register themselves and are not modeled as
124 * Alchemy resources, so there is no resource ARN to scope down to.
125 */
126 export const makeGreengrassAccountHttpBinding = <I, A, E, R>(options: {
127 /** Fully-qualified binding tag, e.g. `AWS.GreengrassV2.ListComponents`. */
128 tag: string;
129 /** The distilled operation, invoked with the caller's request as-is. */
130 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
131 /** IAM actions granted on `*`. */
132 actions: readonly string[];
133 }) =>
134 Effect.gen(function* () {
135 const bind = yield* AWSBindings.Bind;
136 const op = yield* AWSBindings.withCredentials(options.operation);
137
138 return Effect.fn(function* () {
139 if (!globalThis.__ALCHEMY_RUNTIME__) {
140 yield* bind`${options.tag}()`({
141 policyStatements: [
142 {
143 Effect: "Allow",
144 Action: [...options.actions],
145 Resource: ["*"],
146 },
147 ],
148 });
149 }
150 return Effect.fn(options.tag)(function* (request?: I) {
151 return yield* op((request ?? {}) as I);
152 });
153 });
154 });