Skip to content
Myr

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

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

* 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 ---------

authored by Taku Amano · committed by GitHub · parent 7b30835 · Browse files · 3 files+32 added−14 removed
This commit

State

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

Trailers

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

Diff stats

3 files+32 added−14 removed

  • tests:+16 added−0 removed
  • other:+16 added−14 removed

3 files+32 added−14 removed

modified:src/middleware/combine/index.test.ts+16 added
164 unmodified lines
165166 expect(await res.text()).toBe('Hello Middleware 1')167 expect(middleware2).not.toBeCalled()168 })169170 it('Should pass the path params to middlewares', async () => {171 const app = new Hono()172 app.use('*', nextMiddleware)173 const paramMiddleware: MiddlewareHandler = async (c) => {174 return c.json(c.req.param(), 200)175 }176177 app.use('/:id', every(paramMiddleware))178 app.get('/:id', (c) => {179 return c.text('Hello World')180 })181182 const res = await app.request('http://localhost/123')183 expect(await res.json()).toEqual({ id: '123' })184 })185})186187describe('except', () => {188 let app: Hono
108 unmodified lines
modified:src/middleware/combine/index.ts+15 added-12 removed
modified:src/compose.ts+1 added-2 removed
1import { Context } from './context'2import type { ParamIndexMap, Params } from './router'2import type { Env, ErrorHandler, NotFoundHandler } from './types'34/**5 * Interface representing the context for a composition operation.
23 unmodified lines
29 *30 * @returns {(context: C, next?: Function) => Promise<C>} - A composed middleware function.31 */32export const compose = <C extends ComposeContext, E extends Env = Env>(34 middleware: [[Function, unknown], ParamIndexMap | Params][],33 middleware: [[Function, unknown], unknown][] | [[Function]][],34 onError?: ErrorHandler<E>,35 onNotFound?: NotFoundHandler<E>36): ((context: C, next?: Function) => Promise<C>) => {37 return (context, next) => {
58 unmodified lines