Skip to content

feat(route): add handlerPath helper to get actual route handler path#4825

Open
GourangaDasSamrat wants to merge 1 commit intohonojs:mainfrom
GourangaDasSamrat:feat/add-handler-path-helper
Open

feat(route): add handlerPath helper to get actual route handler path#4825
GourangaDasSamrat wants to merge 1 commit intohonojs:mainfrom
GourangaDasSamrat:feat/add-handler-path-helper

Conversation

@GourangaDasSamrat
Copy link
Copy Markdown

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Adds a new handlerPath(c) helper to hono/route that returns the path of the actual route handler, unaffected by middleware registration order.

Problem: routePath(c, -1) breaks when middleware is registered after a route handler. Because Hono matches all routes up-front, the last entry in matchedRoutes becomes the trailing middleware (/*), not the actual handler.

app.use(async (c, next) => {
  console.log(routePath(c, -1)) // '/*' ← wrong
  await next()
})
app.get('/health', (c) => c.json({ status: 'ok' }))
app.use(async (c, next) => { await next() }) // causes the issue

Fix: handlerPath(c) finds the first matched route whose method is not ALL (i.e. not registered via app.use()), giving the correct handler path every time.

app.use(async (c, next) => {
  console.log(handlerPath(c)) // '/health' ✓
  await next()
})
app.get('/health', (c) => c.json({ status: 'ok' }))
app.use(async (c, next) => { await next() })

No breaking changes — all existing helpers are unchanged.

Closes #4609

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Register middleware after routing, routePath(c, -1) are error

1 participant