UNPKG

4.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.RoutesMapper = void 0;
4const constants_1 = require("@nestjs/common/constants");
5const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6const metadata_scanner_1 = require("../metadata-scanner");
7const paths_explorer_1 = require("../router/paths-explorer");
8const router_module_1 = require("../router/router-module");
9class RoutesMapper {
10 constructor(container) {
11 this.container = container;
12 this.pathsExplorer = new paths_explorer_1.PathsExplorer(new metadata_scanner_1.MetadataScanner());
13 }
14 mapRouteToRouteInfo(route) {
15 if ((0, shared_utils_1.isString)(route)) {
16 return this.getRouteInfoFromPath(route);
17 }
18 const routePathOrPaths = this.getRoutePath(route);
19 if (this.isRouteInfo(routePathOrPaths, route)) {
20 return this.getRouteInfoFromObject(route);
21 }
22 return this.getRouteInfoFromController(route, routePathOrPaths);
23 }
24 getRouteInfoFromPath(routePath) {
25 const defaultRequestMethod = -1;
26 return [
27 {
28 path: (0, shared_utils_1.addLeadingSlash)(routePath),
29 method: defaultRequestMethod,
30 },
31 ];
32 }
33 getRouteInfoFromObject(routeInfoObject) {
34 const routeInfo = {
35 path: (0, shared_utils_1.addLeadingSlash)(routeInfoObject.path),
36 method: routeInfoObject.method,
37 };
38 if (routeInfoObject.version) {
39 routeInfo.version = routeInfoObject.version;
40 }
41 return [routeInfo];
42 }
43 getRouteInfoFromController(controller, routePath) {
44 const controllerPaths = this.pathsExplorer.scanForPaths(Object.create(controller), controller.prototype);
45 const moduleRef = this.getHostModuleOfController(controller);
46 const modulePath = this.getModulePath(moduleRef === null || moduleRef === void 0 ? void 0 : moduleRef.metatype);
47 const concatPaths = (acc, currentValue) => acc.concat(currentValue);
48 return []
49 .concat(routePath)
50 .map(routePath => controllerPaths
51 .map(item => {
52 var _a;
53 return (_a = item.path) === null || _a === void 0 ? void 0 : _a.map(p => {
54 let path = modulePath !== null && modulePath !== void 0 ? modulePath : '';
55 path += this.normalizeGlobalPath(routePath) + (0, shared_utils_1.addLeadingSlash)(p);
56 const routeInfo = {
57 path,
58 method: item.requestMethod,
59 };
60 if (item.version) {
61 routeInfo.version = item.version;
62 }
63 return routeInfo;
64 });
65 })
66 .reduce(concatPaths, []))
67 .reduce(concatPaths, []);
68 }
69 isRouteInfo(path, objectOrClass) {
70 return (0, shared_utils_1.isUndefined)(path);
71 }
72 normalizeGlobalPath(path) {
73 const prefix = (0, shared_utils_1.addLeadingSlash)(path);
74 return prefix === '/' ? '' : prefix;
75 }
76 getRoutePath(route) {
77 return Reflect.getMetadata(constants_1.PATH_METADATA, route);
78 }
79 getHostModuleOfController(metatype) {
80 if (!metatype) {
81 return;
82 }
83 const modulesContainer = this.container.getModules();
84 const moduleRefsSet = router_module_1.targetModulesByContainer.get(modulesContainer);
85 if (!moduleRefsSet) {
86 return;
87 }
88 const modules = Array.from(modulesContainer.values()).filter(moduleRef => moduleRefsSet.has(moduleRef));
89 return modules.find(({ routes }) => routes.has(metatype));
90 }
91 getModulePath(metatype) {
92 if (!metatype) {
93 return;
94 }
95 const modulesContainer = this.container.getModules();
96 const modulePath = Reflect.getMetadata(constants_1.MODULE_PATH + modulesContainer.applicationId, metatype);
97 return modulePath !== null && modulePath !== void 0 ? modulePath : Reflect.getMetadata(constants_1.MODULE_PATH, metatype);
98 }
99}
100exports.RoutesMapper = RoutesMapper;