From 3056c842092b9e20e9a94bfe99685bc1ae910143 Mon Sep 17 00:00:00 2001 From: QuantCode Agent Date: Thu, 18 Jun 2026 03:15:58 +0000 Subject: [PATCH] fix: repair failing tests and type errors across api and shared packages - implement paginate stub in shared/utils/pagination.ts - fix case-sensitivity bug in auth middleware public-route allow-list - align shared User type field (userName) with source usage - add missing badRequest import in routes/users.ts - wire bun-types into tsconfig so bun:test and process resolve --- packages/api/src/middleware/auth.ts | 11 ++++------- packages/api/src/routes/users.ts | 5 +---- packages/shared/src/types.ts | 2 +- packages/shared/src/utils/pagination.ts | 9 +++++---- tsconfig.json | 2 ++ 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/packages/api/src/middleware/auth.ts b/packages/api/src/middleware/auth.ts index dde32d9..2d7575e 100644 --- a/packages/api/src/middleware/auth.ts +++ b/packages/api/src/middleware/auth.ts @@ -7,15 +7,12 @@ 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. + * The allow-list is case-sensitive and uses uppercase method names as per + * RFC 7231. Both GET and POST are public; PUT, DELETE, and PATCH require + * a valid Bearer token. */ 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..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..e57aadc 100644 --- a/packages/shared/src/utils/pagination.ts +++ b/packages/shared/src/utils/pagination.ts @@ -6,10 +6,11 @@ import type { PaginatedResponse } from "../types" * @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 total = items.length + const totalPages = total === 0 ? 0 : Math.ceil(total / size) + const start = (page - 1) * size + const data = start >= total ? [] : items.slice(start, start + size) + return { data, page, pageSize: size, total, totalPages } } diff --git a/tsconfig.json b/tsconfig.json index 53de6fd..2f3af46 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,8 @@ "moduleResolution": "bundler", "strict": true, "skipLibCheck": true, + "lib": ["ES2022"], + "types": ["bun-types"], "paths": { "@e2e/shared": ["./packages/shared/src/index.ts"] }