-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[explorer] implement local kv api #12075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
524243d
f54442d
2ebaef9
68a9532
1dc92f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| "miniflare": minor | ||
| --- | ||
|
|
||
| Implement local KV API for experimental/WIP local resource explorer | ||
|
|
||
| The following APIs have been (mostly) implemented: | ||
| GET /storage/kv/namespaces - List namespaces | ||
| GET /storage/kv/namespaces/:id/keys - List keys | ||
| GET /storage/kv/namespaces/:id/values/:key - Get value | ||
| PUT /storage/kv/namespaces/:id/values/:key - Write value | ||
| DELETE /storage/kv/namespaces/:id/values/:key - Delete key | ||
| POST /storage/kv/namespaces/:id/bulk/get - Bulk get values |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,10 @@ | |
| { | ||
| "binding": "KV", | ||
| }, | ||
| { | ||
| "binding": "KV_WITH_ID", | ||
| "id": "some-kv-id", | ||
| }, | ||
| ], | ||
| "d1_databases": [ | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,6 +73,7 @@ | |
| "acorn-walk": "8.3.2", | ||
| "capnp-es": "catalog:default", | ||
| "capnweb": "^0.1.0", | ||
| "chanfana": "^2.8.3", | ||
| "chokidar": "^4.0.1", | ||
| "concurrently": "^8.2.2", | ||
| "devalue": "^5.3.2", | ||
|
|
@@ -87,6 +88,7 @@ | |
| "get-port": "^7.1.0", | ||
| "glob-to-regexp": "0.4.1", | ||
| "heap-js": "^2.5.0", | ||
| "hono": "^4.11.5", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious about why we use hono for vs its size? |
||
| "http-cache-semantics": "^4.1.0", | ||
| "kleur": "^4.1.5", | ||
| "mime": "^3.0.0", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,55 @@ | ||
| // local explorer API Worker | ||
| // Provides a REST API for viewing and manipulating user resources | ||
|
|
||
| import { WorkerEntrypoint } from "cloudflare:workers"; | ||
| import { fromHono } from "chanfana"; | ||
| import { Hono } from "hono"; | ||
| import { | ||
| BulkGetKVValues, | ||
| DeleteKVValue, | ||
| GetKVValue, | ||
| ListKVKeys, | ||
| ListKVNamespaces, | ||
| PutKVValue, | ||
| } from "./resources/kv"; | ||
|
|
||
| export default class LocalExplorerAPI extends WorkerEntrypoint { | ||
| async fetch(): Promise<Response> { | ||
| return new Response("Hello from local explorer API"); | ||
| } | ||
| } | ||
| type BindingIdMap = { | ||
| kv: Record<string, string>; // namespaceId -> bindingName | ||
| }; | ||
| export type Env = { | ||
| [key: string]: unknown; | ||
| LOCAL_EXPLORER_BINDING_MAP: BindingIdMap; | ||
| }; | ||
|
|
||
| export type AppBindings = { Bindings: Env }; | ||
|
|
||
| const BASE_PATH = "/cdn-cgi/explorer/api"; | ||
|
|
||
| const app = new Hono<AppBindings>().basePath(BASE_PATH); | ||
|
|
||
| const openapi = fromHono(app, { | ||
| base: BASE_PATH, | ||
| schema: { | ||
| info: { | ||
| title: "Local Explorer API", | ||
| version: "1.0.0", | ||
| description: | ||
| "A local subset of Cloudflare's REST API for exploring resources during local development.", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Have we changed our mind on this ? |
||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // ============================================================================ | ||
| // KV Endpoints | ||
| // ============================================================================ | ||
|
|
||
| openapi.get("/storage/kv/namespaces", ListKVNamespaces); | ||
| openapi.get("/storage/kv/namespaces/:namespaceId/keys", ListKVKeys); | ||
| openapi.get("/storage/kv/namespaces/:namespaceId/values/:keyName", GetKVValue); | ||
| openapi.put("/storage/kv/namespaces/:namespaceId/values/:keyName", PutKVValue); | ||
| openapi.delete( | ||
| "/storage/kv/namespaces/:namespaceId/values/:keyName", | ||
| DeleteKVValue | ||
| ); | ||
| openapi.post("/storage/kv/namespaces/:namespaceId/bulk/get", BulkGetKVValues); | ||
|
|
||
| export default app; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| export const ResponseInfoSchema = z.object({ | ||
| code: z.number(), | ||
| message: z.string(), | ||
| }); | ||
|
|
||
| export const PageResultInfoSchema = z.object({ | ||
| count: z.number(), | ||
| page: z.number(), | ||
| per_page: z.number(), | ||
| total_count: z.number(), | ||
| }); | ||
|
|
||
| export const CursorResultInfoSchema = z.object({ | ||
| count: z.number(), | ||
| cursor: z.string(), | ||
| }); | ||
|
|
||
| export const CloudflareEnvelope = <T extends z.ZodTypeAny>(resultSchema: T) => | ||
| z.object({ | ||
| success: z.boolean(), | ||
| errors: z.array(ResponseInfoSchema), | ||
| messages: z.array(ResponseInfoSchema), | ||
| result: resultSchema, | ||
| }); | ||
|
|
||
| // ============================================================================ | ||
| // Response Helpers | ||
| // ============================================================================ | ||
|
|
||
| export function wrapResponse<T>(result: T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would love to see more JSDoc across this PR. |
||
| return { | ||
| success: true as const, | ||
| errors: [] as { code: number; message: string }[], | ||
| messages: [] as { code: number; message: string }[], | ||
| result, | ||
| }; | ||
| } | ||
|
|
||
| export function errorResponse(status: number, code: number, message: string) { | ||
| return Response.json( | ||
| { | ||
| success: false, | ||
| errors: [{ code, message }], | ||
| messages: [], | ||
| result: null, | ||
| }, | ||
| { status } | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be JSON (and should we check the content type ?)