import { z } from 'zod'; import startercode from './startercode'; export interface Env { R2: R2Bucket; } export default { async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { 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 }); const url = new URL(request.url); const path = url.pathname; const method = request.method; if (path === '/api') { if (method === 'GET') { const params = url.searchParams; const sandboxId = params.get('sandboxId'); if (!sandboxId) { return invalidRequest; } const res = await env.R2.list({ prefix: `projects/${sandboxId}` }); return new Response(JSON.stringify(res), { status: 200 }); } else if (method === 'POST') { return new Response('Hello, world!'); } else return methodNotAllowed; } else if (path === '/api/init' && method === 'POST') { const initSchema = z.object({ sandboxId: z.string(), type: z.enum(['react', 'node']), }); const body = await request.json(); const { sandboxId, type } = initSchema.parse(body); console.log(startercode[type]); await Promise.all( startercode[type].map(async (file) => { await env.R2.put(`projects/${sandboxId}/${file.name}`, file.body); }) ); return success; } else { return notFound; } }, };