The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 07:15 UTC
| 1 | import type { ReadNamespaceClient } from "@alchemy.run/cloudflare/KV/ReadNamespace"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 4 | |
| 5 | /** |
| 6 | * Shared read-side routes exercised by both the binding and HTTP fixtures so |
| 7 | * every method of {@link ReadNamespaceClient} is driven over `fetch`: |
| 8 | * |
| 9 | * - `GET /get?key=` — `get(key)` (text, the default). |
| 10 | * - `GET /get-json?key=` — `get(key, "json")` (parsed JSON value). |
| 11 | * - `GET /get-bulk?keys=a,b` — `get([...], "text")` (bulk read → `Map`). |
| 12 | * - `GET /getWithMetadata?key=` — `getWithMetadata(key)` (value + metadata). |
| 13 | * - `GET /list?prefix=` — `list()` / `list({ prefix })`. |
| 14 | * |
| 15 | * Returns `undefined` when the path is not a read route so the caller can fall |
| 16 | * through (used by the read-write fixtures). |
| 17 | */ |
| 18 | export const readRoutes = (kv: ReadNamespaceClient, url: URL) => |
| 19 | Effect.gen(function* () { |
| 20 | if (url.pathname === "/get") { |
| 21 | const key = url.searchParams.get("key") ?? ""; |
| 22 | const value = yield* kv.get(key).pipe(Effect.orDie); |
| 23 | return yield* HttpServerResponse.json({ value }); |
| 24 | } |
| 25 | if (url.pathname === "/get-json") { |
| 26 | const key = url.searchParams.get("key") ?? ""; |
| 27 | const value = yield* kv.get(key, "json").pipe(Effect.orDie); |
| 28 | return yield* HttpServerResponse.json({ value }); |
| 29 | } |
| 30 | if (url.pathname === "/get-bulk") { |
| 31 | const keys = (url.searchParams.get("keys") ?? "").split(",").filter((k) => k.length > 0); |
| 32 | const values = yield* kv.get(keys, "text").pipe(Effect.orDie); |
| 33 | return yield* HttpServerResponse.json({ |
| 34 | values: Object.fromEntries(values), |
| 35 | }); |
| 36 | } |
| 37 | if (url.pathname === "/getWithMetadata") { |
| 38 | const key = url.searchParams.get("key") ?? ""; |
| 39 | const result = yield* kv.getWithMetadata(key).pipe(Effect.orDie); |
| 40 | return yield* HttpServerResponse.json({ |
| 41 | value: result.value, |
| 42 | metadata: result.metadata, |
| 43 | }); |
| 44 | } |
| 45 | if (url.pathname === "/list") { |
| 46 | const prefix = url.searchParams.get("prefix") ?? undefined; |
| 47 | const result = yield* kv.list(prefix ? { prefix } : undefined).pipe(Effect.orDie); |
| 48 | return yield* HttpServerResponse.json({ |
| 49 | keys: result.keys.map((k) => k.name), |
| 50 | }); |
| 51 | } |
| 52 | return undefined; |
| 53 | }); |