Skip to content

Commit

Permalink
fix(combine/every): make every middleware work with short-circuitin…
Browse files Browse the repository at this point in the history
…g middlewares (#3441)
  • Loading branch information
paolostyle authored Sep 24, 2024
1 parent ad0bba0 commit fe0a82a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/middleware/combine/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ describe('every', () => {
expect(await res.text()).toBe('oops')
expect(middleware2).not.toBeCalled()
})

it('Should return the same response a middleware returns if it short-circuits the chain', async () => {
const middleware1: MiddlewareHandler = async (c) => {
return c.text('Hello Middleware 1')
}
const middleware2 = vi.fn(nextMiddleware)

app.use('/', every(middleware1, middleware2))
app.get('/', (c) => {
return c.text('Hello World')
})
const res = await app.request('http://localhost/')

expect(await res.text()).toBe('Hello Middleware 1')
expect(middleware2).not.toBeCalled()
})
})

describe('except', () => {
Expand Down
1 change: 1 addition & 0 deletions src/middleware/combine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const every = (...middleware: (MiddlewareHandler | Condition)[]): Middlew
if (res === false) {
throw new Error('Unmet condition')
}
return res
})

const handler = async (c: Context, next: Next) =>
Expand Down

0 comments on commit fe0a82a

Please sign in to comment.