From e607291a1e49785cabc26daa952beb992896923f Mon Sep 17 00:00:00 2001 From: QuantCode Agent Date: Fri, 22 May 2026 08:33:21 +0000 Subject: [PATCH] fix: resolve all failing tests and type errors - Fix auth middleware case-sensitivity bug (POST method matching) - Add missing badRequest import in users route handler - Fix field name mismatch between User type and route handler - Implement pagination utility function - Add bun-types to tsconfig for proper type resolution --- packages/api/src/middleware/auth.ts | 7 ++++--- packages/api/src/routes/users.ts | 6 +----- packages/shared/src/types.ts | 2 +- packages/shared/src/utils/pagination.ts | 6 +++++- tsconfig.json | 1 + 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/api/src/middleware/auth.ts b/packages/api/src/middleware/auth.ts index dde32d9..1a6f6ed 100644 --- a/packages/api/src/middleware/auth.ts +++ b/packages/api/src/middleware/auth.ts @@ -14,15 +14,16 @@ import type { MiddlewareHandler } from "hono" * Fix: change `'post'` to `'POST'` in the public methods array. */ export const authMiddleware: MiddlewareHandler = async (c, next) => { - // BUG: 'post' should be 'POST' — POST is never treated as public - const publicMethods = ["GET", "post"] + const publicMethods = ["GET", "POST"] if (publicMethods.includes(c.req.method)) { return next() } const token = c.req.header("Authorization")?.replace("Bearer ", "") - if (!token || token !== (process.env.API_TOKEN ?? "test-token")) { + const env = (globalThis as Record) + const envVars = (env["process"] as { env?: Record } | undefined)?.env ?? {} + if (!token || token !== (envVars["API_TOKEN"] ?? "test-token")) { return c.json({ error: "Unauthorized", status: 401 }, 401) } diff --git a/packages/api/src/routes/users.ts b/packages/api/src/routes/users.ts index 53e605a..8056ce3 100644 --- a/packages/api/src/routes/users.ts +++ b/packages/api/src/routes/users.ts @@ -1,9 +1,6 @@ import { Hono } from "hono" import { db } from "../lib/db" -import { notFound } from "../lib/errors" -// BUG: missing import — `badRequest` is used below but not imported here. -// This causes a ReferenceError at runtime when POST /users is called with invalid data. -// Fix: add `badRequest` to the import from "../lib/errors" +import { notFound, badRequest } from "../lib/errors" const router = new Hono() @@ -20,7 +17,6 @@ router.get("/:id", (c) => { router.post("/", async (c) => { const body = await c.req.json().catch(() => null) if (!body || !body.username || !body.email) { - // BUG: badRequest is not imported — this will throw ReferenceError return badRequest(c, "username and email are required") } const user = db.users.create({ username: body.username, email: body.email }) diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts index a2a1377..d522b01 100644 --- a/packages/shared/src/types.ts +++ b/packages/shared/src/types.ts @@ -8,7 +8,7 @@ export type User = { id: string - userName: string // BUG: should be `username` to match API usage + username: string email: string createdAt: string } diff --git a/packages/shared/src/utils/pagination.ts b/packages/shared/src/utils/pagination.ts index 12f8062..b861f1a 100644 --- a/packages/shared/src/utils/pagination.ts +++ b/packages/shared/src/utils/pagination.ts @@ -11,5 +11,9 @@ import type { PaginatedResponse } from "../types" * The test in packages/shared/test/pagination.test.ts exercises the full contract. */ export function paginate(items: T[], page: number, size: number): PaginatedResponse { - throw new Error("not implemented") + const total = items.length + const totalPages = Math.ceil(total / size) + const start = (page - 1) * size + const data = items.slice(start, start + size) + return { data, page, pageSize: size, total, totalPages } } diff --git a/tsconfig.json b/tsconfig.json index 53de6fd..b4bf326 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "moduleResolution": "bundler", "strict": true, "skipLibCheck": true, + "types": ["bun-types"], "paths": { "@e2e/shared": ["./packages/shared/src/index.ts"] }