The best way to control Claude Code, Codex and more from any computer or phone

main
ts 106 lines 4.06 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { GraphqlApi } from "./GraphqlApi.ts";
4
5 /**
6 * Shared scaffolding for AppSync HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every single-operation `{Op}Http.ts` in
9 * this service is a thin `Layer.effect(Cap, make…HttpBinding({ … }))` over
10 * one of the two builders below. Everything except the operation and the
11 * IAM action is boilerplate:
12 *
13 * - {@link makeAppSyncApiHttpBinding} — control-plane operations scoped to a
14 * bound {@link GraphqlApi} (`FlushApiCache`, `GetIntrospectionSchema`).
15 * The runtime callable injects the bound API's `apiId`.
16 * - {@link makeAppSyncAccountHttpBinding} — account-level pass-through
17 * operations that bind to no resource (`EvaluateCode`).
18 *
19 * Both grant on `Resource: "*"` — these AppSync control-plane actions define
20 * no IAM resource types (per the Service Authorization Reference), so a
21 * scoped API ARN is AccessDenied. `GraphQLHttp` stays bespoke: the data
22 * plane has no SDK operation, each request is a SigV4-signed POST to the
23 * API's `graphqlUrl`.
24 */
25
26 /**
27 * Build the impl Effect for a control-plane operation scoped to a bound
28 * {@link GraphqlApi}. The runtime callable injects the API's `apiId` into
29 * the request; the deploy-time half grants `actions` on `*` (these actions
30 * define no IAM resource types).
31 */
32 export const makeAppSyncApiHttpBinding = <I extends { apiId: string }, A, E, R>(options: {
33 /** Fully-qualified binding tag, e.g. `AWS.AppSync.FlushApiCache`. */
34 tag: string;
35 /** The distilled operation; `apiId` is injected from the bound API. */
36 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
37 /** IAM actions granted on `*`. */
38 actions: readonly string[];
39 }) =>
40 Effect.gen(function* () {
41 const bind = yield* AWSBindings.Bind;
42 const op = yield* AWSBindings.withCredentials(options.operation);
43
44 return Effect.fn(function* (api: GraphqlApi) {
45 // Outputs yield DEFERRED effects — resolving them here registers the
46 // attribute on the host environment; re-yield per invocation below.
47 const ApiId = yield* api.apiId;
48
49 if (!globalThis.__ALCHEMY_RUNTIME__) {
50 yield* bind`${options.tag}(${api})`({
51 policyStatements: [
52 {
53 Effect: "Allow",
54 Action: [...options.actions],
55 // These AppSync control-plane actions define no IAM resource
56 // types — IAM only authorizes them against "*" (a scoped API
57 // ARN is AccessDenied).
58 Resource: ["*"],
59 },
60 ],
61 });
62 }
63
64 return Effect.fn(`${options.tag}(${api.LogicalId})`)(function* (request?: Omit<I, "apiId">) {
65 return yield* op({ ...request, apiId: yield* ApiId } as I);
66 });
67 });
68 });
69
70 /**
71 * Build the impl Effect for an account-level AppSync operation that binds
72 * to no resource. The runtime callable passes the caller's request through
73 * unchanged; the deploy-time half grants `actions` on `*` (these actions
74 * define no IAM resource types).
75 */
76 export const makeAppSyncAccountHttpBinding = <I, A, E, R>(options: {
77 /** Fully-qualified binding tag, e.g. `AWS.AppSync.EvaluateCode`. */
78 tag: string;
79 /** The distilled operation, invoked with the caller's request as-is. */
80 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
81 /** IAM actions granted on `*`. */
82 actions: 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* () {
89 if (!globalThis.__ALCHEMY_RUNTIME__) {
90 yield* bind`${options.tag}()`({
91 policyStatements: [
92 {
93 Effect: "Allow",
94 Action: [...options.actions],
95 // No IAM resource types defined for these actions.
96 Resource: ["*"],
97 },
98 ],
99 });
100 }
101
102 return Effect.fn(options.tag)(function* (request: I) {
103 return yield* op(request);
104 });
105 });
106 });