1- import { PATH_METADATA , SCOPE_OPTIONS_METADATA } from '../../constants' ;
1+ import {
2+ PATH_METADATA ,
3+ PATH_PREFIX_METADATA ,
4+ SCOPE_OPTIONS_METADATA ,
5+ } from '../../constants' ;
26import { isString , isUndefined } from '../../utils/shared.utils' ;
37import { ScopeOptions } from '../../interfaces/scope-options.interface' ;
48
@@ -9,12 +13,22 @@ import { ScopeOptions } from '../../interfaces/scope-options.interface';
913 */
1014export interface ControllerOptions extends ScopeOptions {
1115 /**
12- * Specifies an optional `route path prefix`. The prefix is pre-pended to the
16+ * Specifies an optional `route path prefix`. The prefix is pre-pended to the
1317 * path specified in any request decorator in the class.
1418 *
1519 * @see [Routing](https://docs.nestjs.com/controllers#routing)
1620 */
1721 path ?: string ;
22+
23+ /**
24+ * Specifies an optional setting for enabling/disabling the use of app's
25+ * GlobalPrefix. In other words, a switch to remove the prefix set using
26+ * `setGlobalPrefix()` only for this controller.
27+ *
28+ * @see [Routing](https://docs.nestjs.com/controllers#routing)
29+ * @see [Global path prefix](https://docs.nestjs.com/faq/global-prefix)
30+ */
31+ useGlobalPrefix ?: boolean ;
1832}
1933
2034/**
@@ -113,8 +127,10 @@ export function Controller(options: ControllerOptions): ClassDecorator;
113127 * - `scope` - symbol that determines the lifetime of a Controller instance.
114128 * [See Scope](https://docs.nestjs.com/fundamentals/injection-scopes#usage) for
115129 * more details.
116- * - `prefix ` - string that defines a `route path prefix`. The prefix
130+ * - `path ` - string that defines a `route path prefix`. The prefix
117131 * is pre-pended to the path specified in any request decorator in the class.
132+ * - `useGlobalPrefix` - a boolean value that enables/disables the setting done
133+ * using `setGlobalPrefix()`.
118134 *
119135 * @see [Routing](https://docs.nestjs.com/controllers#routing)
120136 * @see [Controllers](https://docs.nestjs.com/controllers)
@@ -127,14 +143,22 @@ export function Controller(
127143 prefixOrOptions ?: string | ControllerOptions ,
128144) : ClassDecorator {
129145 const defaultPath = '/' ;
130- const [ path , scopeOptions ] = isUndefined ( prefixOrOptions )
131- ? [ defaultPath , undefined ]
146+ const defaultUseGlobalPrefix = true ;
147+ const [ path , useGlobalPrefix , scopeOptions ] = isUndefined ( prefixOrOptions )
148+ ? [ defaultPath , defaultUseGlobalPrefix , undefined ]
132149 : isString ( prefixOrOptions )
133- ? [ prefixOrOptions , undefined ]
134- : [ prefixOrOptions . path || defaultPath , { scope : prefixOrOptions . scope } ] ;
150+ ? [ prefixOrOptions , defaultUseGlobalPrefix , undefined ]
151+ : [
152+ prefixOrOptions . path || defaultPath ,
153+ ! isUndefined ( prefixOrOptions )
154+ ? Boolean ( prefixOrOptions . useGlobalPrefix )
155+ : defaultUseGlobalPrefix ,
156+ { scope : prefixOrOptions . scope } ,
157+ ] ;
135158
136159 return ( target : object ) => {
137160 Reflect . defineMetadata ( PATH_METADATA , path , target ) ;
138161 Reflect . defineMetadata ( SCOPE_OPTIONS_METADATA , scopeOptions , target ) ;
162+ Reflect . defineMetadata ( PATH_PREFIX_METADATA , useGlobalPrefix , target ) ;
139163 } ;
140164}
0 commit comments