From 3fd08a4b3721220911cc47726ec64a85f7fd2991 Mon Sep 17 00:00:00 2001 From: QuantCode Agent Date: Sun, 26 Jul 2026 04:33:00 +0000 Subject: [PATCH] fix: repair failing tests and type errors across api and shared - implement paginate() utility in shared package (edge cases: empty, out-of-range, partial last page) - fix auth middleware HTTP method case-sensitivity so POST /users is public - import badRequest and align username field in users route - rename User.userName -> username in shared types for cross-package consistency - wire already-installed bun-types into tsconfig for process/bun:test resolution --- packages/api/src/middleware/auth.ts | 2 +- packages/api/src/routes/users.ts | 5 +---- packages/shared/src/types.ts | 2 +- packages/shared/src/utils/pagination.ts | 7 ++++++- tsconfig.json | 1 + 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/api/src/middleware/auth.ts b/packages/api/src/middleware/auth.ts index dde32d9..497e70e 100644 --- a/packages/api/src/middleware/auth.ts +++ b/packages/api/src/middleware/auth.ts @@ -15,7 +15,7 @@ import type { MiddlewareHandler } from "hono" */ 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..d5fca4d 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 //I usage email: string createdAt: string } diff --git a/packages/shared/src/utils/pagination.ts b/packages/shared/src/utils/pagination.ts index 12f8062..9b73992 100644 --- a/packages/shared/src/utils/pagination.ts +++ b/packages/shared/src/utils/pagination.ts @@ -11,5 +11,10 @@ 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 = size > 0 ? Math.ceil(total / size) : 0 + const safePage = Math.max(1, page) + const start = (safePage - 1) * size + const data = start >= total ? [] : items.slice(start, start + size) + return { data, total, totalPages, page: safePage, pageSize: size } } 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"] }