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

main
ts 73 lines 2.79 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { eq, sql } from "drizzle-orm";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Schema from "effect/Schema";
8 import { connectDatabase } from "./client.ts";
9 import { Todos } from "./schema.ts";
10
11 export default class Api extends AWS.Lambda.Function<Api>()(
12 "Api",
13 {
14 main: import.meta.url,
15 runtime: "nodejs22.x",
16 functionUrl: { authType: "AWS_IAM" },
17 timeout: Duration.seconds(30),
18 },
19 Effect.gen(function* () {
20 const db = yield* connectDatabase;
21 return {
22 fetch: Effect.gen(function* () {
23 const request = yield* HttpServerRequest;
24 const url = new URL(request.originalUrl);
25 if (request.method === "GET" && url.pathname === "/health") {
26 const result = yield* db.execute(sql`SELECT current_user AS username`);
27 return yield* HttpServerResponse.json(result);
28 }
29 if (url.pathname === "/todos" && request.method === "GET") {
30 return yield* HttpServerResponse.json(yield* db.select().from(Todos));
31 }
32 if (url.pathname === "/todos" && request.method === "POST") {
33 const input = yield* request.json.pipe(
34 Effect.flatMap(
35 Schema.decodeUnknownEffect(
36 Schema.Struct({
37 id: Schema.String.check(Schema.isUUID()),
38 text: Schema.NonEmptyString,
39 }),
40 ),
41 ),
42 );
43 return yield* HttpServerResponse.json(yield* db.insert(Todos).values(input).returning(), {
44 status: 201,
45 });
46 }
47 if (url.pathname.startsWith("/todos/")) {
48 const id = yield* Schema.decodeUnknownEffect(Schema.String.check(Schema.isUUID()))(
49 url.pathname.slice(7),
50 );
51 if (request.method === "PATCH") {
52 const input = yield* request.json.pipe(
53 Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ done: Schema.Boolean }))),
54 );
55 return yield* HttpServerResponse.json(
56 yield* db.update(Todos).set(input).where(eq(Todos.id, id)).returning(),
57 );
58 }
59 if (request.method === "DELETE") {
60 yield* db.delete(Todos).where(eq(Todos.id, id));
61 return HttpServerResponse.empty({ status: 204 });
62 }
63 }
64 return HttpServerResponse.text("Not found", { status: 404 });
65 }).pipe(
66 Effect.catchTag("SchemaError", () =>
67 Effect.succeed(HttpServerResponse.text("Invalid request", { status: 400 })),
68 ),
69 Effect.orDie,
70 ),
71 };
72 }).pipe(Effect.provide(AWS.DSQL.ConnectHttp)),
73 ) {}