@@ -2,6 +2,7 @@ import { HttpServer } from '@nestjs/common';
22import { METHOD_METADATA , PATH_METADATA } from '@nestjs/common/constants' ;
33import { RequestMethod } from '@nestjs/common/enums/request-method.enum' ;
44import { InternalServerErrorException } from '@nestjs/common/exceptions' ;
5+ import { RouteInfo } from '@nestjs/common/interfaces' ;
56import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface' ;
67import { Type } from '@nestjs/common/interfaces/type.interface' ;
78import { Logger } from '@nestjs/common/services/logger.service' ;
@@ -56,7 +57,7 @@ export class RouterExplorer {
5657 private readonly injector ?: Injector ,
5758 private readonly routerProxy ?: RouterProxy ,
5859 private readonly exceptionsFilter ?: ExceptionsFilter ,
59- config ?: ApplicationConfig ,
60+ private readonly config ?: ApplicationConfig ,
6061 ) {
6162 this . executionContextCreator = new RouterExecutionContext (
6263 new RouteParamsFactory ( ) ,
@@ -154,7 +155,6 @@ export class RouterExplorer {
154155 host : string ,
155156 ) {
156157 ( routePaths || [ ] ) . forEach ( pathProperties => {
157- const { path, requestMethod } = pathProperties ;
158158 this . applyCallbackToRouter (
159159 router ,
160160 pathProperties ,
@@ -163,17 +163,51 @@ export class RouterExplorer {
163163 basePath ,
164164 host ,
165165 ) ;
166- path . forEach ( item => {
167- const pathStr = this . stripEndSlash ( basePath ) + this . stripEndSlash ( item ) ;
168- this . logger . log ( ROUTE_MAPPED_MESSAGE ( pathStr , requestMethod ) ) ;
169- } ) ;
170166 } ) ;
171167 }
172168
173169 public stripEndSlash ( str : string ) {
174170 return str [ str . length - 1 ] === '/' ? str . slice ( 0 , str . length - 1 ) : str ;
175171 }
176172
173+ public removeGlobalPrefixFromPath ( path : string ) {
174+ const globalPrefix = addLeadingSlash ( this . config . getGlobalPrefix ( ) ) ;
175+ return path . replace ( globalPrefix , '' ) ;
176+ }
177+
178+ public isRouteExcludedFromGlobalPrefix (
179+ path : string ,
180+ requestMethod : RequestMethod ,
181+ ) {
182+ const options = this . config . getGlobalPrefixOptions ( ) ;
183+ if ( ! options . exclude ) {
184+ return false ;
185+ }
186+ const excludedRouteInfos = options . exclude . map (
187+ ( route : string | RouteInfo ) => {
188+ if ( isString ( route ) ) {
189+ return {
190+ path : addLeadingSlash ( route ) ,
191+ method : RequestMethod . ALL ,
192+ } ;
193+ }
194+ return {
195+ path : addLeadingSlash ( route . path ) ,
196+ method : route . method ,
197+ } ;
198+ } ,
199+ ) ;
200+
201+ return excludedRouteInfos . some ( ( route : RouteInfo ) => {
202+ if ( route . path !== path ) {
203+ return false ;
204+ }
205+ return (
206+ route . method === RequestMethod . ALL || route . method === requestMethod
207+ ) ;
208+ } ) ;
209+ }
210+
177211 private applyCallbackToRouter < T extends HttpServer > (
178212 router : T ,
179213 pathProperties : RoutePathProperties ,
@@ -210,10 +244,22 @@ export class RouterExplorer {
210244 requestMethod ,
211245 ) ;
212246
213- const hostHandler = this . applyHostFilter ( host , proxy ) ;
247+ const routeHandler = this . applyHostFilter ( host , proxy ) ;
214248 paths . forEach ( path => {
215- const fullPath = this . stripEndSlash ( basePath ) + path ;
216- routerMethod ( this . stripEndSlash ( fullPath ) || '/' , hostHandler ) ;
249+ let finalPath = this . stripEndSlash ( basePath ) + this . stripEndSlash ( path ) ;
250+
251+ const isGlobalPrefixSet = ! ! this . config . getGlobalPrefix ( ) ;
252+ if ( isGlobalPrefixSet ) {
253+ const unprefixedFullPath = this . removeGlobalPrefixFromPath ( finalPath ) ;
254+
255+ const isExcludedOfGlobalPrefix = this . isRouteExcludedFromGlobalPrefix (
256+ unprefixedFullPath ,
257+ requestMethod ,
258+ ) ;
259+ finalPath = isExcludedOfGlobalPrefix ? unprefixedFullPath : finalPath ;
260+ }
261+ routerMethod ( finalPath || '/' , routeHandler ) ;
262+ this . logger . log ( ROUTE_MAPPED_MESSAGE ( finalPath , requestMethod ) ) ;
217263 } ) ;
218264 }
219265
0 commit comments