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
| 1 | /** |
| 2 | * Shared HTTP-binding scaffolding for the AppIntegrations service. |
| 3 | * NOT exported from the service barrel — each `{Op}Http.ts` is a thin |
| 4 | * one-call `Layer.effect(Cap, makeAppIntegrations*HttpBinding({ ... }))`. |
| 5 | */ |
| 6 | import type { Input } from "alchemy/Input"; |
| 7 | import type { Output } from "alchemy/Output"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as AWSBindings from "../AWSBindings.ts"; |
| 10 | import { AWSEnvironment } from "../Environment.ts"; |
| 11 | |
| 12 | /** Deploy-time environment handed to the `resources` IAM builder. */ |
| 13 | export interface AppIntegrationsIamEnv { |
| 14 | region: string; |
| 15 | accountId: string; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Build the implementation effect for a resource-scoped AppIntegrations HTTP |
| 20 | * binding: resolve the resource identifier at init, grant the IAM actions on |
| 21 | * the host at deploy time, and inject the identifier into every request at |
| 22 | * runtime under `requestKey`. |
| 23 | */ |
| 24 | export const makeAppIntegrationsHttpBinding = < |
| 25 | Res extends { LogicalId: string }, |
| 26 | IdKey extends string, |
| 27 | WireReq extends { [K in IdKey]?: unknown }, |
| 28 | Out, |
| 29 | Err, |
| 30 | OpR, |
| 31 | >(options: { |
| 32 | /** Capability name used in the bind sid and runtime span. */ |
| 33 | name: string; |
| 34 | /** The distilled AppIntegrations operation backing the binding. */ |
| 35 | operation: Effect.Effect<(input: WireReq) => Effect.Effect<Out, Err>, never, OpR>; |
| 36 | /** Wire key the resolved resource identifier is injected under. */ |
| 37 | requestKey: IdKey; |
| 38 | /** The resource attribute used as the wire identifier. */ |
| 39 | identifier: (resource: Res) => Output<string, never>; |
| 40 | /** IAM actions granted on the host at deploy time. */ |
| 41 | iamActions: string[]; |
| 42 | /** IAM resources the actions are granted on. */ |
| 43 | resources: (resource: Res, env: AppIntegrationsIamEnv) => Input<string>[]; |
| 44 | /** |
| 45 | * Dependent actions the AppIntegrations service invokes on the caller's |
| 46 | * behalf (e.g. `appflow:CreateFlow` for data-integration associations). |
| 47 | * Granted on `*` in a second statement — these AWS-managed side resources |
| 48 | * have no stable ARN at deploy time. |
| 49 | */ |
| 50 | dependentActions?: string[]; |
| 51 | }) => |
| 52 | Effect.gen(function* () { |
| 53 | const bind = yield* AWSBindings.Bind; |
| 54 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 55 | |
| 56 | return Effect.fn(function* (resource: Res) { |
| 57 | const Identifier = yield* options.identifier(resource); |
| 58 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 59 | const { accountId, region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{ |
| 60 | accountId: string; |
| 61 | region: string; |
| 62 | }>; |
| 63 | yield* bind`AWS.AppIntegrations.${options.name}(${resource})`({ |
| 64 | policyStatements: [ |
| 65 | { |
| 66 | Effect: "Allow", |
| 67 | Action: options.iamActions, |
| 68 | Resource: options.resources(resource, { region, accountId }), |
| 69 | }, |
| 70 | ...(options.dependentActions?.length |
| 71 | ? [ |
| 72 | { |
| 73 | Effect: "Allow" as const, |
| 74 | Action: options.dependentActions, |
| 75 | Resource: ["*"], |
| 76 | }, |
| 77 | ] |
| 78 | : []), |
| 79 | ], |
| 80 | }); |
| 81 | } |
| 82 | return Effect.fn(`AWS.AppIntegrations.${options.name}(${resource.LogicalId})`)(function* ( |
| 83 | request?: Omit<WireReq, IdKey>, |
| 84 | ) { |
| 85 | // The identifier key is re-added on top of the caller's request, so |
| 86 | // the widened spread is exactly a WireReq. |
| 87 | return yield* op({ |
| 88 | ...request, |
| 89 | [options.requestKey]: yield* Identifier, |
| 90 | } as unknown as WireReq); |
| 91 | }); |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | /** |
| 96 | * Build the implementation effect for an account-scoped AppIntegrations HTTP |
| 97 | * binding (no resource argument): grant the IAM actions on `*` at deploy time |
| 98 | * and forward requests to the operation at runtime. |
| 99 | */ |
| 100 | export const makeAppIntegrationsAccountHttpBinding = <WireReq, Out, Err, OpR>(options: { |
| 101 | /** Capability name used in the bind sid and runtime span. */ |
| 102 | name: string; |
| 103 | /** The distilled AppIntegrations operation backing the binding. */ |
| 104 | operation: Effect.Effect<(input: WireReq) => Effect.Effect<Out, Err>, never, OpR>; |
| 105 | /** IAM actions granted on the host at deploy time. */ |
| 106 | iamActions: string[]; |
| 107 | }) => |
| 108 | Effect.gen(function* () { |
| 109 | const bind = yield* AWSBindings.Bind; |
| 110 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 111 | |
| 112 | return Effect.fn(function* () { |
| 113 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 114 | yield* bind`AWS.AppIntegrations.${options.name}()`({ |
| 115 | policyStatements: [ |
| 116 | { |
| 117 | Effect: "Allow", |
| 118 | Action: options.iamActions, |
| 119 | Resource: ["*"], |
| 120 | }, |
| 121 | ], |
| 122 | }); |
| 123 | } |
| 124 | return Effect.fn(`AWS.AppIntegrations.${options.name}`)(function* (request?: WireReq) { |
| 125 | // List requests are all-optional structs; an absent request is the |
| 126 | // empty request. |
| 127 | return yield* op((request ?? {}) as WireReq); |
| 128 | }); |
| 129 | }); |
| 130 | }); |