2024-04-23 17:46:51 -04:00
|
|
|
import { z } from 'zod';
|
|
|
|
import startercode from './startercode';
|
2024-04-23 01:53:37 -04:00
|
|
|
|
|
|
|
export interface Env {
|
|
|
|
R2: R2Bucket;
|
|
|
|
}
|
2024-04-22 00:30:50 -04:00
|
|
|
|
|
|
|
export default {
|
2024-04-23 17:46:51 -04:00
|
|
|
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
|
|
|
const success = new Response('Success', { status: 200 });
|
2024-04-26 00:10:43 -04:00
|
|
|
const invalidRequest = new Response('Invalid Request', { status: 400 });
|
2024-04-23 17:46:51 -04:00
|
|
|
const notFound = new Response('Not Found', { status: 404 });
|
|
|
|
const methodNotAllowed = new Response('Method Not Allowed', { status: 405 });
|
|
|
|
|
2024-04-23 01:53:37 -04:00
|
|
|
const url = new URL(request.url);
|
|
|
|
const path = url.pathname;
|
|
|
|
const method = request.method;
|
2024-04-23 17:46:51 -04:00
|
|
|
|
2024-04-26 00:10:43 -04:00
|
|
|
if (path === '/api') {
|
|
|
|
if (method === 'GET') {
|
|
|
|
const params = url.searchParams;
|
|
|
|
const sandboxId = params.get('sandboxId');
|
2024-04-26 21:57:30 -04:00
|
|
|
const fileId = params.get('fileId');
|
|
|
|
|
|
|
|
if (sandboxId) {
|
|
|
|
const res = await env.R2.list({ prefix: `projects/${sandboxId}` });
|
|
|
|
return new Response(JSON.stringify(res), { status: 200 });
|
|
|
|
} else if (fileId) {
|
|
|
|
const obj = await env.R2.get(fileId);
|
|
|
|
if (obj === null) {
|
|
|
|
return new Response(`${fileId} not found`, { status: 404 });
|
|
|
|
}
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('etag', obj.httpEtag);
|
|
|
|
obj.writeHttpMetadata(headers);
|
|
|
|
|
|
|
|
const text = await obj.text();
|
|
|
|
|
|
|
|
return new Response(text, {
|
|
|
|
headers,
|
|
|
|
});
|
|
|
|
} else return invalidRequest;
|
2024-04-26 00:10:43 -04:00
|
|
|
} else if (method === 'POST') {
|
|
|
|
return new Response('Hello, world!');
|
|
|
|
} else return methodNotAllowed;
|
2024-04-27 14:23:09 -04:00
|
|
|
} else if (path === '/api/rename' && method === 'POST') {
|
|
|
|
const renameSchema = z.object({
|
|
|
|
fileId: z.string(),
|
|
|
|
newFileId: z.string(),
|
|
|
|
data: z.string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const body = await request.json();
|
|
|
|
const { fileId, newFileId, data } = renameSchema.parse(body);
|
|
|
|
|
|
|
|
await env.R2.delete(fileId);
|
|
|
|
await env.R2.put(newFileId, data);
|
|
|
|
|
2024-04-27 19:12:25 -04:00
|
|
|
return success;
|
|
|
|
} else if (path === '/api/save' && method === 'POST') {
|
|
|
|
const renameSchema = z.object({
|
|
|
|
fileId: z.string(),
|
|
|
|
data: z.string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const body = await request.json();
|
|
|
|
const { fileId, data } = renameSchema.parse(body);
|
|
|
|
|
|
|
|
await env.R2.put(fileId, data);
|
|
|
|
|
2024-04-27 14:23:09 -04:00
|
|
|
return success;
|
2024-04-26 00:10:43 -04:00
|
|
|
} else if (path === '/api/init' && method === 'POST') {
|
2024-04-23 17:46:51 -04:00
|
|
|
const initSchema = z.object({
|
|
|
|
sandboxId: z.string(),
|
|
|
|
type: z.enum(['react', 'node']),
|
|
|
|
});
|
|
|
|
|
|
|
|
const body = await request.json();
|
|
|
|
const { sandboxId, type } = initSchema.parse(body);
|
|
|
|
|
2024-04-24 01:07:20 -04:00
|
|
|
console.log(startercode[type]);
|
2024-04-23 17:46:51 -04:00
|
|
|
|
|
|
|
await Promise.all(
|
2024-04-24 01:07:20 -04:00
|
|
|
startercode[type].map(async (file) => {
|
2024-04-23 17:46:51 -04:00
|
|
|
await env.R2.put(`projects/${sandboxId}/${file.name}`, file.body);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
} else {
|
|
|
|
return notFound;
|
2024-04-23 01:53:37 -04:00
|
|
|
}
|
2024-04-22 00:30:50 -04:00
|
|
|
},
|
|
|
|
};
|