The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Drizzle from "@alchemy.run/drizzle/Postgres"; |
| 3 | import { eq } from "drizzle-orm"; |
| 4 | import { Layer } from "effect"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpServerRequest from "effect/http/HttpServerRequest"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | import { Hyperdrive } from "./Db.ts"; |
| 9 | import { relations, Users } from "./schema.ts"; |
| 10 | |
| 11 | export default class Api extends Cloudflare.Worker<Api>()( |
| 12 | "Api", |
| 13 | { |
| 14 | main: import.meta.url, |
| 15 | }, |
| 16 | Effect.gen(function* () { |
| 17 | const conn = yield* Cloudflare.Hyperdrive.Connect(Hyperdrive); |
| 18 | const db = yield* Drizzle.Postgres(conn.connectionString, { |
| 19 | relations, |
| 20 | }); |
| 21 | |
| 22 | return { |
| 23 | fetch: Effect.gen(function* () { |
| 24 | const request = yield* HttpServerRequest.HttpServerRequest; |
| 25 | switch (request.method) { |
| 26 | case "GET": { |
| 27 | if (request.url === "/") { |
| 28 | const users = yield* db.select().from(Users); |
| 29 | return yield* HttpServerResponse.json({ users }); |
| 30 | } |
| 31 | const id = Number(request.url.split("/").pop()); |
| 32 | if (Number.isNaN(id)) { |
| 33 | return yield* HttpServerResponse.json({ error: "Invalid user ID" }, { status: 400 }); |
| 34 | } |
| 35 | const user = yield* db.query.Users.findFirst({ |
| 36 | where: { id }, |
| 37 | with: { posts: true }, |
| 38 | }); |
| 39 | return yield* HttpServerResponse.json({ user }); |
| 40 | } |
| 41 | case "POST": { |
| 42 | const user = yield* db |
| 43 | .insert(Users) |
| 44 | .values({ |
| 45 | name: crypto.randomUUID(), |
| 46 | email: crypto.randomUUID(), |
| 47 | }) |
| 48 | .returning(); |
| 49 | return yield* HttpServerResponse.json({ user }); |
| 50 | } |
| 51 | case "DELETE": { |
| 52 | const id = Number(request.url.split("/").pop()); |
| 53 | if (Number.isNaN(id)) { |
| 54 | return yield* HttpServerResponse.json({ error: "Invalid user ID" }, { status: 400 }); |
| 55 | } |
| 56 | const [user] = yield* db.delete(Users).where(eq(Users.id, id)).returning(); |
| 57 | return yield* HttpServerResponse.json({ user }); |
| 58 | } |
| 59 | default: { |
| 60 | return yield* HttpServerResponse.json({ error: "Method not allowed" }, { status: 405 }); |
| 61 | } |
| 62 | } |
| 63 | }).pipe( |
| 64 | Effect.catch((cause: any) => { |
| 65 | const peel = (e: any): any => (e?.cause ? peel(e.cause) : e); |
| 66 | const root = peel(cause); |
| 67 | return HttpServerResponse.json( |
| 68 | { |
| 69 | ok: false, |
| 70 | error: String(cause), |
| 71 | rootError: root?.message ?? String(root), |
| 72 | rootCode: root?.code, |
| 73 | }, |
| 74 | { status: 500 }, |
| 75 | ); |
| 76 | }), |
| 77 | ), |
| 78 | }; |
| 79 | }).pipe(Effect.provide(Layer.mergeAll(Cloudflare.Hyperdrive.ConnectBinding))), |
| 80 | ) {} |