Skip to content
Myr

fix(route): preserve the base path of the mounted route() app (#4942)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Taku Amano · committed by GitHub · parent 7e62bcd · Browse files · 3 files+82 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+82 added−3 removed

  • tests:+73 added−0 removed
  • other:+9 added−3 removed

3 files+82 added−3 removed

modified:src/helper/route/index.test.ts+21 added
1import { Context } from '../../context'2import { Hono } from '../../hono'3import { matchedRoutes, routePath, baseRoutePath, basePath } from '.'45const defaultContextOptions = {6 executionCtx: {
183 unmodified lines
190191 expect(basePath(c)).toBe('/sub-app-path/foo/app')192 })193194 it('should return the fully expanded basePath for nested route() apps', async () => {195 const app = new Hono()196 const sub1 = new Hono()197 const sub2 = new Hono()198 const sub3 = new Hono()199200 sub3.get('/assets/*', (c) => c.json({ baseRoutePath: baseRoutePath(c), basePath: basePath(c) }))201 sub2.route('/:app/sub3/assets', sub3)202 sub1.route('/:app/sub2/assets', sub2)203 app.route('/:app/sub1/assets', sub1)204205 const res = await app.request(206 '/s1/sub1/assets/ss2/sub2/assets/sss3/sub3/assets/assets/file-hoge/assets-kage.js'207 )208 expect(await res.json()).toEqual({209 baseRoutePath: '/:app/sub1/assets/:app/sub2/assets/:app/sub3/assets',210 basePath: '/s1/sub1/assets/ss2/sub2/assets/sss3/sub3/assets',211 })212 })213214 it('should return basePath with custom regex pattern', () => {215 const handlerA = () => {}216 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')217 const c = new Context(rawRequest, {
23 unmodified lines
modified:src/hono-base.ts+9 added-3 removed
224 unmodified lines
225 (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res226 ;(handler as any)[COMPOSED_HANDLER] = r.handler227 }228229 subApp.#addRoute(r.method, r.path, handler)229 subApp.#addRoute(r.method, r.path, handler, r.basePath)230 })231 return this232 }233
147 unmodified lines
381 this.#addRoute(METHOD_NAME_ALL, mergePath(path, '*'), handler)382 return this383 }384385 #addRoute(method: string, path: string, handler: H): void {385 #addRoute(method: string, path: string, handler: H, baseRoutePath?: string): void {386 method = method.toUpperCase()387 path = mergePath(this._basePath, path)388 const r: RouterRoute = { basePath: this._basePath, path, method, handler }388 const r: RouterRoute = {389 basePath:390 baseRoutePath !== undefined ? mergePath(this._basePath, baseRoutePath) : this._basePath,391 path,392 method,393 handler,394 }395 this.router.add(method, path, [handler, r])396 this.routes.push(r)397 }398
147 unmodified lines
modified:src/hono.test.ts+52 added
482 unmodified lines
483 expect(res.status).toBe(200)484 expect(await res.text()).toBe('get /book')485 })486487 describe('Nested route - basePath of the mounted routes', () => {488 it('Should set basePath to the mount path', () => {489 const app = new Hono()490 const sub = new Hono()491 sub.get('/posts/:id', (c) => c.text('post'))492 app.route('/:sub', sub)493494 expect(app.routes).toEqual([495 {496 basePath: '/:sub',497 method: 'GET',498 path: '/:sub/posts/:id',499 handler: expect.any(Function),500 },501 ])502 })503504 it('Should accumulate basePath through nested route() calls', () => {505 const app = new Hono()506 const sub1 = new Hono()507 const sub2 = new Hono()508 sub2.get('/posts/:id', (c) => c.text('post'))509 sub1.route('/:sub2', sub2)510 app.route('/:sub1', sub1)511512 expect(app.routes).toEqual([513 {514 basePath: '/:sub1/:sub2',515 method: 'GET',516 path: '/:sub1/:sub2/posts/:id',517 handler: expect.any(Function),518 },519 ])520 })521522 it('Should merge the basePath of a subApp created with basePath()', () => {523 const app = new Hono()524 const sub = new Hono().basePath('/book')525 sub.get('/:id', (c) => c.text('book'))526 app.route('/api', sub)527528 expect(app.routes).toEqual([529 {530 basePath: '/api/book',531 method: 'GET',532 path: '/api/book/:id',533 handler: expect.any(Function),534 },535 ])536 })537 })538539 it('Multiple route', async () => {540 const app = new Hono()541542 const book = new Hono()
3183 unmodified lines