Skip to content

Commit

Permalink
Merge 8ae0a43 into 1812567
Browse files Browse the repository at this point in the history
  • Loading branch information
Shady Khalifa committed Jan 31, 2018
2 parents 1812567 + 8ae0a43 commit f46288a
Show file tree
Hide file tree
Showing 14 changed files with 1,378 additions and 348 deletions.
1 change: 1 addition & 0 deletions lib/common/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export declare const GUARDS_METADATA = "__guards__";
export declare const INTERCEPTORS_METADATA = "__interceptors__";
export declare const HTTP_CODE_METADATA = "__httpCode__";
export declare const GATEWAY_MIDDLEWARES = "__gatewayMiddlewares";
export declare const MODULE_PATH = "__module_path__";
1 change: 1 addition & 0 deletions lib/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ exports.GUARDS_METADATA = '__guards__';
exports.INTERCEPTORS_METADATA = '__interceptors__';
exports.HTTP_CODE_METADATA = '__httpCode__';
exports.GATEWAY_MIDDLEWARES = '__gatewayMiddlewares';
exports.MODULE_PATH = '__module_path__';
2 changes: 1 addition & 1 deletion lib/core/router/interfaces/explorer.inteface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { Controller } from '@nestjs/common/interfaces/index';
import { Metatype } from '@nestjs/common/interfaces/metatype.interface';
export interface RouterExplorer {
explore(instance: Controller, metatype: Metatype<Controller>, module: string): any;
fetchRouterPath(metatype: Metatype<Controller>): string;
fetchRouterPath(metatype: Metatype<Controller>, prefix?: string): string;
}
2 changes: 1 addition & 1 deletion lib/core/router/router-explorer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export declare class ExpressRouterExplorer implements RouterExplorer {
private readonly logger;
constructor(metadataScanner?: MetadataScanner, routerProxy?: RouterProxy, expressAdapter?: ExpressAdapter, exceptionsFilter?: ExceptionsFilter, config?: ApplicationConfig, container?: NestContainer);
explore(instance: Controller, metatype: Metatype<Controller>, module: string): any;
fetchRouterPath(metatype: Metatype<Controller>): string;
fetchRouterPath(metatype: Metatype<Controller>, prefix?: string): string;
validateRoutePath(path: string): string;
scanForPaths(instance: Controller, prototype?: any): RoutePathProperties[];
exploreMethodMetadata(instance: Controller, instancePrototype: any, methodName: string): RoutePathProperties;
Expand Down
6 changes: 4 additions & 2 deletions lib/core/router/router-explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ class ExpressRouterExplorer {
this.applyPathsToRouterProxy(router, routerPaths, instance, module);
return router;
}
fetchRouterPath(metatype) {
const path = Reflect.getMetadata(constants_1.PATH_METADATA, metatype);
fetchRouterPath(metatype, prefix) {
let path = Reflect.getMetadata(constants_1.PATH_METADATA, metatype);
if (prefix)
path = prefix + this.validateRoutePath(path);
return this.validateRoutePath(path);
}
validateRoutePath(path) {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/router/routes-resolver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export declare class RoutesResolver implements Resolver {
private readonly routerBuilder;
constructor(container: NestContainer, expressAdapter: any, config: ApplicationConfig);
resolve(express: Application): void;
setupRouters(routes: Map<string, InstanceWrapper<Controller>>, moduleName: string, express: Application): void;
setupRouters(routes: Map<string, InstanceWrapper<Controller>>, moduleName: string, modulePath: string, express: Application): void;
setupNotFoundHandler(express: Application): void;
setupExceptionHandler(express: Application): void;
}
10 changes: 7 additions & 3 deletions lib/core/router/routes-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const router_exception_filters_1 = require("./router-exception-filters");
const metadata_scanner_1 = require("../metadata-scanner");
const router_explorer_1 = require("./router-explorer");
const common_1 = require("@nestjs/common");
const constants_1 = require("@nestjs/common/constants");
class RoutesResolver {
constructor(container, expressAdapter, config) {
this.container = container;
Expand All @@ -19,13 +20,16 @@ class RoutesResolver {
}
resolve(express) {
const modules = this.container.getModules();
modules.forEach(({ routes }, moduleName) => this.setupRouters(routes, moduleName, express));
modules.forEach((module, moduleName) => {
const path = Reflect.getMetadata(constants_1.MODULE_PATH, module.metatype);
this.setupRouters(module.routes, moduleName, path, express);
});
this.setupNotFoundHandler(express);
this.setupExceptionHandler(express);
}
setupRouters(routes, moduleName, express) {
setupRouters(routes, moduleName, modulePath, express) {
routes.forEach(({ instance, metatype }) => {
const path = this.routerBuilder.fetchRouterPath(metatype);
const path = this.routerBuilder.fetchRouterPath(metatype, modulePath);
const controllerName = metatype.name;
this.logger.log(messages_1.ControllerMappingMessage(controllerName, path));
const router = this.routerBuilder.explore(instance, metatype, moduleName);
Expand Down
1 change: 1 addition & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export const GUARDS_METADATA = '__guards__';
export const INTERCEPTORS_METADATA = '__interceptors__';
export const HTTP_CODE_METADATA = '__httpCode__';
export const GATEWAY_MIDDLEWARES = '__gatewayMiddlewares';
export const MODULE_PATH = '__module_path__';
2 changes: 1 addition & 1 deletion src/core/router/interfaces/explorer.inteface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { Metatype } from '@nestjs/common/interfaces/metatype.interface';

export interface RouterExplorer {
explore(instance: Controller, metatype: Metatype<Controller>, module: string);
fetchRouterPath(metatype: Metatype<Controller>): string;
fetchRouterPath(metatype: Metatype<Controller>, prefix?: string): string;
}
8 changes: 6 additions & 2 deletions src/core/router/router-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ export class ExpressRouterExplorer implements RouterExplorer {
return router;
}

public fetchRouterPath(metatype: Metatype<Controller>): string {
const path = Reflect.getMetadata(PATH_METADATA, metatype);
public fetchRouterPath(
metatype: Metatype<Controller>,
prefix?: string,
): string {
let path = Reflect.getMetadata(PATH_METADATA, metatype);
if (prefix) path = prefix + this.validateRoutePath(path);
return this.validateRoutePath(path);
}

Expand Down
14 changes: 9 additions & 5 deletions src/core/router/routes-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { RouterExplorer } from './interfaces/explorer.inteface';
import { ExpressRouterExplorer } from './router-explorer';
import { ApplicationConfig } from './../application-config';
import { NotFoundException } from '@nestjs/common';

import { MODULE_PATH } from '@nestjs/common/constants';
export class RoutesResolver implements Resolver {
private readonly logger = new Logger(RoutesResolver.name, true);
private readonly routerProxy = new RouterProxy();
Expand All @@ -36,9 +36,12 @@ export class RoutesResolver implements Resolver {

public resolve(express: Application) {
const modules = this.container.getModules();
modules.forEach(({ routes }, moduleName) =>
this.setupRouters(routes, moduleName, express),
);
modules.forEach(({ routes, metatype }, moduleName) => {
const path = metatype
? Reflect.getMetadata(MODULE_PATH, metatype)
: undefined;
this.setupRouters(routes, moduleName, path, express);
});

this.setupNotFoundHandler(express);
this.setupExceptionHandler(express);
Expand All @@ -47,10 +50,11 @@ export class RoutesResolver implements Resolver {
public setupRouters(
routes: Map<string, InstanceWrapper<Controller>>,
moduleName: string,
modulePath: string,
express: Application,
) {
routes.forEach(({ instance, metatype }) => {
const path = this.routerBuilder.fetchRouterPath(metatype);
const path = this.routerBuilder.fetchRouterPath(metatype, modulePath);
const controllerName = metatype.name;

this.logger.log(ControllerMappingMessage(controllerName, path));
Expand Down
13 changes: 13 additions & 0 deletions src/core/test/router/router-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@ describe('RouterExplorer', () => {
expect(bindStub.callCount).to.be.eql(paths.length);
});
});

describe('fetchRouterPath', () => {
it('should return expected path', () => {
expect(routerBuilder.fetchRouterPath(TestRoute)).to.be.eql('/global');
expect(routerBuilder.fetchRouterPath(TestRoute, '/module')).to.be.eql(
'/module/global',
);
});

it('should throw it a there is a bad path expected path', () => {
expect(() => routerBuilder.validateRoutePath(undefined)).to.throw();
});
});
});
2 changes: 1 addition & 1 deletion src/core/test/router/routes-resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('RoutesResolver', () => {
});

const use = sinon.spy();
routesResolver.setupRouters(routes, '', { use } as any);
routesResolver.setupRouters(routes, '', undefined, { use } as any);
expect(use.calledWith('/global', router)).to.be.true;
});
});
Expand Down

0 comments on commit f46288a

Please sign in to comment.