From 006f966df9a6db5d69fac3c155df5e14cf8dae3c Mon Sep 17 00:00:00 2001 From: QuantCode Agent Date: Wed, 23 Sep 2026 16:30:53 +0000 Subject: [PATCH] fix: repair failing tests and type errors across api and shared packages - auth middleware: fix HTTP method casing ("post" -> "POST") so POST /users is correctly treated as a public route - users route: add missing badRequest import (was causing a runtime ReferenceError on invalid POST bodies) - shared types: rename User.userName -> username to match the API and test contract - shared pagination: implement the paginate() stub (1-indexed pages, correct slice/total/totalPages, empty-array yields totalPages 0) and guard against non-finite page/size - tsconfig: add "types": ["bun-types"] so bun:test and process resolve under typecheck (no new dependencies added) All 22 tests pass and tsc --noEmit is clean. --- packages/api/src/middleware/auth.ts | 10 +++------- packages/api/src/routes/users.ts | 6 +----- packages/shared/src/types.ts | 6 +----- packages/shared/src/utils/pagination.ts | 23 +++++++++++++++++++---- tsconfig.json | 1 + 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/packages/api/src/middleware/auth.ts b/packages/api/src/middleware/auth.ts index dde32d9..5c4e5e0 100644 --- a/packages/api/src/middleware/auth.ts +++ b/packages/api/src/middleware/auth.ts @@ -7,15 +7,11 @@ import type { MiddlewareHandler } from "hono" * GET, POST → public (no token required) * PUT, DELETE, PATCH → require Bearer token * - * BUG: The allow-list check uses `'post'` (lowercase) instead of `'POST'`. - * HTTP methods are always uppercase per RFC 7231, so POST is never matched - * as a public method — POST requests incorrectly require a token. - * - * Fix: change `'post'` to `'POST'` in the public methods array. + * HTTP methods are always uppercase per RFC 7231, so the allow-list is + * compared against uppercase method names. */ 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() diff --git a/packages/api/src/routes/users.ts b/packages/api/src/routes/users.ts index 53e605a..eaea2d5 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 { badRequest, notFound } 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..b6f7974 100644 --- a/packages/shared/src/types.ts +++ b/packages/shared/src/types.ts @@ -1,14 +1,10 @@ /** * Shared types used by both the API and any consumers. - * - * BUG: The field is named `userName` here but the API routes reference `username` - * (lowercase n). This causes a type error in routes/users.ts and a runtime - * mismatch when serialising responses. */ 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..a78cc5f 100644 --- a/packages/shared/src/utils/pagination.ts +++ b/packages/shared/src/utils/pagination.ts @@ -3,13 +3,28 @@ import type { PaginatedResponse } from "../types" /** * Paginate an array of items. * + * `page` and `size` are clamped to a minimum of 1 and truncated to integers; + * non-finite values (`NaN`, `Infinity`) are treated as 1. A page beyond the + * available range returns an empty `data` array rather than throwing. An empty + * input array yields `totalPages: 0`. + * * @param items Full array of items * @param page 1-indexed page number * @param size Number of items per page - * - * TODO: implement this function — it is currently a stub. - * 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 clamp = (n: number) => (Number.isFinite(n) ? Math.max(1, Math.floor(n)) : 1) + const pageSize = clamp(size) + const currentPage = clamp(page) + const total = items.length + const totalPages = Math.ceil(total / pageSize) + const start = (currentPage - 1) * pageSize + + return { + data: items.slice(start, start + pageSize), + page: currentPage, + pageSize, + 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"] }