@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, 06:00 UTC

main
ts 73 lines 2.68 KB
Raw
1 import * as DAX from "@alchemy.run/aws/DAX";
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 // Well-formed-but-nonexistent cluster name — the /clusters route drives the
12 // DescribeClusters binding against it so the fixture exercises the IAM grant
13 // and the typed error decode at zero cost (no cluster is ever created).
14 const NONEXISTENT_CLUSTER_NAME = "alchemy-nonexistent-dax-probe";
15
16 export class DAXTestFunction extends Lambda.Function<Lambda.Function>()("DAXTestFunction") {}
17
18 export default DAXTestFunction.make(
19 {
20 main,
21 functionUrl: true,
22 },
23 Effect.gen(function* () {
24 const describeClusters = yield* DAX.DescribeClusters();
25 const describeEvents = yield* DAX.DescribeEvents();
26
27 const bound = {
28 describeClusters,
29 describeEvents,
30 };
31
32 return {
33 fetch: Effect.gen(function* () {
34 const request = yield* HttpServerRequest;
35 const url = new URL(request.originalUrl);
36 const pathname = url.pathname;
37
38 if (request.method === "GET" && pathname === "/bindings") {
39 return yield* HttpServerResponse.json({
40 bound: Object.keys(bound),
41 });
42 }
43
44 if (request.method === "GET" && pathname === "/clusters") {
45 // A nonexistent cluster name must surface the service's typed
46 // not-found tag — an IAM gap would surface AccessDeniedException
47 // and fail the route with an opaque 500 instead.
48 const tag = yield* describeClusters({
49 ClusterNames: [NONEXISTENT_CLUSTER_NAME],
50 }).pipe(
51 Effect.map(() => "Found"),
52 Effect.catchTag("ClusterNotFoundFault", (e) => Effect.succeed(e._tag)),
53 );
54 return yield* HttpServerResponse.json({ tag });
55 }
56
57 if (request.method === "GET" && pathname === "/events") {
58 // Listing the account's recent DAX events succeeds (possibly
59 // empty) — proves the grant and the response schema decode.
60 const result = yield* describeEvents();
61 return yield* HttpServerResponse.json({
62 count: (result.Events ?? []).length,
63 });
64 }
65
66 return yield* HttpServerResponse.json(
67 { error: "Not found", method: request.method, pathname },
68 { status: 404 },
69 );
70 }).pipe(Effect.orDie),
71 };
72 }).pipe(Effect.provide(Layer.mergeAll(DAX.DescribeClustersHttp, DAX.DescribeEventsHttp))),
73 );