UNPKG

3.59 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 router_explorer_1 = require("../router/router-explorer");
8const router_module_1 = require("../router/router-module");
9class RoutesMapper {
10 constructor(container) {
11 this.container = container;
12 this.routerExplorer = new router_explorer_1.RouterExplorer(new metadata_scanner_1.MetadataScanner(), container);
13 }
14 mapRouteToRouteInfo(route) {
15 if ((0, shared_utils_1.isString)(route)) {
16 const defaultRequestMethod = -1;
17 return [
18 {
19 path: (0, shared_utils_1.addLeadingSlash)(route),
20 method: defaultRequestMethod,
21 },
22 ];
23 }
24 const routePathOrPaths = this.getRoutePath(route);
25 if (this.isRouteInfo(routePathOrPaths, route)) {
26 return [
27 {
28 path: (0, shared_utils_1.addLeadingSlash)(route.path),
29 method: route.method,
30 },
31 ];
32 }
33 const controllerPaths = this.routerExplorer.scanForPaths(Object.create(route), route.prototype);
34 const moduleRef = this.getHostModuleOfController(route);
35 const modulePath = this.getModulePath(moduleRef === null || moduleRef === void 0 ? void 0 : moduleRef.metatype);
36 const concatPaths = (acc, currentValue) => acc.concat(currentValue);
37 return []
38 .concat(routePathOrPaths)
39 .map(routePath => controllerPaths
40 .map(item => {
41 var _a;
42 return (_a = item.path) === null || _a === void 0 ? void 0 : _a.map(p => {
43 let path = modulePath !== null && modulePath !== void 0 ? modulePath : '';
44 path += this.normalizeGlobalPath(routePath) + (0, shared_utils_1.addLeadingSlash)(p);
45 return {
46 path,
47 method: item.requestMethod,
48 };
49 });
50 })
51 .reduce(concatPaths, []))
52 .reduce(concatPaths, []);
53 }
54 isRouteInfo(path, objectOrClass) {
55 return (0, shared_utils_1.isUndefined)(path);
56 }
57 normalizeGlobalPath(path) {
58 const prefix = (0, shared_utils_1.addLeadingSlash)(path);
59 return prefix === '/' ? '' : prefix;
60 }
61 getRoutePath(route) {
62 return Reflect.getMetadata(constants_1.PATH_METADATA, route);
63 }
64 getHostModuleOfController(metatype) {
65 if (!metatype) {
66 return;
67 }
68 const modulesContainer = this.container.getModules();
69 const moduleRefsSet = router_module_1.targetModulesByContainer.get(modulesContainer);
70 if (!moduleRefsSet) {
71 return;
72 }
73 const modules = Array.from(modulesContainer.values()).filter(moduleRef => moduleRefsSet.has(moduleRef));
74 return modules.find(({ routes }) => routes.has(metatype));
75 }
76 getModulePath(metatype) {
77 if (!metatype) {
78 return;
79 }
80 const modulesContainer = this.container.getModules();
81 const modulePath = Reflect.getMetadata(constants_1.MODULE_PATH + modulesContainer.applicationId, metatype);
82 return modulePath !== null && modulePath !== void 0 ? modulePath : Reflect.getMetadata(constants_1.MODULE_PATH, metatype);
83 }
84}
85exports.RoutesMapper = RoutesMapper;