UNPKG

3.87 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const common_1 = require("@nestjs/common");
4const constants_1 = require("@nestjs/common/constants");
5const logger_service_1 = require("@nestjs/common/services/logger.service");
6const messages_1 = require("../helpers/messages");
7const metadata_scanner_1 = require("../metadata-scanner");
8const router_exception_filters_1 = require("./router-exception-filters");
9const router_explorer_1 = require("./router-explorer");
10const router_proxy_1 = require("./router-proxy");
11class RoutesResolver {
12 constructor(container, config, injector) {
13 this.container = container;
14 this.config = config;
15 this.injector = injector;
16 this.logger = new logger_service_1.Logger(RoutesResolver.name, true);
17 this.routerProxy = new router_proxy_1.RouterProxy();
18 this.routerExceptionsFilter = new router_exception_filters_1.RouterExceptionFilters(container, config, container.getHttpAdapterRef());
19 const metadataScanner = new metadata_scanner_1.MetadataScanner();
20 this.routerBuilder = new router_explorer_1.RouterExplorer(metadataScanner, this.container, this.injector, this.routerProxy, this.routerExceptionsFilter, this.config);
21 }
22 resolve(applicationRef, basePath) {
23 const modules = this.container.getModules();
24 modules.forEach(({ controllers, metatype }, moduleName) => {
25 let path = metatype ? this.getModulePathMetadata(metatype) : undefined;
26 path = path ? basePath + path : basePath;
27 this.registerRouters(controllers, moduleName, path, applicationRef);
28 });
29 }
30 registerRouters(routes, moduleName, basePath, applicationRef) {
31 routes.forEach(instanceWrapper => {
32 const { metatype } = instanceWrapper;
33 const host = this.getHostMetadata(metatype);
34 const path = this.routerBuilder.extractRouterPath(metatype, basePath);
35 const controllerName = metatype.name;
36 this.logger.log(messages_1.CONTROLLER_MAPPING_MESSAGE(controllerName, path));
37 this.routerBuilder.explore(instanceWrapper, moduleName, applicationRef, path, host);
38 });
39 }
40 registerNotFoundHandler() {
41 const applicationRef = this.container.getHttpAdapterRef();
42 const callback = (req, res) => {
43 const method = applicationRef.getRequestMethod(req);
44 const url = applicationRef.getRequestUrl(req);
45 throw new common_1.NotFoundException(`Cannot ${method} ${url}`);
46 };
47 const handler = this.routerExceptionsFilter.create({}, callback, undefined);
48 const proxy = this.routerProxy.createProxy(callback, handler);
49 applicationRef.setNotFoundHandler &&
50 applicationRef.setNotFoundHandler(proxy, this.config.getGlobalPrefix());
51 }
52 registerExceptionHandler() {
53 const callback = (err, req, res, next) => {
54 throw this.mapExternalException(err);
55 };
56 const handler = this.routerExceptionsFilter.create({}, callback, undefined);
57 const proxy = this.routerProxy.createExceptionLayerProxy(callback, handler);
58 const applicationRef = this.container.getHttpAdapterRef();
59 applicationRef.setErrorHandler &&
60 applicationRef.setErrorHandler(proxy, this.config.getGlobalPrefix());
61 }
62 mapExternalException(err) {
63 switch (true) {
64 case err instanceof SyntaxError:
65 return new common_1.BadRequestException(err.message);
66 default:
67 return err;
68 }
69 }
70 getModulePathMetadata(metatype) {
71 return Reflect.getMetadata(constants_1.MODULE_PATH, metatype);
72 }
73 getHostMetadata(metatype) {
74 return Reflect.getMetadata(constants_1.HOST_METADATA, metatype);
75 }
76}
77exports.RoutesResolver = RoutesResolver;