File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,20 +4,13 @@ import type { MiddlewareHandler } from "hono"
44 * Simple token-based auth middleware.
55 *
66 * Policy:
7- * GET, POST → public (no token required)
7+ * 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 */
16- export const authMiddleware : MiddlewareHandler = async ( c , next ) => {
17- // BUG: 'post' should be 'POST' — POST is never treated as public
18- const publicMethods = [ "GET" , "post" ]
10+ const PUBLIC_METHODS = [ "GET" , "POST" ]
1911
20- if ( publicMethods . includes ( c . req . method ) ) {
12+ export const authMiddleware : MiddlewareHandler = async ( c , next ) => {
13+ if ( PUBLIC_METHODS . includes ( c . req . method . toUpperCase ( ) ) ) {
2114 return next ( )
2215 }
2316
Original file line number Diff line number Diff line change 11import { Hono } from "hono"
22import { 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
85const router = new Hono ( )
96
@@ -20,7 +17,6 @@ router.get("/:id", (c) => {
2017router . 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 } )
Original file line number Diff line number Diff line change 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
95export 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}
Original file line number Diff line number Diff line change @@ -7,9 +7,21 @@ import type { PaginatedResponse } from "../types"
77 * @param page 1-indexed page number
88 * @param size Number of items per page
99 *
10- * TODO: implement this function — it is currently a stub.
11- * The test in packages/shared/test/pagination.test.ts exercises the full contract .
10+ * Out-of-range pages yield an empty `data` array rather than throwing, so
11+ * callers can safely pass through user-supplied page numbers .
1212 */
1313export function paginate < T > ( items : T [ ] , page : number , size : number ) : PaginatedResponse < T > {
14- throw new Error ( "not implemented" )
14+ const pageSize = Math . max ( 1 , Math . trunc ( size ) )
15+ const currentPage = Math . max ( 1 , Math . trunc ( page ) )
16+ const total = items . length
17+ const totalPages = Math . ceil ( total / pageSize )
18+ const start = ( currentPage - 1 ) * pageSize
19+
20+ return {
21+ data : items . slice ( start , start + pageSize ) ,
22+ page : currentPage ,
23+ pageSize,
24+ total,
25+ totalPages,
26+ }
1527}
Original file line number Diff line number Diff line change 55 "moduleResolution" : " bundler" ,
66 "strict" : true ,
77 "skipLibCheck" : true ,
8+ "types" : [" bun-types" ],
89 "paths" : {
910 "@e2e/shared" : [" ./packages/shared/src/index.ts" ]
1011 }
You can’t perform that action at this time.
0 commit comments