From f25c23951f764959b71408b8f08c623a25d00433 Mon Sep 17 00:00:00 2001 From: seveibar Date: Sat, 6 Jul 2024 10:31:41 -0700 Subject: [PATCH] wip refactoring dev server to level from sqlite --- .../routes/api/dev_package_examples/create.ts | 3 -- .../routes/api/dev_package_examples/get.ts | 31 +++++++------------ dev-server-api/routes/index.ts | 9 +----- dev-server-api/src/db/schema.ts | 2 +- dev-server-api/src/db/zod-level-db.ts | 2 +- dev-server-api/static-routes.ts | 6 +++- .../routes/dev_package_examples/get.test.ts | 26 ++++++++++++++++ 7 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 dev-server-api/tests/routes/dev_package_examples/get.test.ts diff --git a/dev-server-api/routes/api/dev_package_examples/create.ts b/dev-server-api/routes/api/dev_package_examples/create.ts index 5118d0f8..fe95c4e0 100644 --- a/dev-server-api/routes/api/dev_package_examples/create.ts +++ b/dev-server-api/routes/api/dev_package_examples/create.ts @@ -1,5 +1,4 @@ import { withWinterSpec } from "src/with-winter-spec" -import { NotFoundError } from "edgespec/middleware" import { z } from "zod" export default withWinterSpec({ @@ -23,8 +22,6 @@ export default withWinterSpec({ auth: "none", })(async (req, ctx) => { const tscircuit_soup = req.jsonBody.tscircuit_soup - ? JSON.stringify(req.jsonBody.tscircuit_soup) - : undefined const existingDevPackageExample = await ctx.db.find("dev_package_example", { file_path: req.jsonBody.file_path, diff --git a/dev-server-api/routes/api/dev_package_examples/get.ts b/dev-server-api/routes/api/dev_package_examples/get.ts index 4310e0f1..1be15ff7 100644 --- a/dev-server-api/routes/api/dev_package_examples/get.ts +++ b/dev-server-api/routes/api/dev_package_examples/get.ts @@ -26,30 +26,21 @@ export default withWinterSpec({ .datetime() .nullable() .default(null), - last_updated_at: z.string().datetime(), + last_updated_at: z.string().datetime().nullable(), }), }), auth: "none", })(async (req, ctx) => { + const dev_package_example = await ctx.db.get( + "dev_package_example", + req.commonParams.dev_package_example_id + ) + + if (!dev_package_example) { + throw new NotFoundError("Package not found") + } + return ctx.json({ - dev_package_example: await ctx.db - .selectFrom("dev_package_example") - .selectAll() - .where( - "dev_package_example_id", - "=", - req.commonParams.dev_package_example_id - ) - .executeTakeFirstOrThrow((e) => { - throw new NotFoundError("Package not found") - }) - .then((r) => ({ - ...r, - is_loading: r.is_loading === 1, - tscircuit_soup: JSON.parse(r.tscircuit_soup), - completed_edit_events: r.completed_edit_events - ? JSON.parse(r.completed_edit_events) - : null, - })), + dev_package_example, }) }) diff --git a/dev-server-api/routes/index.ts b/dev-server-api/routes/index.ts index f7531daa..4caf598f 100644 --- a/dev-server-api/routes/index.ts +++ b/dev-server-api/routes/index.ts @@ -1,19 +1,12 @@ import { withWinterSpec } from "../src/with-winter-spec" import { z } from "zod" -import staticRoutes from "../static-routes" export default withWinterSpec({ methods: ["GET"], auth: "none", })(async (req, ctx) => { return new Response( - `This is the dev server API view database

${Object.entries( - staticRoutes - ) - .map( - ([routePath]) => `
${routePath}
` - ) - .join("")}`, + `This is the dev server API view database`, { headers: { "content-type": "text/html", diff --git a/dev-server-api/src/db/schema.ts b/dev-server-api/src/db/schema.ts index e3d79623..5eef7c78 100644 --- a/dev-server-api/src/db/schema.ts +++ b/dev-server-api/src/db/schema.ts @@ -16,7 +16,7 @@ export const DevPackageExampleSchema = z.object({ file_path: z.string(), export_name: nullableText(), tscircuit_soup: z.any().nullable(), // Using any for JSON type - completed_edit_events: z.any().nullable(), // Using any for JSON type + completed_edit_events: z.array(z.any()).nullable().default([]), // Using any for JSON type error: nullableText(), is_loading: z.boolean(), soup_last_updated_at: nullableText(), diff --git a/dev-server-api/src/db/zod-level-db.ts b/dev-server-api/src/db/zod-level-db.ts index 7f45e930..fcd13e01 100644 --- a/dev-server-api/src/db/zod-level-db.ts +++ b/dev-server-api/src/db/zod-level-db.ts @@ -20,7 +20,7 @@ export class ZodLevelDatabase { async get( collection: K, - id: string + id: string | number ): Promise { const key = `${collection}:${id}` const data = await this.db.get(key) diff --git a/dev-server-api/static-routes.ts b/dev-server-api/static-routes.ts index 3c363d4c..a5868452 100644 --- a/dev-server-api/static-routes.ts +++ b/dev-server-api/static-routes.ts @@ -2,6 +2,7 @@ // import { WinterSpecRouteMap } from "@winterspec/types" const routeMap = { + "/api/db/download": (await import('routes/api/db/download.ts')).default, "/api/dev_package_examples/create": (await import('routes/api/dev_package_examples/create.ts')).default, "/api/dev_package_examples/get": (await import('routes/api/dev_package_examples/get.ts')).default, "/api/dev_package_examples/list": (await import('routes/api/dev_package_examples/list.ts')).default, @@ -14,7 +15,10 @@ const routeMap = { "/api/export_requests/list": (await import('routes/api/export_requests/list.ts')).default, "/api/export_requests/update": (await import('routes/api/export_requests/update.ts')).default, "/api/health": (await import('routes/api/health.ts')).default, - "/health": (await import('routes/health.ts')).default + "/api/package_info/create": (await import('routes/api/package_info/create.ts')).default, + "/api/package_info/get": (await import('routes/api/package_info/get.ts')).default, + "/health": (await import('routes/health.ts')).default, + "/": (await import('routes/index.ts')).default } export default routeMap diff --git a/dev-server-api/tests/routes/dev_package_examples/get.test.ts b/dev-server-api/tests/routes/dev_package_examples/get.test.ts new file mode 100644 index 00000000..76193228 --- /dev/null +++ b/dev-server-api/tests/routes/dev_package_examples/get.test.ts @@ -0,0 +1,26 @@ +import { it, expect } from "bun:test" +import { getTestFixture } from "tests/fixtures/get-test-server" + +it("POST /api/dev_package_examples/create", async () => { + const { axios } = await getTestFixture() + + await axios + .post("/api/dev_package_examples/create", { + file_path: "examples/basic-resistor.tsx", + export_name: "default", + tscircuit_soup: [], + is_loading: true, + }) + .then((r) => r.data) + + const res = await axios + .post("/api/dev_package_examples/get", { + dev_package_example_id: 1, + }) + .then((r) => r.data) + + console.log(res.dev_package_example) + expect(res.dev_package_example.file_path).toEqual( + "examples/basic-resistor.tsx" + ) +})