@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 142 lines 4.82 KB
Raw
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 { AttributeGroup } from "./AttributeGroup.ts";
6
7 /**
8 * Shared scaffolding for AppRegistry 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 three
12 * builders below. Everything except the operation, the IAM action list, and
13 * the injected identifier is boilerplate.
14 *
15 * AppRegistry IAM actions live under the `servicecatalog:` service prefix.
16 */
17
18 /**
19 * Build the impl Effect for an application-scoped operation: the runtime
20 * callable injects the bound {@link Application}'s ID as `application` and
21 * the deploy-time half grants `actions` on the application ARN.
22 */
23 export const makeApplicationScopedHttpBinding = <
24 I extends { application: string },
25 A,
26 E,
27 R,
28 >(options: {
29 /** Fully-qualified binding tag, e.g. `AWS.AppRegistry.GetApplication`. */
30 tag: string;
31 /** The distilled operation; `application` is injected from the resource. */
32 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
33 /** IAM actions granted on the application ARN. */
34 actions: readonly string[];
35 }) =>
36 Effect.gen(function* () {
37 const bind = yield* AWSBindings.Bind;
38 const op = yield* AWSBindings.withCredentials(options.operation);
39
40 return Effect.fn(function* (application: Application) {
41 const ApplicationId = yield* application.applicationId;
42 if (!globalThis.__ALCHEMY_RUNTIME__) {
43 yield* bind`${options.tag}(${application})`({
44 policyStatements: [
45 {
46 Effect: "Allow",
47 Action: [...options.actions],
48 Resource: [Output.interpolate`${application.applicationArn}`],
49 },
50 ],
51 });
52 }
53 return Effect.fn(`${options.tag}(${application.LogicalId})`)(function* (
54 request?: Omit<I, "application">,
55 ) {
56 return yield* op({
57 ...request,
58 application: yield* ApplicationId,
59 } as I);
60 });
61 });
62 });
63
64 /**
65 * Build the impl Effect for an attribute-group-scoped operation: the runtime
66 * callable injects the bound {@link AttributeGroup}'s ID as `attributeGroup`
67 * and the deploy-time half grants `actions` on the attribute group ARN.
68 */
69 export const makeAttributeGroupScopedHttpBinding = <
70 I extends { attributeGroup: string },
71 A,
72 E,
73 R,
74 >(options: {
75 /** Fully-qualified binding tag, e.g. `AWS.AppRegistry.GetAttributeGroup`. */
76 tag: string;
77 /** The distilled operation; `attributeGroup` is injected from the resource. */
78 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
79 /** IAM actions granted on the attribute group ARN. */
80 actions: readonly string[];
81 }) =>
82 Effect.gen(function* () {
83 const bind = yield* AWSBindings.Bind;
84 const op = yield* AWSBindings.withCredentials(options.operation);
85
86 return Effect.fn(function* (attributeGroup: AttributeGroup) {
87 const AttributeGroupId = yield* attributeGroup.attributeGroupId;
88 if (!globalThis.__ALCHEMY_RUNTIME__) {
89 yield* bind`${options.tag}(${attributeGroup})`({
90 policyStatements: [
91 {
92 Effect: "Allow",
93 Action: [...options.actions],
94 Resource: [Output.interpolate`${attributeGroup.attributeGroupArn}`],
95 },
96 ],
97 });
98 }
99 return Effect.fn(`${options.tag}(${attributeGroup.LogicalId})`)(function* (
100 request?: Omit<I, "attributeGroup">,
101 ) {
102 return yield* op({
103 ...request,
104 attributeGroup: yield* AttributeGroupId,
105 } as I);
106 });
107 });
108 });
109
110 /**
111 * Build the impl Effect for an account-level operation (no target resource).
112 * The deploy-time half grants `actions` on `*`.
113 */
114 export const makeAppRegistryAccountHttpBinding = <I, A, E, R>(options: {
115 /** Fully-qualified binding tag, e.g. `AWS.AppRegistry.SyncResource`. */
116 tag: string;
117 /** The distilled operation, invoked with the caller's request as-is. */
118 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
119 /** IAM actions granted on `*`. */
120 actions: readonly string[];
121 }) =>
122 Effect.gen(function* () {
123 const bind = yield* AWSBindings.Bind;
124 const op = yield* AWSBindings.withCredentials(options.operation);
125
126 return Effect.fn(function* () {
127 if (!globalThis.__ALCHEMY_RUNTIME__) {
128 yield* bind`${options.tag}()`({
129 policyStatements: [
130 {
131 Effect: "Allow",
132 Action: [...options.actions],
133 Resource: ["*"],
134 },
135 ],
136 });
137 }
138 return Effect.fn(options.tag)(function* (request?: I) {
139 return yield* op((request ?? {}) as I);
140 });
141 });
142 });