86 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-04-16 19:06:23 -04:00
import type { DrizzleD1Database } from "drizzle-orm/d1";
2024-04-16 18:19:34 -04:00
import { drizzle } from "drizzle-orm/d1";
2024-04-16 19:06:23 -04:00
import { json } from "itty-router-extras";
import { ZodError, z } from "zod";
import { user, sandbox } from "./schema";
import * as schema from "./schema";
2024-04-22 00:30:50 -04:00
import { eq } from "drizzle-orm";
2024-04-16 18:19:34 -04:00
export interface Env {
DB: D1Database;
}
2024-04-21 22:55:49 -04:00
// https://github.com/drizzle-team/drizzle-orm/tree/main/examples/cloudflare-d1
2024-04-16 18:19:34 -04:00
export default {
2024-04-17 21:24:57 -04:00
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const success = new Response("Success", { status: 200 });
const invalidRequest = new Response("Invalid Request", { status: 400 });
const notFound = new Response("Not Found", { status: 404 });
const methodNotAllowed = new Response("Method Not Allowed", { status: 405 });
2024-04-17 21:24:57 -04:00
const url = new URL(request.url);
const path = url.pathname;
const method = request.method;
const db = drizzle(env.DB, { schema });
2024-04-17 21:24:57 -04:00
if (path === "/api/sandbox/create" && method === "POST") {
const initSchema = z.object({
type: z.enum(["react", "node"]),
name: z.string(),
userId: z.string(),
});
2024-04-22 00:30:50 -04:00
const body = await request.json();
const { type, name, userId } = initSchema.parse(body);
2024-04-22 00:30:50 -04:00
const sb = await db.insert(sandbox).values({ type, name, userId }).returning().get();
console.log("sb:", sb);
await fetch("https://storage.ishaan1013.workers.dev/api/init", {
method: "POST",
body: JSON.stringify({ sandboxId: sb.id, type }),
headers: { "Content-Type": "application/json" },
});
return success;
// } else if (path === "/api/sandbox/files") {
} else if (path === "/api/user") {
2024-04-18 15:25:20 -04:00
if (method === "GET") {
const params = url.searchParams;
2024-04-18 15:25:20 -04:00
if (params.has("id")) {
2024-04-17 22:55:02 -04:00
const id = params.get("id") as string;
const res = await db.query.user.findFirst({
where: (user, { eq }) => eq(user.id, id),
with: {
sandbox: true,
},
});
2024-04-17 22:55:02 -04:00
return json(res ?? {});
} else {
2024-04-18 15:25:20 -04:00
const res = await db.select().from(user).all();
return json(res ?? {});
2024-04-17 22:55:02 -04:00
}
2024-04-18 15:25:20 -04:00
} else if (method === "POST") {
const userSchema = z.object({
id: z.string(),
name: z.string(),
email: z.string().email(),
});
const body = await request.json();
const { id, name, email } = userSchema.parse(body);
const res = await db.insert(user).values({ id, name, email }).returning().get();
return json({ res });
2024-04-17 21:24:57 -04:00
} else {
2024-04-22 00:30:50 -04:00
return methodNotAllowed;
2024-04-17 21:24:57 -04:00
}
2024-04-22 00:30:50 -04:00
} else return notFound;
2024-04-17 21:24:57 -04:00
},
2024-04-16 18:19:34 -04:00
};