From 6f5dc241ed6dbfb49a7ab19874554bd5f0da8948 Mon Sep 17 00:00:00 2001 From: QuantCode Agent Date: Fri, 11 Sep 2026 16:33:00 +0000 Subject: [PATCH] fix: repair failing tests and type errors across api and shared packages - users.ts: import missing badRequest helper (was ReferenceError -> 500) - auth.ts: fix HTTP method case-sensitivity so public POST /users works; normalise incoming method casing and clarify allow-list docblock - shared/types.ts: rename User.userName -> username to match API contract/tests - shared/pagination.ts: implement paginate() (1-based paging) with input clamping - tsconfig.json: add bun-types to types array to resolve process/bun:test errors --- packages/api/src/middleware/auth.ts | 14 +++++------- packages/api/src/routes/users.ts | 6 +---- packages/shared/src/types.ts | 6 +---- packages/shared/src/utils/pagination.ts | 29 ++++++++++++++++++++----- tsconfig.json | 1 + 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/packages/api/src/middleware/auth.ts b/packages/api/src/middleware/auth.ts index dde32d9..ed6a653 100644 --- a/packages/api/src/middleware/auth.ts +++ b/packages/api/src/middleware/auth.ts @@ -7,17 +7,15 @@ 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. + * Method comparison normalises only the incoming request method, which is + * upper-cased before being compared. Allow-list entries are not normalised, + * so they must themselves be uppercase to match (HTTP methods are uppercase + * per RFC 7231). */ 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)) { + if (publicMethods.includes(c.req.method.toUpperCase())) { return next() } 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..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..392eff4 100644 --- a/packages/shared/src/utils/pagination.ts +++ b/packages/shared/src/utils/pagination.ts @@ -3,13 +3,30 @@ import type { PaginatedResponse } from "../types" /** * Paginate an array of items. * - * @param items Full array of items - * @param page 1-indexed page number - * @param size Number of items per page + * Inputs are clamped before any index math: a page below 1 is treated as 1 and + * a size below 1 is treated as 1. Without this, negative or zero values yield + * silently-wrong windows (a negative start index slices from the end of the + * array, and a zero size divides by zero when computing totalPages). The + * clamped values are the ones reported back, so the response always describes + * the window that was actually returned. * - * TODO: implement this function — it is currently a stub. - * The test in packages/shared/test/pagination.test.ts exercises the full contract. + * @param items Full array of items + * @param page 1-indexed page number (values below 1 are treated as 1) + * @param size Number of items per page (values below 1 are treated as 1) */ export function paginate(items: T[], page: number, size: number): PaginatedResponse { - throw new Error("not implemented") + const effectivePage = Math.max(1, page) + const effectiveSize = Math.max(1, size) + + const total = items.length + const totalPages = Math.ceil(total / effectiveSize) + const start = (effectivePage - 1) * effectiveSize + + return { + data: items.slice(start, start + effectiveSize), + page: effectivePage, + pageSize: effectiveSize, + 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"] }