Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support _middleware.ts for deeply nested param routes #230

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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