Skip to content

feat: add getBasePath() public method#4766

Open
G4brym wants to merge 1 commit intohonojs:mainfrom
G4brym:feat/get-base-path
Open

feat: add getBasePath() public method#4766
G4brym wants to merge 1 commit intohonojs:mainfrom
G4brym:feat/get-base-path

Conversation

@G4brym
Copy link

@G4brym G4brym commented Feb 24, 2026

Summary

Adds a getBasePath() public method to the Hono class, exposing the current base path of a Hono instance without relying on the private _basePath property.

Motivation

Currently, the only way to read a Hono instance's base path at runtime is by accessing the private _basePath property. This is fragile — it's 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 _basePath defensively with runtime type guards (see PR), but a proper public API would be much better.

Changes

  • src/hono-base.ts — Added getBasePath(): string method on HonoBase that returns the current _basePath value
  • src/hono.test.ts — Added 4 tests:
    • Returns "/" for a fresh instance
    • Returns the base path after basePath()
    • Returns the merged path after chained basePath() calls
    • Original instance is unaffected after basePath() creates a clone

Usage

const app = new Hono().basePath('/api')
app.getBasePath() // '/api'

const v1 = app.basePath('/v1')
v1.getBasePath()  // '/api/v1'
app.getBasePath()  // '/api' (unchanged)

All 205 tests in src/hono.test.ts pass.


This PR was generated with AI assistance (OpenCode).

@G4brym G4brym marked this pull request as draft February 24, 2026 12:18
@G4brym G4brym force-pushed the feat/get-base-path branch from 91b7f8f to b148db4 Compare February 24, 2026 12:21
@G4brym G4brym force-pushed the feat/get-base-path branch from b148db4 to ebf9231 Compare February 24, 2026 12:24
@G4brym G4brym marked this pull request as ready for review February 24, 2026 12:24
@yusukebe
Copy link
Member

yusukebe commented Mar 2, 2026

Hey @G4brym

If you want to get a base path in a handler, you can use the basePath() in the Route Helper: https://hono.dev/docs/helpers/route#basepath

Have you checked it?

@G4brym
Copy link
Author

G4brym commented Mar 2, 2026

Hey @yusukebe, thanks for the pointer!
I checked that, but it requires a Context (c) and only works inside a request handler
my use case is different, I need the base path at setup time, outside of a request

for example:

const app = new Hono().basePath('/api')                                                                                                                                                                                                                                                                              
const router = fromHono(app)  // ← need to know "/api" here, no `c` exists yet 

@G4brym
Copy link
Author

G4brym commented Mar 2, 2026

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

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.

2 participants