1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const constants_1 = require("@nestjs/common/constants");
|
4 | const instance_wrapper_1 = require("../injector/instance-wrapper");
|
5 | class MiddlewareContainer {
|
6 | constructor(container) {
|
7 | this.container = container;
|
8 | this.middleware = new Map();
|
9 | this.configurationSets = new Map();
|
10 | }
|
11 | getMiddlewareCollection(moduleKey) {
|
12 | if (!this.middleware.has(moduleKey)) {
|
13 | const moduleRef = this.container.getModuleByKey(moduleKey);
|
14 | this.middleware.set(moduleKey, moduleRef.middlewares);
|
15 | }
|
16 | return this.middleware.get(moduleKey);
|
17 | }
|
18 | getConfigurations() {
|
19 | return this.configurationSets;
|
20 | }
|
21 | insertConfig(configList, moduleKey) {
|
22 | const middleware = this.getMiddlewareCollection(moduleKey);
|
23 | const targetConfig = this.getTargetConfig(moduleKey);
|
24 | const configurations = configList || [];
|
25 | const insertMiddleware = (metatype) => {
|
26 | const token = metatype.name;
|
27 | middleware.set(token, new instance_wrapper_1.InstanceWrapper({
|
28 | scope: this.getClassScope(metatype),
|
29 | metatype,
|
30 | name: token,
|
31 | }));
|
32 | };
|
33 | configurations.forEach(config => {
|
34 | [].concat(config.middleware).map(insertMiddleware);
|
35 | targetConfig.add(config);
|
36 | });
|
37 | }
|
38 | getTargetConfig(module) {
|
39 | if (!this.configurationSets.has(module)) {
|
40 | this.configurationSets.set(module, new Set());
|
41 | }
|
42 | return this.configurationSets.get(module);
|
43 | }
|
44 | getClassScope(type) {
|
45 | const metadata = Reflect.getMetadata(constants_1.SCOPE_OPTIONS_METADATA, type);
|
46 | return metadata && metadata.scope;
|
47 | }
|
48 | }
|
49 | exports.MiddlewareContainer = MiddlewareContainer;
|