1 | "use strict";
|
2 | var RouterModule_1;
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.RouterModule = exports.targetModulesByContainer = exports.ROUTES = void 0;
|
5 | const tslib_1 = require("tslib");
|
6 | const common_1 = require("@nestjs/common");
|
7 | const constants_1 = require("@nestjs/common/constants");
|
8 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
9 | const modules_container_1 = require("../injector/modules-container");
|
10 | const utils_1 = require("./utils");
|
11 | exports.ROUTES = Symbol('ROUTES');
|
12 | exports.targetModulesByContainer = new WeakMap();
|
13 |
|
14 |
|
15 |
|
16 | let RouterModule = RouterModule_1 = class RouterModule {
|
17 | constructor(modulesContainer, routes) {
|
18 | this.modulesContainer = modulesContainer;
|
19 | this.routes = routes;
|
20 | this.routes = this.deepCloneRoutes(routes);
|
21 | this.initialize();
|
22 | }
|
23 | static register(routes) {
|
24 | return {
|
25 | module: RouterModule_1,
|
26 | providers: [
|
27 | {
|
28 | provide: exports.ROUTES,
|
29 | useValue: routes,
|
30 | },
|
31 | ],
|
32 | };
|
33 | }
|
34 | deepCloneRoutes(routes) {
|
35 | return routes.map((routeOrType) => {
|
36 | if (typeof routeOrType === 'function') {
|
37 | return routeOrType;
|
38 | }
|
39 | if (routeOrType.children) {
|
40 | return Object.assign(Object.assign({}, routeOrType), { children: this.deepCloneRoutes(routeOrType.children) });
|
41 | }
|
42 | return Object.assign({}, routeOrType);
|
43 | });
|
44 | }
|
45 | initialize() {
|
46 | const flattenedRoutes = (0, utils_1.flattenRoutePaths)(this.routes);
|
47 | flattenedRoutes.forEach(route => {
|
48 | const modulePath = (0, shared_utils_1.normalizePath)(route.path);
|
49 | this.registerModulePathMetadata(route.module, modulePath);
|
50 | this.updateTargetModulesCache(route.module);
|
51 | });
|
52 | }
|
53 | registerModulePathMetadata(moduleCtor, modulePath) {
|
54 | Reflect.defineMetadata(constants_1.MODULE_PATH + this.modulesContainer.applicationId, modulePath, moduleCtor);
|
55 | }
|
56 | updateTargetModulesCache(moduleCtor) {
|
57 | let moduleClassSet;
|
58 | if (exports.targetModulesByContainer.has(this.modulesContainer)) {
|
59 | moduleClassSet = exports.targetModulesByContainer.get(this.modulesContainer);
|
60 | }
|
61 | else {
|
62 | moduleClassSet = new WeakSet();
|
63 | exports.targetModulesByContainer.set(this.modulesContainer, moduleClassSet);
|
64 | }
|
65 | const moduleRef = Array.from(this.modulesContainer.values()).find(item => (item === null || item === void 0 ? void 0 : item.metatype) === moduleCtor);
|
66 | if (!moduleRef) {
|
67 | return;
|
68 | }
|
69 | moduleClassSet.add(moduleRef);
|
70 | }
|
71 | };
|
72 | RouterModule = RouterModule_1 = tslib_1.__decorate([
|
73 | (0, common_1.Module)({}),
|
74 | tslib_1.__param(1, (0, common_1.Inject)(exports.ROUTES)),
|
75 | tslib_1.__metadata("design:paramtypes", [modules_container_1.ModulesContainer, Array])
|
76 | ], RouterModule);
|
77 | exports.RouterModule = RouterModule;
|