Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4349735

Browse files
fix(middleware/combine): preventc.req.routeIndex from being changed (#3663)
* test(middleware/combine): add test for every middlewareCo-authored-by: Paweł Dąbrowski <dabrowskip9@gmail.com>* refactor(compose): Loosen `compose` parameter typesThe 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>
1 parent7b30835 commit4349735

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

‎src/compose.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import{Context}from'./context'
2-
importtype{ParamIndexMap,Params}from'./router'
32
importtype{Env,ErrorHandler,NotFoundHandler}from'./types'
43

54
/**
@@ -31,7 +30,7 @@ interface ComposeContext {
3130
*@returns {(context: C, next?: Function) => Promise<C>} - A composed middleware function.
3231
*/
3332
exportconstcompose=<CextendsComposeContext,EextendsEnv=Env>(
34-
middleware:[[Function,unknown],ParamIndexMap|Params][],
33+
middleware:[[Function,unknown],unknown][]|[[Function]][],
3534
onError?:ErrorHandler<E>,
3635
onNotFound?:NotFoundHandler<E>
3736
):((context:C,next?:Function)=>Promise<C>)=>{

‎src/middleware/combine/index.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ describe('every', () => {
166166
expect(awaitres.text()).toBe('Hello Middleware 1')
167167
expect(middleware2).not.toBeCalled()
168168
})
169+
170+
it('Should pass the path params to middlewares',async()=>{
171+
constapp=newHono()
172+
app.use('*',nextMiddleware)
173+
constparamMiddleware:MiddlewareHandler=async(c)=>{
174+
returnc.json(c.req.param(),200)
175+
}
176+
177+
app.use('/:id',every(paramMiddleware))
178+
app.get('/:id',(c)=>{
179+
returnc.text('Hello World')
180+
})
181+
182+
constres=awaitapp.request('http://localhost/123')
183+
expect(awaitres.json()).toEqual({id:'123'})
184+
})
169185
})
170186

171187
describe('except',()=>{

‎src/middleware/combine/index.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,22 @@ export const some = (...middleware: (MiddlewareHandler | Condition)[]): Middlewa
8989
* ```
9090
*/
9191
exportconstevery=(...middleware:(MiddlewareHandler|Condition)[]):MiddlewareHandler=>{
92-
constwrappedMiddleware=middleware.map((m)=>async(c:Context,next:Next)=>{
93-
constres=awaitm(c,next)
94-
if(res===false){
95-
thrownewError('Unmet condition')
96-
}
97-
returnres
98-
})
99-
100-
consthandler=async(c:Context,next:Next)=>
101-
compose<Context>(wrappedMiddleware.map((m)=>[[m,undefined],c.req.param()]))(c,next)
102-
10392
returnasyncfunctionevery(c,next){
104-
awaithandler(c,next)
93+
constcurrentRouteIndex=c.req.routeIndex
94+
awaitcompose<Context>(
95+
middleware.map((m)=>[
96+
[
97+
async(c:Context,next:Next)=>{
98+
c.req.routeIndex=currentRouteIndex// should be unchanged in this context
99+
constres=awaitm(c,next)
100+
if(res===false){
101+
thrownewError('Unmet condition')
102+
}
103+
returnres
104+
},
105+
],
106+
])
107+
)(c,next)
105108
}
106109
}
107110

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp