Skip to content

Commit

Permalink
fix: support _middleware.ts for deeply nested param routes (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Oct 5, 2024
1 parent 6860e22 commit aa4a1cb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
1 change: 0 additions & 1 deletion mocks/app/routes/about/[name]/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createRoute } from '../../../../../src/factory'

const addHeader = createMiddleware(async (c, next) => {
await next()
console.log('fooo')
c.res.headers.append('x-message', 'from middleware')
})

Expand Down
2 changes: 1 addition & 1 deletion mocks/app/routes/about/[name]/_renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default jsxRenderer(({ children, title }) => {
</head>
<body>
<h1>About</h1>
<address>{children}</address>
<div>{children}</div>
</body>
</html>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createMiddleware } from 'hono/factory'
import { createRoute } from '../../../../../../../src/factory'

const addHeader = createMiddleware(async (c, next) => {
await next()
c.res.headers.append('x-message-nested', 'from nested middleware')
})

export default createRoute(addHeader)
10 changes: 10 additions & 0 deletions mocks/app/routes/about/[name]/hobbies/[hobby_name]/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createRoute } from '../../../../../../../src/factory'

export default createRoute((c) => {
const { name, hobby_name } = c.req.param()
return c.render(
<p>
{name}'s hobby is {hobby_name}
</p>
)
})
2 changes: 1 addition & 1 deletion src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const createApp = <E extends Env>(options: BaseServerOptions<E>): Hono<E>
})

const middlewareFile = Object.keys(MIDDLEWARE_FILE).find((x) => {
const replacedDir = dir.replace('[', '\\[').replace(']', '\\]')
const replacedDir = dir.replaceAll('[', '\\[').replace(']', '\\]')
return new RegExp(replacedDir + '/_middleware.tsx?').test(x)
})

Expand Down
15 changes: 13 additions & 2 deletions test-integration/apps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('Basic', () => {
method: 'GET',
},
]
expect(app.routes).toHaveLength(routes.length * 2)
expect(app.routes).toHaveLength(46)
expect(app.routes).toEqual(
expect.arrayContaining(
routes.map(({ path, method }) => {
Expand Down Expand Up @@ -224,7 +224,18 @@ describe('With preserved', () => {
expect(res.headers.get('x-message')).toBe('from middleware')
// hono/jsx escape a single quote to &#39;
expect(await res.text()).toBe(
'<!DOCTYPE html><html><head><title>me&#39;s address</title></head><body><h1>About</h1><address><b>me&#39;s address</b></address></body></html>'
'<!DOCTYPE html><html><head><title>me&#39;s address</title></head><body><h1>About</h1><div><b>me&#39;s address</b></div></body></html>'
)
})

it('Should return 200 response - /about/me/hobbies/baseball', async () => {
const res = await app.request('/about/me/hobbies/baseball')
expect(res.status).toBe(200)
expect(res.headers.get('x-message')).toBe('from middleware')
expect(res.headers.get('x-message-nested')).toBe('from nested middleware')
// hono/jsx escape a single quote to &#39;
expect(await res.text()).toBe(
'<!DOCTYPE html><html><head><title></title></head><body><h1>About</h1><div><p>me&#39;s hobby is baseball</p></div></body></html>'
)
})

Expand Down

0 comments on commit aa4a1cb

Please sign in to comment.