@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 109 lines 3.78 KB
Raw
1 import * as CodeConnections from "@alchemy.run/aws/CodeConnections";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import path from "pathe";
8
9 const main = path.resolve(import.meta.dirname, "handler.ts");
10
11 // Connection names are capped at 32 chars.
12 export const fixtureConnectionName = "alchemy-test-codeconn-bind";
13
14 export class CodeConnectionsTestFunction extends Lambda.Function<Lambda.Function>()(
15 "CodeConnectionsTestFunction",
16 ) {}
17
18 export default CodeConnectionsTestFunction.make(
19 {
20 main,
21 functionUrl: true,
22 },
23 Effect.gen(function* () {
24 // The fixture connection stays PENDING (its OAuth handshake is a manual
25 // console step) — reading its live state still exercises the bindings.
26 const connection = yield* CodeConnections.Connection("BindingsConn", {
27 connectionName: fixtureConnectionName,
28 providerType: "GitHub",
29 });
30
31 // --- connection-scoped binding ---
32 const getConnection = yield* CodeConnections.GetConnection(connection);
33
34 // --- account-level bindings ---
35 const listConnections = yield* CodeConnections.ListConnections();
36 const listHosts = yield* CodeConnections.ListHosts();
37 const listRepositoryLinks = yield* CodeConnections.ListRepositoryLinks();
38
39 const bound = {
40 getConnection,
41 listConnections,
42 listHosts,
43 listRepositoryLinks,
44 };
45
46 return {
47 fetch: Effect.gen(function* () {
48 const request = yield* HttpServerRequest;
49 const url = new URL(request.originalUrl);
50 const pathname = url.pathname;
51
52 if (request.method === "GET" && pathname === "/bindings") {
53 return yield* HttpServerResponse.json({
54 bound: Object.keys(bound),
55 });
56 }
57
58 if (request.method === "GET" && pathname === "/connection") {
59 // Exercises `ConnectionArn` injection + the IAM grant on the
60 // connection ARN (a grant gap would surface AccessDeniedException
61 // and fail the route with an opaque 500).
62 const result = yield* getConnection();
63 return yield* HttpServerResponse.json({
64 name: result.Connection?.ConnectionName ?? null,
65 status: result.Connection?.ConnectionStatus ?? null,
66 providerType: result.Connection?.ProviderType ?? null,
67 });
68 }
69
70 if (request.method === "GET" && pathname === "/connections") {
71 const result = yield* listConnections({
72 ProviderTypeFilter: "GitHub",
73 });
74 return yield* HttpServerResponse.json({
75 names: (result.Connections ?? []).map((c) => c.ConnectionName ?? null),
76 });
77 }
78
79 if (request.method === "GET" && pathname === "/hosts") {
80 const result = yield* listHosts();
81 return yield* HttpServerResponse.json({
82 names: (result.Hosts ?? []).map((h) => h.Name ?? null),
83 });
84 }
85
86 if (request.method === "GET" && pathname === "/repository-links") {
87 const result = yield* listRepositoryLinks();
88 return yield* HttpServerResponse.json({
89 repositories: (result.RepositoryLinks ?? []).map((l) => l.RepositoryName ?? null),
90 });
91 }
92
93 return yield* HttpServerResponse.json(
94 { error: "Not found", method: request.method, pathname },
95 { status: 404 },
96 );
97 }).pipe(Effect.orDie),
98 };
99 }).pipe(
100 Effect.provide(
101 Layer.mergeAll(
102 CodeConnections.GetConnectionHttp,
103 CodeConnections.ListConnectionsHttp,
104 CodeConnections.ListHostsHttp,
105 CodeConnections.ListRepositoryLinksHttp,
106 ),
107 ),
108 ),
109 );