Skip to content

Commit 3a9e0a2

Browse files
fix: apply request macros to every incoming request in enhanceRequest
`requestMacroRegistry` defines `bearerToken()`, `input()`, `has()`, `missing()`, `filled()`, `only()`, `except()`, `wantsJson()`, `isAjax()`, `ip()`, `userAgent()`, `header()`, etc. — but `applyMacros()` was never called from the live request flow. Macros only attached when callers manually invoked `RequestHelpers.withMacros(req)` or `RequestHelpers.create(...)`. Effect: middleware and handlers calling `request.bearerToken()` etc. blew up with `TypeError: request.bearerToken is not a function` because the macros weren't on the request. Reproduced with the Stacks `Auth` middleware pulling the bearer token off an authenticated request. Fix: call `RequestWithMacros.applyMacros(enhancedReq)` inside `enhanceRequest` right before the cookie utility is attached. Macros run first; the cookie utility object then overrides the `cookies` macro (the macro returns a plain record while consumers expect the get/set/delete object form). All 359 existing tests pass.
1 parent ca2fbdb commit 3a9e0a2

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

packages/bun-router/src/router/server.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Server } from 'bun'
22
import type { EnhancedRequest, HTTPMethod, ServerOptions } from '../types'
33
import type { Router } from './router'
4+
import { RequestWithMacros } from '../request/macros'
45
import { runWithRequest, setCurrentRequest } from '../request/context'
56

67
/**
@@ -333,7 +334,13 @@ export function registerServerHandling(RouterClass: typeof Router): void {
333334
_cookiesToDelete: [],
334335
}) as unknown as EnhancedRequest
335336

336-
// Add cookie methods to the request
337+
// Attach registered request macros (input, has, bearerToken, etc.) so
338+
// they're available in middleware and handlers without manual setup.
339+
// Applied *before* the cookie utility so the cookies macro doesn't
340+
// overwrite the get/set/delete object form consumers depend on.
341+
RequestWithMacros.applyMacros(enhancedReq)
342+
343+
// Add cookie methods to the request (overrides any macro named `cookies`).
337344
Object.assign(enhancedReq, { cookies: { ...getCookies(), ...cookies } })
338345

339346
return enhancedReq

0 commit comments

Comments
 (0)