From fe0a82a615dd8893e7f6b9e708e0c0cb3394f4a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20D=C4=85browski?= Date: Tue, 24 Sep 2024 04:58:39 +0200 Subject: [PATCH] fix(combine/every): make `every` middleware work with short-circuiting middlewares (#3441) --- src/middleware/combine/index.test.ts | 16 ++++++++++++++++ src/middleware/combine/index.ts | 1 + 2 files changed, 17 insertions(+) diff --git a/src/middleware/combine/index.test.ts b/src/middleware/combine/index.test.ts index 47944d02..fbeea63f 100644 --- a/src/middleware/combine/index.test.ts +++ b/src/middleware/combine/index.test.ts @@ -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', () => { diff --git a/src/middleware/combine/index.ts b/src/middleware/combine/index.ts index 1e7c703e..3f8dee81 100644 --- a/src/middleware/combine/index.ts +++ b/src/middleware/combine/index.ts @@ -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) =>