feat: extend basePath() helper to accept Hono instances#4766
feat: extend basePath() helper to accept Hono instances#4766G4brym wants to merge 1 commit intohonojs:mainfrom
Conversation
b148db4 to
ebf9231
Compare
|
Hey @G4brym If you want to get a base path in a handler, you can use the Have you checked it? |
|
Hey @yusukebe, thanks for the pointer! for example: const app = new Hono().basePath('/api')
const router = fromHono(app) // ← need to know "/api" here, no `c` exists yet |
|
or maybe the basePath helper function could be updated to also work with Hono apps, like: import {
basePath,
} from 'hono/route'
const subApp = new Hono()
const path = basePath(subApp) // '/api'
app.route('/:sub', subApp)i can update the pr if you agree with this change |
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ebf9231 to
7a32c2d
Compare
|
Hey there, just updated the pr to match the comment above |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4766 +/- ##
==========================================
+ Coverage 91.48% 92.89% +1.41%
==========================================
Files 177 177
Lines 11551 11800 +249
Branches 3353 3517 +164
==========================================
+ Hits 10567 10962 +395
+ Misses 983 837 -146
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
Extends the existing
basePath()helper inhono/routeto also accept aHonoinstance, allowing libraries to read a Hono app's base path at setup time — without relying on the private_basePathproperty.Motivation
Currently, the only way to read a Hono instance's base path outside of a request handler is by accessing the private
_basePathproperty. This is fragile — it is not part of the public API and could break without notice.Libraries that integrate with Hono (such as chanfana, an OpenAPI schema generator) need to detect the base path to correctly generate API documentation and route schemas. Right now, chanfana reads
_basePathdefensively with runtime type guards (see PR), but a proper public API would be much better.Changes
src/helper/route/index.ts— Added an overload tobasePath()that accepts aHonoBaseinstance and returns its base path directlysrc/helper/route/index.test.ts— Added 4 tests for the new overload:"/"for a fresh instancebasePath()basePath()callsbasePath()creates a cloneUsage
The existing
basePath(c: Context)handler behaviour is fully preserved.