From 609196296b82b980873ff070f8b3b9d5a7ee5b6e Mon Sep 17 00:00:00 2001 From: QuantCode Agent Date: Mon, 15 Jun 2026 03:29:25 +0000 Subject: [PATCH] fix: resolve failing tests and type errors across api and shared packages - auth middleware: fix case-sensitive HTTP method check so POST /users is public - users route: import missing badRequest error helper (was 500, now 400) - shared types: rename User.userName -> username to match consumers - shared: implement paginate() utility (was a stub) with page clamping - tsconfig: wire up bun/node types to resolve process and bun:test errors --- packages/api/src/middleware/auth.ts | 3 +-- packages/api/src/routes/users.ts | 5 +---- packages/shared/src/types.ts | 9 +-------- packages/shared/src/utils/pagination.ts | 12 +++++++----- tsconfig.json | 1 + 5 files changed, 11 insertions(+), 19 deletions(-) diff --git a/packages/api/src/middleware/auth.ts b/packages/api/src/middleware/auth.ts index dde32d9..567c4c8 100644 --- a/packages/api/src/middleware/auth.ts +++ b/packages/api/src/middleware/auth.ts @@ -14,8 +14,7 @@ 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() diff --git a/packages/api/src/routes/users.ts b/packages/api/src/routes/users.ts index 53e605a..ea4702a 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() diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts index a2a1377..ff89675 100644 --- a/packages/shared/src/types.ts +++ b/packages/shared/src/types.ts @@ -1,14 +1,7 @@ -/** - * 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..d429461 100644 --- a/packages/shared/src/utils/pagination.ts +++ b/packages/shared/src/utils/pagination.ts @@ -4,12 +4,14 @@ import type { PaginatedResponse } from "../types" * Paginate an array of items. * * @param items Full array of items - * @param page 1-indexed page number + * @param page 1-indexed page number (clamped to ≥1) * @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 safePage = Math.max(1, Math.floor(page)) + const total = items.length + const totalPages = total === 0 ? 0 : Math.ceil(total / size) + const start = (safePage - 1) * size + const data = start >= total ? [] : items.slice(start, start + size) + return { data, page: safePage, 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"] }