0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

fix(middleware/combine): prevent c.req.routeIndex from being changed (#3663)

* test(middleware/combine): add test for every middleware

Co-authored-by: Paweł Dąbrowski <dabrowskip9@gmail.com>

* refactor(compose): Loosen `compose` parameter types

The current implementation of `compose` does not use some elements of the received middleware,
so they do not have to be passed on.

* fix(middleware/combine): prevent `c.req.routeIndex` from being changed

---------

Co-authored-by: Paweł Dąbrowski <dabrowskip9@gmail.com>
This commit is contained in:
Taku Amano 2024-11-13 17:31:06 +09:00 committed by GitHub
parent 7b308358f4
commit 4349735823
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 14 deletions

View File

@ -1,5 +1,4 @@
import { Context } from './context'
import type { ParamIndexMap, Params } from './router'
import type { Env, ErrorHandler, NotFoundHandler } from './types'
/**
@ -31,7 +30,7 @@ interface ComposeContext {
* @returns {(context: C, next?: Function) => Promise<C>} - A composed middleware function.
*/
export const compose = <C extends ComposeContext, E extends Env = Env>(
middleware: [[Function, unknown], ParamIndexMap | Params][],
middleware: [[Function, unknown], unknown][] | [[Function]][],
onError?: ErrorHandler<E>,
onNotFound?: NotFoundHandler<E>
): ((context: C, next?: Function) => Promise<C>) => {

View File

@ -166,6 +166,22 @@ describe('every', () => {
expect(await res.text()).toBe('Hello Middleware 1')
expect(middleware2).not.toBeCalled()
})
it('Should pass the path params to middlewares', async () => {
const app = new Hono()
app.use('*', nextMiddleware)
const paramMiddleware: MiddlewareHandler = async (c) => {
return c.json(c.req.param(), 200)
}
app.use('/:id', every(paramMiddleware))
app.get('/:id', (c) => {
return c.text('Hello World')
})
const res = await app.request('http://localhost/123')
expect(await res.json()).toEqual({ id: '123' })
})
})
describe('except', () => {

View File

@ -89,19 +89,22 @@ export const some = (...middleware: (MiddlewareHandler | Condition)[]): Middlewa
* ```
*/
export const every = (...middleware: (MiddlewareHandler | Condition)[]): MiddlewareHandler => {
const wrappedMiddleware = middleware.map((m) => async (c: Context, next: Next) => {
return async function every(c, next) {
const currentRouteIndex = c.req.routeIndex
await compose<Context>(
middleware.map((m) => [
[
async (c: Context, next: Next) => {
c.req.routeIndex = currentRouteIndex // should be unchanged in this context
const res = await m(c, next)
if (res === false) {
throw new Error('Unmet condition')
}
return res
})
const handler = async (c: Context, next: Next) =>
compose<Context>(wrappedMiddleware.map((m) => [[m, undefined], c.req.param()]))(c, next)
return async function every(c, next) {
await handler(c, next)
},
],
])
)(c, next)
}
}