Skip to content

Commit 8cd42f5

Browse files
Merge branch 'shekohex-nest-router'
2 parents d843678 + 8fdf428 commit 8cd42f5

14 files changed

Lines changed: 47 additions & 4902 deletions

lib/common/constants.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export declare const GUARDS_METADATA = "__guards__";
2020
export declare const INTERCEPTORS_METADATA = "__interceptors__";
2121
export declare const HTTP_CODE_METADATA = "__httpCode__";
2222
export declare const GATEWAY_MIDDLEWARES = "__gatewayMiddlewares";
23+
export declare const MODULE_PATH = "__module_path__";

lib/common/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ exports.GUARDS_METADATA = '__guards__';
2222
exports.INTERCEPTORS_METADATA = '__interceptors__';
2323
exports.HTTP_CODE_METADATA = '__httpCode__';
2424
exports.GATEWAY_MIDDLEWARES = '__gatewayMiddlewares';
25+
exports.MODULE_PATH = '__module_path__';

lib/core/router/interfaces/explorer.inteface.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { Controller } from '@nestjs/common/interfaces/index';
22
import { Metatype } from '@nestjs/common/interfaces/metatype.interface';
33
export interface RouterExplorer {
44
explore(instance: Controller, metatype: Metatype<Controller>, module: string): any;
5-
fetchRouterPath(metatype: Metatype<Controller>): string;
5+
fetchRouterPath(metatype: Metatype<Controller>, prefix?: string): string;
66
}

lib/core/router/router-explorer.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export declare class ExpressRouterExplorer implements RouterExplorer {
2020
private readonly logger;
2121
constructor(metadataScanner?: MetadataScanner, routerProxy?: RouterProxy, expressAdapter?: ExpressAdapter, exceptionsFilter?: ExceptionsFilter, config?: ApplicationConfig, container?: NestContainer);
2222
explore(instance: Controller, metatype: Metatype<Controller>, module: string): any;
23-
fetchRouterPath(metatype: Metatype<Controller>): string;
23+
fetchRouterPath(metatype: Metatype<Controller>, prefix?: string): string;
2424
validateRoutePath(path: string): string;
2525
scanForPaths(instance: Controller, prototype?: any): RoutePathProperties[];
2626
exploreMethodMetadata(instance: Controller, instancePrototype: any, methodName: string): RoutePathProperties;

lib/core/router/router-explorer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ class ExpressRouterExplorer {
3232
this.applyPathsToRouterProxy(router, routerPaths, instance, module);
3333
return router;
3434
}
35-
fetchRouterPath(metatype) {
36-
const path = Reflect.getMetadata(constants_1.PATH_METADATA, metatype);
35+
fetchRouterPath(metatype, prefix) {
36+
let path = Reflect.getMetadata(constants_1.PATH_METADATA, metatype);
37+
if (prefix)
38+
path = prefix + this.validateRoutePath(path);
3739
return this.validateRoutePath(path);
3840
}
3941
validateRoutePath(path) {

lib/core/router/routes-resolver.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export declare class RoutesResolver implements Resolver {
1414
private readonly routerBuilder;
1515
constructor(container: NestContainer, expressAdapter: any, config: ApplicationConfig);
1616
resolve(express: Application): void;
17-
setupRouters(routes: Map<string, InstanceWrapper<Controller>>, moduleName: string, express: Application): void;
17+
setupRouters(routes: Map<string, InstanceWrapper<Controller>>, moduleName: string, modulePath: string, express: Application): void;
1818
setupNotFoundHandler(express: Application): void;
1919
setupExceptionHandler(express: Application): void;
2020
}

lib/core/router/routes-resolver.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const router_exception_filters_1 = require("./router-exception-filters");
77
const metadata_scanner_1 = require("../metadata-scanner");
88
const router_explorer_1 = require("./router-explorer");
99
const common_1 = require("@nestjs/common");
10+
const constants_1 = require("@nestjs/common/constants");
1011
class RoutesResolver {
1112
constructor(container, expressAdapter, config) {
1213
this.container = container;
@@ -19,13 +20,16 @@ class RoutesResolver {
1920
}
2021
resolve(express) {
2122
const modules = this.container.getModules();
22-
modules.forEach(({ routes }, moduleName) => this.setupRouters(routes, moduleName, express));
23+
modules.forEach((module, moduleName) => {
24+
const path = Reflect.getMetadata(constants_1.MODULE_PATH, module.metatype);
25+
this.setupRouters(module.routes, moduleName, path, express);
26+
});
2327
this.setupNotFoundHandler(express);
2428
this.setupExceptionHandler(express);
2529
}
26-
setupRouters(routes, moduleName, express) {
30+
setupRouters(routes, moduleName, modulePath, express) {
2731
routes.forEach(({ instance, metatype }) => {
28-
const path = this.routerBuilder.fetchRouterPath(metatype);
32+
const path = this.routerBuilder.fetchRouterPath(metatype, modulePath);
2933
const controllerName = metatype.name;
3034
this.logger.log(messages_1.ControllerMappingMessage(controllerName, path));
3135
const router = this.routerBuilder.explore(instance, metatype, moduleName);

src/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ export const GUARDS_METADATA = '__guards__';
2121
export const INTERCEPTORS_METADATA = '__interceptors__';
2222
export const HTTP_CODE_METADATA = '__httpCode__';
2323
export const GATEWAY_MIDDLEWARES = '__gatewayMiddlewares';
24+
export const MODULE_PATH = '__module_path__';

src/core/router/interfaces/explorer.inteface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { Metatype } from '@nestjs/common/interfaces/metatype.interface';
33

44
export interface RouterExplorer {
55
explore(instance: Controller, metatype: Metatype<Controller>, module: string);
6-
fetchRouterPath(metatype: Metatype<Controller>): string;
6+
fetchRouterPath(metatype: Metatype<Controller>, prefix?: string): string;
77
}

src/core/router/router-explorer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ export class ExpressRouterExplorer implements RouterExplorer {
6060
return router;
6161
}
6262

63-
public fetchRouterPath(metatype: Metatype<Controller>): string {
64-
const path = Reflect.getMetadata(PATH_METADATA, metatype);
63+
public fetchRouterPath(
64+
metatype: Metatype<Controller>,
65+
prefix?: string,
66+
): string {
67+
let path = Reflect.getMetadata(PATH_METADATA, metatype);
68+
if (prefix) path = prefix + this.validateRoutePath(path);
6569
return this.validateRoutePath(path);
6670
}
6771

0 commit comments

Comments
 (0)