1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.RoutesResolver = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const constants_1 = require("@nestjs/common/constants");
|
6 | const logger_service_1 = require("@nestjs/common/services/logger.service");
|
7 | const messages_1 = require("../helpers/messages");
|
8 | const metadata_scanner_1 = require("../metadata-scanner");
|
9 | const route_path_factory_1 = require("./route-path-factory");
|
10 | const router_exception_filters_1 = require("./router-exception-filters");
|
11 | const router_explorer_1 = require("./router-explorer");
|
12 | const router_proxy_1 = require("./router-proxy");
|
13 | class RoutesResolver {
|
14 | constructor(container, applicationConfig, injector) {
|
15 | this.container = container;
|
16 | this.applicationConfig = applicationConfig;
|
17 | this.injector = injector;
|
18 | this.logger = new logger_service_1.Logger(RoutesResolver.name, {
|
19 | timestamp: true,
|
20 | });
|
21 | this.routerProxy = new router_proxy_1.RouterProxy();
|
22 | const httpAdapterRef = container.getHttpAdapterRef();
|
23 | this.routerExceptionsFilter = new router_exception_filters_1.RouterExceptionFilters(container, applicationConfig, httpAdapterRef);
|
24 | this.routePathFactory = new route_path_factory_1.RoutePathFactory(this.applicationConfig);
|
25 | const metadataScanner = new metadata_scanner_1.MetadataScanner();
|
26 | this.routerExplorer = new router_explorer_1.RouterExplorer(metadataScanner, this.container, this.injector, this.routerProxy, this.routerExceptionsFilter, this.applicationConfig, this.routePathFactory);
|
27 | }
|
28 | resolve(applicationRef, globalPrefix) {
|
29 | const modules = this.container.getModules();
|
30 | modules.forEach(({ controllers, metatype }, moduleName) => {
|
31 | const modulePath = this.getModulePathMetadata(metatype);
|
32 | this.registerRouters(controllers, moduleName, globalPrefix, modulePath, applicationRef);
|
33 | });
|
34 | }
|
35 | registerRouters(routes, moduleName, globalPrefix, modulePath, applicationRef) {
|
36 | routes.forEach(instanceWrapper => {
|
37 | const { metatype } = instanceWrapper;
|
38 | const host = this.getHostMetadata(metatype);
|
39 | const routerPaths = this.routerExplorer.extractRouterPath(metatype);
|
40 | const controllerVersion = this.getVersionMetadata(metatype);
|
41 | const controllerName = metatype.name;
|
42 | routerPaths.forEach(path => {
|
43 | const pathsToLog = this.routePathFactory.create({
|
44 | ctrlPath: path,
|
45 | modulePath,
|
46 | globalPrefix,
|
47 | });
|
48 | if (!controllerVersion) {
|
49 | pathsToLog.forEach(path => {
|
50 | const logMessage = (0, messages_1.CONTROLLER_MAPPING_MESSAGE)(controllerName, path);
|
51 | this.logger.log(logMessage);
|
52 | });
|
53 | }
|
54 | else {
|
55 | pathsToLog.forEach(path => {
|
56 | const logMessage = (0, messages_1.VERSIONED_CONTROLLER_MAPPING_MESSAGE)(controllerName, path, controllerVersion);
|
57 | this.logger.log(logMessage);
|
58 | });
|
59 | }
|
60 | const versioningOptions = this.applicationConfig.getVersioning();
|
61 | const routePathMetadata = {
|
62 | ctrlPath: path,
|
63 | modulePath,
|
64 | globalPrefix,
|
65 | controllerVersion,
|
66 | versioningOptions,
|
67 | };
|
68 | this.routerExplorer.explore(instanceWrapper, moduleName, applicationRef, host, routePathMetadata);
|
69 | });
|
70 | });
|
71 | }
|
72 | registerNotFoundHandler() {
|
73 | const applicationRef = this.container.getHttpAdapterRef();
|
74 | const callback = (req, res) => {
|
75 | const method = applicationRef.getRequestMethod(req);
|
76 | const url = applicationRef.getRequestUrl(req);
|
77 | throw new common_1.NotFoundException(`Cannot ${method} ${url}`);
|
78 | };
|
79 | const handler = this.routerExceptionsFilter.create({}, callback, undefined);
|
80 | const proxy = this.routerProxy.createProxy(callback, handler);
|
81 | applicationRef.setNotFoundHandler &&
|
82 | applicationRef.setNotFoundHandler(proxy, this.applicationConfig.getGlobalPrefix());
|
83 | }
|
84 | registerExceptionHandler() {
|
85 | const callback = (err, req, res, next) => {
|
86 | throw this.mapExternalException(err);
|
87 | };
|
88 | const handler = this.routerExceptionsFilter.create({}, callback, undefined);
|
89 | const proxy = this.routerProxy.createExceptionLayerProxy(callback, handler);
|
90 | const applicationRef = this.container.getHttpAdapterRef();
|
91 | applicationRef.setErrorHandler &&
|
92 | applicationRef.setErrorHandler(proxy, this.applicationConfig.getGlobalPrefix());
|
93 | }
|
94 | mapExternalException(err) {
|
95 | switch (true) {
|
96 |
|
97 |
|
98 |
|
99 | case err instanceof SyntaxError || err instanceof URIError:
|
100 | return new common_1.BadRequestException(err.message);
|
101 | default:
|
102 | return err;
|
103 | }
|
104 | }
|
105 | getModulePathMetadata(metatype) {
|
106 | const modulesContainer = this.container.getModules();
|
107 | const modulePath = Reflect.getMetadata(constants_1.MODULE_PATH + modulesContainer.applicationId, metatype);
|
108 | return modulePath !== null && modulePath !== void 0 ? modulePath : Reflect.getMetadata(constants_1.MODULE_PATH, metatype);
|
109 | }
|
110 | getHostMetadata(metatype) {
|
111 | return Reflect.getMetadata(constants_1.HOST_METADATA, metatype);
|
112 | }
|
113 | getVersionMetadata(metatype) {
|
114 | var _a;
|
115 | const versioningConfig = this.applicationConfig.getVersioning();
|
116 | if (versioningConfig) {
|
117 | return ((_a = Reflect.getMetadata(constants_1.VERSION_METADATA, metatype)) !== null && _a !== void 0 ? _a : versioningConfig.defaultVersion);
|
118 | }
|
119 | }
|
120 | }
|
121 | exports.RoutesResolver = RoutesResolver;
|