Skip to content

Commit ebf9231

Browse files
committed
feat: add getBasePath() public method to expose current base path
1 parent b1df304 commit ebf9231

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/hono-base.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,23 @@ class Hono<
252252
return subApp
253253
}
254254

255+
/**
256+
* `.getBasePath()` returns the current base path of the Hono instance.
257+
*
258+
* @see {@link https://hono.dev/docs/api/routing#base-path}
259+
*
260+
* @returns {string} the current base path
261+
*
262+
* @example
263+
* ```ts
264+
* const app = new Hono().basePath('/api')
265+
* app.getBasePath() // returns '/api'
266+
* ```
267+
*/
268+
getBasePath(): string {
269+
return this._basePath
270+
}
271+
255272
/**
256273
* `.onError()` handles an error and returns a customized Response.
257274
*

src/hono.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3658,6 +3658,30 @@ describe('app.basePath() with the internal #clone()', () => {
36583658
})
36593659
})
36603660

3661+
describe('app.getBasePath()', () => {
3662+
it('Should return "/" for a fresh Hono instance', () => {
3663+
const app = new Hono()
3664+
expect(app.getBasePath()).toBe('/')
3665+
})
3666+
3667+
it('Should return the base path after basePath()', () => {
3668+
const app = new Hono().basePath('/api')
3669+
expect(app.getBasePath()).toBe('/api')
3670+
})
3671+
3672+
it('Should return the merged path after chained basePath() calls', () => {
3673+
const app = new Hono().basePath('/api').basePath('/v1')
3674+
expect(app.getBasePath()).toBe('/api/v1')
3675+
})
3676+
3677+
it('Should not affect the original instance', () => {
3678+
const app = new Hono()
3679+
const based = app.basePath('/api')
3680+
expect(app.getBasePath()).toBe('/')
3681+
expect(based.getBasePath()).toBe('/api')
3682+
})
3683+
})
3684+
36613685
describe('Catch-all route with empty segment', () => {
36623686
it('Should return empty string for empty catch-all param', async () => {
36633687
const app = new Hono()

0 commit comments

Comments
 (0)