From 1ae72e1fdb338a6e00ac0ca19615602a91edc5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Mon, 14 Mar 2022 14:43:03 +0100 Subject: [PATCH] fix(core): apply global middleware to routes excluded from prefix --- .../global-prefix/e2e/global-prefix.spec.ts | 8 ++++---- .../nest-application/global-prefix/src/app.controller.ts | 6 +++--- .../nest-application/global-prefix/src/app.module.ts | 7 ++++++- packages/core/middleware/middleware-module.ts | 7 ++++--- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/integration/nest-application/global-prefix/e2e/global-prefix.spec.ts b/integration/nest-application/global-prefix/e2e/global-prefix.spec.ts index b11becd24ca..131d2dd0343 100644 --- a/integration/nest-application/global-prefix/e2e/global-prefix.spec.ts +++ b/integration/nest-application/global-prefix/e2e/global-prefix.spec.ts @@ -108,15 +108,15 @@ describe('Global prefix', () => { server = app.getHttpServer(); await app.init(); - await request(server).get('/hello/foo').expect(200); + await request(server) + .get('/hello/foo') + .expect(200, 'Hello: Data attached in middleware'); await request(server) .get('/middleware/foo') .expect(200, MIDDLEWARE_PARAM_VALUE); - await request(server) - .get('/api/v1/middleware/foo') - .expect(404); + await request(server).get('/api/v1/middleware/foo').expect(404); }); afterEach(async () => { diff --git a/integration/nest-application/global-prefix/src/app.controller.ts b/integration/nest-application/global-prefix/src/app.controller.ts index 8d1472a2fda..912c0d76cc9 100644 --- a/integration/nest-application/global-prefix/src/app.controller.ts +++ b/integration/nest-application/global-prefix/src/app.controller.ts @@ -1,10 +1,10 @@ -import { Controller, Get, Post } from '@nestjs/common'; +import { Controller, Get, Post, Req } from '@nestjs/common'; @Controller() export class AppController { @Get('hello/:name') - getHello(): string { - return 'hello'; + getHello(@Req() req): string { + return 'Hello: ' + req.extras?.data; } @Get('health') diff --git a/integration/nest-application/global-prefix/src/app.module.ts b/integration/nest-application/global-prefix/src/app.module.ts index 2123145a8f3..85d1e6ac481 100644 --- a/integration/nest-application/global-prefix/src/app.module.ts +++ b/integration/nest-application/global-prefix/src/app.module.ts @@ -17,6 +17,11 @@ export class AppModule { .apply((req, res, next) => res.end(MIDDLEWARE_PARAM_VALUE)) .forRoutes({ path: MIDDLEWARE_VALUE + '/*', method: RequestMethod.GET }) .apply((req, res, next) => res.status(201).end(MIDDLEWARE_PARAM_VALUE)) - .forRoutes({ path: MIDDLEWARE_VALUE + '/*', method: RequestMethod.POST }); + .forRoutes({ path: MIDDLEWARE_VALUE + '/*', method: RequestMethod.POST }) + .apply((req, res, next) => { + req.extras = { data: 'Data attached in middleware' }; + next(); + }) + .forRoutes({ path: '*', method: RequestMethod.GET }); } } diff --git a/packages/core/middleware/middleware-module.ts b/packages/core/middleware/middleware-module.ts index 748887811cc..ecb98e1b598 100644 --- a/packages/core/middleware/middleware-module.ts +++ b/packages/core/middleware/middleware-module.ts @@ -9,7 +9,6 @@ import { addLeadingSlash, isUndefined, } from '@nestjs/common/utils/shared.utils'; -import { isRouteExcluded, isRequestMethodAll } from '../router/utils'; import { ApplicationConfig } from '../application-config'; import { InvalidMiddlewareException } from '../errors/exceptions/invalid-middleware.exception'; import { RuntimeException } from '../errors/exceptions/runtime.exception'; @@ -23,6 +22,7 @@ import { InstanceToken, Module } from '../injector/module'; import { REQUEST_CONTEXT_ID } from '../router/request/request-constants'; import { RouterExceptionFilters } from '../router/router-exception-filters'; import { RouterProxy } from '../router/router-proxy'; +import { isRequestMethodAll, isRouteExcluded } from '../router/utils'; import { MiddlewareBuilder } from './builder'; import { MiddlewareContainer } from './container'; import { MiddlewareResolver } from './resolver'; @@ -274,8 +274,9 @@ export class MiddlewareModule { const prefix = this.config.getGlobalPrefix(); const excludedRoutes = this.config.getGlobalPrefixOptions().exclude; if ( - Array.isArray(excludedRoutes) && - isRouteExcluded(excludedRoutes, path, method) + (Array.isArray(excludedRoutes) && + isRouteExcluded(excludedRoutes, path, method)) || + ['*', '/*', '(.*)', '/(.*)'].includes(path) ) { path = addLeadingSlash(path); } else {