Skip to content

Commit bd08369

Browse files
author
QuantCode Agent
committed
fix: repair failing tests and type errors across api and shared packages
- auth: fix HTTP method case-sensitivity in public-methods allow-list ("post" -> "POST") so POST requests are correctly treated as public - users route: add missing badRequest import causing ReferenceError/TS2552 - shared types: rename User.userName -> username to match the test contract and align both packages - pagination: implement paginate<T> per its test contract, with clamping/guards for non-finite and out-of-range page/size - tsconfig: add "bun-types" to types so bun:test and process globals resolve
1 parent e77b6fc commit bd08369

5 files changed

Lines changed: 30 additions & 24 deletions

File tree

‎packages/api/src/middleware/auth.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@ import type { MiddlewareHandler } from "hono"
66
* Policy:
77
* GET, POST → public (no token required)
88
* PUT, DELETE, PATCH → require Bearer token
9-
*
10-
* BUG: The allow-list check uses `'post'` (lowercase) instead of `'POST'`.
11-
* HTTP methods are always uppercase per RFC 7231, so POST is never matched
12-
* as a public method — POST requests incorrectly require a token.
13-
*
14-
* Fix: change `'post'` to `'POST'` in the public methods array.
159
*/
1610
export const authMiddleware: MiddlewareHandler = async (c, next) => {
17-
// BUG: 'post' should be 'POST' — POST is never treated as public
18-
const publicMethods = ["GET", "post"]
11+
const publicMethods = ["GET", "POST"]
1912

2013
if (publicMethods.includes(c.req.method)) {
2114
return next()

‎packages/api/src/routes/users.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Hono } from "hono"
22
import { db } from "../lib/db"
3-
import { notFound } from "../lib/errors"
4-
// BUG: missing import — `badRequest` is used below but not imported here.
5-
// This causes a ReferenceError at runtime when POST /users is called with invalid data.
6-
// Fix: add `badRequest` to the import from "../lib/errors"
3+
import { notFound, badRequest } from "../lib/errors"
74

85
const router = new Hono()
96

@@ -20,7 +17,6 @@ router.get("/:id", (c) => {
2017
router.post("/", async (c) => {
2118
const body = await c.req.json().catch(() => null)
2219
if (!body || !body.username || !body.email) {
23-
// BUG: badRequest is not imported — this will throw ReferenceError
2420
return badRequest(c, "username and email are required")
2521
}
2622
const user = db.users.create({ username: body.username, email: body.email })

‎packages/shared/src/types.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
/**
22
* Shared types used by both the API and any consumers.
3-
*
4-
* BUG: The field is named `userName` here but the API routes reference `username`
5-
* (lowercase n). This causes a type error in routes/users.ts and a runtime
6-
* mismatch when serialising responses.
73
*/
84

95
export type User = {
106
id: string
11-
userName: string // BUG: should be `username` to match API usage
7+
username: string
128
email: string
139
createdAt: string
1410
}
Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
import type { PaginatedResponse } from "../types"
22

3+
function toPositiveInt(value: number): number {
4+
if (!Number.isFinite(value)) return 1
5+
const floored = Math.floor(value)
6+
return floored < 1 ? 1 : floored
7+
}
8+
39
/**
410
* Paginate an array of items.
511
*
6-
* @param items Full array of items
7-
* @param page 1-indexed page number
8-
* @param size Number of items per page
12+
* `page` and `size` are truncated to integers and clamped to a minimum of 1.
13+
* Non-finite values (`NaN`, `Infinity`, `-Infinity`) and values below 1 fall
14+
* back to 1, so untrusted input such as `Number(query.page)` can never leak
15+
* `NaN` into the response.
916
*
10-
* TODO: implement this function — it is currently a stub.
11-
* The test in packages/shared/test/pagination.test.ts exercises the full contract.
17+
* @param items Full array of items
18+
* @param page 1-indexed page number; defaults to 1 if non-finite or < 1
19+
* @param size Number of items per page; defaults to 1 if non-finite or < 1
1220
*/
1321
export function paginate<T>(items: T[], page: number, size: number): PaginatedResponse<T> {
14-
throw new Error("not implemented")
22+
const total = items.length
23+
const pageSize = toPositiveInt(size)
24+
const currentPage = toPositiveInt(page)
25+
const totalPages = Math.ceil(total / pageSize)
26+
const start = (currentPage - 1) * pageSize
27+
28+
return {
29+
data: items.slice(start, start + pageSize),
30+
page: currentPage,
31+
pageSize,
32+
total,
33+
totalPages,
34+
}
1535
}

‎tsconfig.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"moduleResolution": "bundler",
66
"strict": true,
77
"skipLibCheck": true,
8+
"types": ["bun-types"],
89
"paths": {
910
"@e2e/shared": ["./packages/shared/src/index.ts"]
1011
}

0 commit comments

Comments
 (0)