Files
nest/packages/core/router/routes-resolver.ts
2018-12-15 21:22:36 +01:00

113 lines
3.9 KiB
TypeScript

import { BadRequestException, NotFoundException } from '@nestjs/common';
import { MODULE_PATH } from '@nestjs/common/constants';
import { HttpServer } from '@nestjs/common/interfaces';
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
import { Logger } from '@nestjs/common/services/logger.service';
import { ApplicationConfig } from '../application-config';
import { CONTROLLER_MAPPING_MESSAGE } from '../helpers/messages';
import { NestContainer } from '../injector/container';
import { InstanceWrapper } from '../injector/instance-wrapper';
import { MetadataScanner } from '../metadata-scanner';
import { Resolver } from './interfaces/resolver.interface';
import { RouterExceptionFilters } from './router-exception-filters';
import { RouterExplorer } from './router-explorer';
import { RouterProxy } from './router-proxy';
export class RoutesResolver implements Resolver {
private readonly logger = new Logger(RoutesResolver.name, true);
private readonly routerProxy = new RouterProxy();
private readonly routerExceptionsFilter: RouterExceptionFilters;
private readonly routerBuilder: RouterExplorer;
constructor(
private readonly container: NestContainer,
private readonly config: ApplicationConfig,
) {
this.routerExceptionsFilter = new RouterExceptionFilters(
container,
config,
container.getApplicationRef(),
);
this.routerBuilder = new RouterExplorer(
new MetadataScanner(),
this.container,
this.routerProxy,
this.routerExceptionsFilter,
this.config,
);
}
public resolve<T extends HttpServer>(applicationRef: T, basePath: string) {
const modules = this.container.getModules();
modules.forEach(({ controllers, metatype }, moduleName) => {
let path = metatype
? Reflect.getMetadata(MODULE_PATH, metatype)
: undefined;
path = path ? path + basePath : basePath;
this.registerRouters(controllers, moduleName, path, applicationRef);
});
}
public registerRouters(
routes: Map<string, InstanceWrapper<Controller>>,
moduleName: string,
basePath: string,
applicationRef: HttpServer,
) {
routes.forEach(({ instance, metatype }) => {
const path = this.routerBuilder.extractRouterPath(metatype, basePath);
const controllerName = metatype.name;
this.logger.log(CONTROLLER_MAPPING_MESSAGE(controllerName, path));
this.routerBuilder.explore(
instance,
metatype,
moduleName,
applicationRef,
path,
);
});
}
public registerNotFoundHandler() {
const applicationRef = this.container.getApplicationRef();
const callback = <TRequest, TResponse>(req: TRequest, res: TResponse) => {
const method = applicationRef.getRequestMethod(req);
const url = applicationRef.getRequestUrl(req);
throw new NotFoundException(`Cannot ${method} ${url}`);
};
const handler = this.routerExceptionsFilter.create({}, callback, undefined);
const proxy = this.routerProxy.createProxy(callback, handler);
applicationRef.setNotFoundHandler &&
applicationRef.setNotFoundHandler(proxy);
}
public registerExceptionHandler() {
const callback = <TError, TRequest, TResponse>(
err: TError,
req: TRequest,
res: TResponse,
next: Function,
) => {
throw this.mapExternalException(err);
};
const handler = this.routerExceptionsFilter.create(
{},
callback as any,
undefined,
);
const proxy = this.routerProxy.createExceptionLayerProxy(callback, handler);
const applicationRef = this.container.getApplicationRef();
applicationRef.setErrorHandler && applicationRef.setErrorHandler(proxy);
}
public mapExternalException(err: any) {
switch (true) {
case err instanceof SyntaxError:
return new BadRequestException(err.message);
default:
return err;
}
}
}