mirror of
https://github.com/VibedByKaKi/t3-code-android-nightly.git
synced 2026-10-20 01:07:44 +02:00
72 lines
2.7 KiB
TypeScript
72 lines
2.7 KiB
TypeScript
import type { ReadWriteNamespaceClient } from "@/Cloudflare/Artifacts/ReadWriteNamespace.ts";
|
|
import * as Effect from "effect/Effect";
|
|
import * as HttpServerResponse from "effect/http/HttpServerResponse";
|
|
|
|
/**
|
|
* Shared routes that exercise every method of {@link ReadWriteNamespaceClient} over
|
|
* `fetch`. The same routes back both the Effect-native worker (which yields
|
|
* `Cloudflare.Artifacts.ReadWriteNamespace(...)`) and — by mirroring the logic in plain
|
|
* async/await — the async worker, so both invocation styles drive the exact
|
|
* same behavior against one shared namespace.
|
|
*
|
|
* Routes:
|
|
* - `POST /create?name=` — create a repo, returns `{ name, remote, hasToken }`
|
|
* - `GET /list` — list repos, returns `{ names: string[], total }`
|
|
* - `GET /get?name=` — fetch a repo handle, returns `{ found: boolean }`
|
|
* - `DELETE /delete?name=` — delete a repo, returns `{ deleted: boolean }`
|
|
*
|
|
* Returns `undefined` for unknown paths so the worker shell can 404.
|
|
*/
|
|
export const artifactsRoutes = (client: ReadWriteNamespaceClient, url: URL) =>
|
|
Effect.gen(function* () {
|
|
const name = url.searchParams.get("name") ?? "";
|
|
|
|
// Surface the typed ArtifactsError as a 500 with the message in the
|
|
// body (instead of orDie's empty 500) so a failing test's
|
|
// WorkerNotReady error says WHAT the binding rejected with.
|
|
const failWith = (error: { message: string }) =>
|
|
HttpServerResponse.json({ error: error.message }, { status: 500 });
|
|
|
|
if (url.pathname === "/create") {
|
|
return yield* client.create(name, { setDefaultBranch: "main" }).pipe(
|
|
Effect.flatMap((repo) =>
|
|
HttpServerResponse.json({
|
|
name: repo.name,
|
|
remote: repo.remote,
|
|
defaultBranch: repo.defaultBranch,
|
|
hasToken: typeof repo.token === "string" && repo.token.length > 0,
|
|
}),
|
|
),
|
|
Effect.catchTag("ArtifactsError", failWith),
|
|
);
|
|
}
|
|
|
|
if (url.pathname === "/list") {
|
|
return yield* client.list().pipe(
|
|
Effect.flatMap((result) =>
|
|
HttpServerResponse.json({
|
|
names: result.repos.map((r) => r.name),
|
|
total: result.total,
|
|
}),
|
|
),
|
|
Effect.catchTag("ArtifactsError", failWith),
|
|
);
|
|
}
|
|
|
|
if (url.pathname === "/get") {
|
|
const found = yield* client.get(name).pipe(
|
|
Effect.as(true),
|
|
Effect.catchTag("ArtifactsError", () => Effect.succeed(false)),
|
|
);
|
|
return yield* HttpServerResponse.json({ found });
|
|
}
|
|
|
|
if (url.pathname === "/delete") {
|
|
return yield* client.delete(name).pipe(
|
|
Effect.flatMap((deleted) => HttpServerResponse.json({ deleted })),
|
|
Effect.catchTag("ArtifactsError", failWith),
|
|
);
|
|
}
|
|
|
|
return undefined;
|
|
});
|