1+ import { HttpServer } from '@nestjs/common' ;
12import { RequestMethod } from '@nestjs/common/enums/request-method.enum' ;
2- import { MiddlewareConfiguration } from '@nestjs/common/interfaces/middleware/middleware-configuration.interface' ;
3+ import { MiddlewareConfiguration , RouteInfo } from '@nestjs/common/interfaces/middleware/middleware-configuration.interface' ;
34import { NestMiddleware } from '@nestjs/common/interfaces/middleware/nest-middleware.interface' ;
45import { NestModule } from '@nestjs/common/interfaces/modules/nest-module.interface' ;
56import { Type } from '@nestjs/common/interfaces/type.interface' ;
@@ -8,7 +9,6 @@ import { ApplicationConfig } from '../application-config';
89import { InvalidMiddlewareException } from '../errors/exceptions/invalid-middleware.exception' ;
910import { RuntimeException } from '../errors/exceptions/runtime.exception' ;
1011import { ExceptionsHandler } from '../exceptions/exceptions-handler' ;
11- import { RouterMethodFactory } from '../helpers/router-method-factory' ;
1212import { NestContainer } from '../injector/container' ;
1313import { Module } from '../injector/module' ;
1414import { RouterExceptionFilters } from '../router/router-exception-filters' ;
@@ -20,7 +20,6 @@ import { RoutesMapper } from './routes-mapper';
2020
2121export class MiddlewareModule {
2222 private readonly routerProxy = new RouterProxy ( ) ;
23- private readonly routerMethodFactory = new RouterMethodFactory ( ) ;
2423 private routerExceptionFilter : RouterExceptionFilters ;
2524 private routesMapper : RoutesMapper ;
2625 private resolver : MiddlewareResolver ;
@@ -108,10 +107,10 @@ export class MiddlewareModule {
108107 ) {
109108 const { forRoutes } = config ;
110109 await Promise . all (
111- forRoutes . map ( async ( routePath : string ) => {
110+ forRoutes . map ( async ( routeInfo : RouteInfo ) => {
112111 await this . registerRouteMiddleware (
113112 middlewareContainer ,
114- routePath ,
113+ routeInfo ,
115114 config ,
116115 module ,
117116 applicationRef ,
@@ -122,7 +121,7 @@ export class MiddlewareModule {
122121
123122 public async registerRouteMiddleware (
124123 middlewareContainer : MiddlewareContainer ,
125- routePath : string ,
124+ routeInfo : RouteInfo ,
126125 config : MiddlewareConfiguration ,
127126 module : string ,
128127 applicationRef : any ,
@@ -141,8 +140,8 @@ export class MiddlewareModule {
141140 instance ,
142141 metatype ,
143142 applicationRef ,
144- RequestMethod . ALL ,
145- routePath ,
143+ routeInfo . method ,
144+ routeInfo . path ,
146145 ) ;
147146 } ) ,
148147 ) ;
@@ -151,7 +150,7 @@ export class MiddlewareModule {
151150 private async bindHandler (
152151 instance : NestMiddleware ,
153152 metatype : Type < NestMiddleware > ,
154- applicationRef : any ,
153+ applicationRef : HttpServer ,
155154 method : RequestMethod ,
156155 path : string ,
157156 ) {
@@ -163,13 +162,16 @@ export class MiddlewareModule {
163162 instance . resolve ,
164163 undefined ,
165164 ) ;
166- const router = this . routerMethodFactory
167- . get ( applicationRef , method )
168- . bind ( applicationRef ) ;
169-
170- const bindWithProxy = obj =>
171- this . bindHandlerWithProxy ( exceptionsHandler , router , obj , path ) ;
165+ const router = applicationRef . createMiddlewareFactory ( method ) ;
166+ const bindWithProxy = middlewareInstance =>
167+ this . bindHandlerWithProxy (
168+ exceptionsHandler ,
169+ router ,
170+ middlewareInstance ,
171+ path ,
172+ ) ;
172173 const resolve = instance . resolve ( ) ;
174+
173175 if ( ! ( resolve instanceof Promise ) ) {
174176 bindWithProxy ( resolve ) ;
175177 return ;
0 commit comments