From 0320ef3173f247ff30263863110a0ed67f026884 Mon Sep 17 00:00:00 2001 From: QuantCode Agent Date: Sun, 26 Jul 2026 23:08:56 +0000 Subject: [PATCH] fix: repair auth casing, shared type field, pagination, and imports - auth: normalise HTTP method comparison so POST /users is public (was case-sensitive) - shared types: rename User.userName -> username for cross-package consistency - users route: add missing badRequest/notFound imports - pagination: implement paginate() per test contract - tsconfig: add bun-types so process and bun:test resolve --- packages/api/src/middleware/auth.ts | 13 +++---------- packages/api/src/routes/users.ts | 5 +---- packages/shared/src/types.ts | 6 +----- packages/shared/src/utils/pagination.ts | 17 ++++++++++++++--- tsconfig.json | 1 + 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/packages/api/src/middleware/auth.ts b/packages/api/src/middleware/auth.ts index dde32d9..65f642f 100644 --- a/packages/api/src/middleware/auth.ts +++ b/packages/api/src/middleware/auth.ts @@ -6,18 +6,11 @@ import type { MiddlewareHandler } from "hono" * Policy: * 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. */ -export const authMiddleware: MiddlewareHandler = async (c, next) => { - // BUG: 'post' should be 'POST' — POST is never treated as public - const publicMethods = ["GET", "post"] +const PUBLIC_METHODS = ["GET", "POST"] - if (publicMethods.includes(c.req.method)) { +export const authMiddleware: MiddlewareHandler = async (c, next) => { + if (PUBLIC_METHODS.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..d6273ba 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() 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..76f8ff6 100644 --- a/packages/shared/src/utils/pagination.ts +++ b/packages/shared/src/utils/pagination.ts @@ -7,9 +7,20 @@ import type { PaginatedResponse } from "../types" * @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. + * Out-of-range pages return an empty `data` array rather than throwing. */ export function paginate(items: T[], page: number, size: number): PaginatedResponse { - throw new Error("not implemented") + const pageSize = Math.max(1, Math.trunc(size)) + const currentPage = Math.max(1, Math.trunc(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"] }