From 4d88c79839148b8b89a16dd90afa81919e203ee0 Mon Sep 17 00:00:00 2001 From: dragontaek-lee Date: Sat, 25 May 2024 23:07:44 +0900 Subject: [PATCH] fix(core): prevent exclude method from overwriting previous calls --- .../hello-world/e2e/exclude-middleware.spec.ts | 12 ++++++++++++ packages/core/middleware/builder.ts | 10 +++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/integration/hello-world/e2e/exclude-middleware.spec.ts b/integration/hello-world/e2e/exclude-middleware.spec.ts index 1bc6f3df2fe..3fad028d09b 100644 --- a/integration/hello-world/e2e/exclude-middleware.spec.ts +++ b/integration/hello-world/e2e/exclude-middleware.spec.ts @@ -45,6 +45,11 @@ class TestController { overviewById() { return RETURN_VALUE; } + + @Get('multiple/exclude') + multipleExclude() { + return RETURN_VALUE; + } } @Module({ @@ -59,6 +64,7 @@ class TestModule { path: 'middleware', method: RequestMethod.POST, }) + .exclude('multiple/exclude') .forRoutes('*'); } } @@ -110,6 +116,12 @@ describe('Exclude middleware', () => { .expect(200, RETURN_VALUE); }); + it(`should exclude "/multiple/exclude" endpoint`, () => { + return request(app.getHttpServer()) + .get('/multiple/exclude') + .expect(200, RETURN_VALUE); + }); + afterEach(async () => { await app.close(); }); diff --git a/packages/core/middleware/builder.ts b/packages/core/middleware/builder.ts index 40301577a51..773d97f328d 100644 --- a/packages/core/middleware/builder.ts +++ b/packages/core/middleware/builder.ts @@ -58,8 +58,9 @@ export class MiddlewareBuilder implements MiddlewareConsumer { public exclude( ...routes: Array ): MiddlewareConfigProxy { - this.excludedRoutes = this.getRoutesFlatList(routes).reduce( - (excludedRoutes, route) => { + this.excludedRoutes = [ + ...this.excludedRoutes, + ...this.getRoutesFlatList(routes).reduce((excludedRoutes, route) => { for (const routePath of this.routeInfoPathExtractor.extractPathFrom( route, )) { @@ -70,9 +71,8 @@ export class MiddlewareBuilder implements MiddlewareConsumer { } return excludedRoutes; - }, - [] as RouteInfo[], - ); + }, [] as RouteInfo[]), + ]; return this; }