1import type { AsyncWorkerEnv } from "./stack.ts";
2
3/**
4 * Async (non-Effect) Worker fixture for the Artifacts namespace binding
5 * declared via `env: { REPOS: Cloudflare.Artifacts.Namespace(...) }`. `InferEnv` maps the
6 * marker to the native `cf.Artifacts` binding, so the handler calls
7 * `env.REPOS.create/list/get/delete(...)` directly. Mirrors the exact same
8 * routes the Effect-native worker drives (see `routes.ts`).
9 */
10export default {
11 async fetch(request: Request, env: AsyncWorkerEnv): Promise<Response> {
12 const url = new URL(request.url);
13 const name = url.searchParams.get("name") ?? "";
14 const repos = env.REPOS;
15
16 if (url.pathname === "/create") {
17 const repo = await repos.create(name, { setDefaultBranch: "main" });
18 return Response.json({
19 name: repo.name,
20 remote: repo.remote,
21 defaultBranch: repo.defaultBranch,
22 hasToken: typeof repo.token === "string" && repo.token.length > 0,
23 });
24 }
25
26 if (url.pathname === "/list") {
27 const result = await repos.list();
28 return Response.json({
29 names: result.repos.map((r) => r.name),
30 total: result.total,
31 });
32 }
33
34 if (url.pathname === "/get") {
35 try {
36 await repos.get(name);
37 return Response.json({ found: true });
38 } catch {
39 return Response.json({ found: false });
40 }
41 }
42
43 if (url.pathname === "/delete") {
44 const deleted = await repos.delete(name);
45 return Response.json({ deleted });
46 }
47
48 return new Response("Not Found", { status: 404 });
49 },
50};