Skip to content

Commit 110f27b

Browse files
Merge pull request nestjs#3727 from prateekkathal/feature/disable-global-prefix
feat(common/core): Added useGlobalPrefix option for Controllers
2 parents 420a530 + 8e664f0 commit 110f27b

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

packages/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const METADATA = {
88
export const SHARED_MODULE_METADATA = '__module:shared__';
99
export const GLOBAL_MODULE_METADATA = '__module:global__';
1010
export const PATH_METADATA = 'path';
11+
export const PATH_PREFIX_METADATA = 'path:globalPrefix';
1112
export const PARAMTYPES_METADATA = 'design:paramtypes';
1213
export const SELF_DECLARED_DEPS_METADATA = 'self:paramtypes';
1314
export const OPTIONAL_DEPS_METADATA = 'optional:paramtypes';

packages/common/decorators/core/controller.decorator.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
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';
26
import { isString, isUndefined } from '../../utils/shared.utils';
37
import { ScopeOptions } from '../../interfaces/scope-options.interface';
48

@@ -9,12 +13,22 @@ import { ScopeOptions } from '../../interfaces/scope-options.interface';
913
*/
1014
export 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
}

packages/core/router/router-explorer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { HttpServer } from '@nestjs/common';
2-
import { METHOD_METADATA, PATH_METADATA } from '@nestjs/common/constants';
2+
import {
3+
PATH_METADATA,
4+
METHOD_METADATA,
5+
PATH_PREFIX_METADATA,
6+
} from '@nestjs/common/constants';
37
import { RequestMethod } from '@nestjs/common/enums/request-method.enum';
48
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
59
import { Type } from '@nestjs/common/interfaces/type.interface';
@@ -88,7 +92,8 @@ export class RouterExplorer {
8892
prefix?: string,
8993
): string {
9094
let path = Reflect.getMetadata(PATH_METADATA, metatype);
91-
if (prefix) path = prefix + this.validateRoutePath(path);
95+
const usePrefix = Reflect.getMetadata(PATH_PREFIX_METADATA, metatype);
96+
if (prefix && usePrefix) path = prefix + this.validateRoutePath(path);
9297
return this.validateRoutePath(path);
9398
}
9499

0 commit comments

Comments
 (0)