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 | import * as Credentials from "@distilled.cloud/aws/Credentials"; |
| 2 | import * as Region from "@distilled.cloud/aws/Region"; |
| 3 | import * as SigV4 from "@distilled.cloud/aws/SigV4"; |
| 4 | import * as Output from "alchemy/Output"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Redacted from "effect/Redacted"; |
| 8 | import * as AWSBindings from "../AWSBindings.ts"; |
| 9 | import { |
| 10 | GraphQL, |
| 11 | GraphQLApiError, |
| 12 | type GraphQLClient, |
| 13 | type GraphQLRequest, |
| 14 | type GraphQLResult, |
| 15 | } from "./GraphQL.ts"; |
| 16 | import type { GraphqlApi } from "./GraphqlApi.ts"; |
| 17 | |
| 18 | /** |
| 19 | * HTTP implementation of the {@link GraphQL} binding. The AppSync data |
| 20 | * plane has no SDK operation — each request is a SigV4-signed POST |
| 21 | * (service `"appsync"`) to the API's `graphqlUrl`, made with the host |
| 22 | * Function's own credentials. Grants `appsync:GraphQL` on every field of |
| 23 | * the bound API (`{apiArn}/types/*/fields/*`). |
| 24 | */ |
| 25 | export const GraphQLHttp = Layer.effect( |
| 26 | GraphQL, |
| 27 | Effect.gen(function* () { |
| 28 | const bind = yield* AWSBindings.Bind; |
| 29 | // Resolve the ambient distilled context once at layer construction — |
| 30 | // credentials themselves are re-read per request (SSO/STS rotate). |
| 31 | const services = yield* AWSBindings.withCredentials( |
| 32 | Effect.context<Credentials.Credentials | Region.Region>(), |
| 33 | ); |
| 34 | |
| 35 | return Effect.fn(function* (api: GraphqlApi) { |
| 36 | // Outputs yield DEFERRED effects — resolving them here registers the |
| 37 | // attribute on the host environment; re-yield per invocation below. |
| 38 | const GraphqlUrl = yield* api.graphqlUrl; |
| 39 | |
| 40 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 41 | yield* bind`AWS.AppSync.GraphQL(${api})`({ |
| 42 | policyStatements: [ |
| 43 | { |
| 44 | Effect: "Allow", |
| 45 | Action: ["appsync:GraphQL"], |
| 46 | Resource: [Output.interpolate`${api.apiArn}/types/*/fields/*`], |
| 47 | }, |
| 48 | ], |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | const execute = Effect.fn(`AWS.AppSync.GraphQL(${api.LogicalId})`)(function* ( |
| 53 | request: GraphQLRequest, |
| 54 | ) { |
| 55 | const url = new URL(yield* GraphqlUrl); |
| 56 | const body = JSON.stringify({ |
| 57 | query: request.query, |
| 58 | variables: request.variables, |
| 59 | operationName: request.operationName, |
| 60 | }); |
| 61 | |
| 62 | // Sign for the API's own region, parsed from its endpoint. |
| 63 | const { credentials, region } = yield* Effect.gen(function* () { |
| 64 | const credentials = yield* yield* Credentials.Credentials; |
| 65 | const region = |
| 66 | /\.appsync-api\.([a-z0-9-]+)\.amazonaws\.com$/.exec(url.hostname)?.[1] ?? |
| 67 | (yield* yield* Region.Region); |
| 68 | return { credentials, region }; |
| 69 | }).pipe(Effect.provideContext(services)); |
| 70 | |
| 71 | const signed = yield* SigV4.sign({ |
| 72 | method: "POST", |
| 73 | url: url.toString(), |
| 74 | headers: { "content-type": "application/json" }, |
| 75 | body, |
| 76 | accessKeyId: Redacted.value(credentials.accessKeyId), |
| 77 | secretAccessKey: credentials.secretAccessKey, |
| 78 | sessionToken: credentials.sessionToken, |
| 79 | service: "appsync", |
| 80 | region, |
| 81 | allHeaders: true, |
| 82 | }); |
| 83 | |
| 84 | const toError = (status: number) => (cause: unknown) => |
| 85 | new GraphQLApiError({ |
| 86 | status, |
| 87 | body: cause instanceof Error ? cause.message : String(cause), |
| 88 | }); |
| 89 | |
| 90 | const response = yield* Effect.tryPromise({ |
| 91 | try: () => |
| 92 | fetch(signed.url, { |
| 93 | method: signed.method, |
| 94 | headers: signed.headers, |
| 95 | body, |
| 96 | }), |
| 97 | catch: toError(0), |
| 98 | }); |
| 99 | const text = yield* Effect.tryPromise({ |
| 100 | try: () => response.text(), |
| 101 | catch: toError(response.status), |
| 102 | }); |
| 103 | if (response.status < 200 || response.status >= 300) { |
| 104 | return yield* new GraphQLApiError({ status: response.status, body: text }); |
| 105 | } |
| 106 | return yield* Effect.try({ |
| 107 | try: () => JSON.parse(text) as GraphQLResult, |
| 108 | catch: toError(response.status), |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | return { execute } as GraphQLClient; |
| 113 | }); |
| 114 | }), |
| 115 | ); |