| 1 | import * as CodeConnections from "@/AWS/CodeConnections"; |
| 2 | import * as Lambda from "@/AWS/Lambda"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 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( |
| 76 | (c) => c.ConnectionName ?? null, |
| 77 | ), |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | if (request.method === "GET" && pathname === "/hosts") { |
| 82 | const result = yield* listHosts(); |
| 83 | return yield* HttpServerResponse.json({ |
| 84 | names: (result.Hosts ?? []).map((h) => h.Name ?? null), |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | if (request.method === "GET" && pathname === "/repository-links") { |
| 89 | const result = yield* listRepositoryLinks(); |
| 90 | return yield* HttpServerResponse.json({ |
| 91 | repositories: (result.RepositoryLinks ?? []).map( |
| 92 | (l) => l.RepositoryName ?? null, |
| 93 | ), |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | return yield* HttpServerResponse.json( |
| 98 | { error: "Not found", method: request.method, pathname }, |
| 99 | { status: 404 }, |
| 100 | ); |
| 101 | }).pipe(Effect.orDie), |
| 102 | }; |
| 103 | }).pipe( |
| 104 | Effect.provide( |
| 105 | Layer.mergeAll( |
| 106 | CodeConnections.GetConnectionHttp, |
| 107 | CodeConnections.ListConnectionsHttp, |
| 108 | CodeConnections.ListHostsHttp, |
| 109 | CodeConnections.ListRepositoryLinksHttp, |
| 110 | ), |
| 111 | ), |
| 112 | ), |
| 113 | ); |