UNPKG

7.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const constants_1 = require("@nestjs/common/constants");
4const circular_dependency_exception_1 = require("../errors/exceptions/circular-dependency.exception");
5const invalid_module_exception_1 = require("../errors/exceptions/invalid-module.exception");
6const unknown_module_exception_1 = require("../errors/exceptions/unknown-module.exception");
7const external_context_creator_1 = require("../helpers/external-context-creator");
8const http_adapter_host_1 = require("../helpers/http-adapter-host");
9const request_constants_1 = require("../router/request/request-constants");
10const compiler_1 = require("./compiler");
11const internal_core_module_1 = require("./internal-core-module");
12const internal_providers_storage_1 = require("./internal-providers-storage");
13const module_1 = require("./module");
14const module_token_factory_1 = require("./module-token-factory");
15const modules_container_1 = require("./modules-container");
16class NestContainer {
17 constructor(_applicationConfig = undefined) {
18 this._applicationConfig = _applicationConfig;
19 this.globalModules = new Set();
20 this.moduleTokenFactory = new module_token_factory_1.ModuleTokenFactory();
21 this.moduleCompiler = new compiler_1.ModuleCompiler(this.moduleTokenFactory);
22 this.modules = new modules_container_1.ModulesContainer();
23 this.dynamicModulesMetadata = new Map();
24 this.internalProvidersStorage = new internal_providers_storage_1.InternalProvidersStorage();
25 }
26 get applicationConfig() {
27 return this._applicationConfig;
28 }
29 setHttpAdapter(httpAdapter) {
30 this.internalProvidersStorage.httpAdapter = httpAdapter;
31 if (!this.internalProvidersStorage.httpAdapterHost) {
32 return;
33 }
34 const host = this.internalProvidersStorage.httpAdapterHost;
35 host.httpAdapter = httpAdapter;
36 }
37 getHttpAdapterRef() {
38 return this.internalProvidersStorage.httpAdapter;
39 }
40 async addModule(metatype, scope) {
41 if (!metatype) {
42 throw new invalid_module_exception_1.InvalidModuleException(scope);
43 }
44 const { type, dynamicMetadata, token } = await this.moduleCompiler.compile(metatype, scope);
45 if (this.modules.has(token)) {
46 return;
47 }
48 const module = new module_1.Module(type, scope, this);
49 this.modules.set(token, module);
50 this.addDynamicMetadata(token, dynamicMetadata, [].concat(scope, type));
51 if (this.isGlobalModule(type, dynamicMetadata)) {
52 this.addGlobalModule(module);
53 }
54 return module;
55 }
56 addDynamicMetadata(token, dynamicModuleMetadata, scope) {
57 if (!dynamicModuleMetadata) {
58 return;
59 }
60 this.dynamicModulesMetadata.set(token, dynamicModuleMetadata);
61 const { imports } = dynamicModuleMetadata;
62 this.addDynamicModules(imports, scope);
63 }
64 addDynamicModules(modules, scope) {
65 if (!modules) {
66 return;
67 }
68 modules.forEach(module => this.addModule(module, scope));
69 }
70 isGlobalModule(metatype, dynamicMetadata) {
71 if (dynamicMetadata && dynamicMetadata.global) {
72 return true;
73 }
74 return !!Reflect.getMetadata(constants_1.GLOBAL_MODULE_METADATA, metatype);
75 }
76 addGlobalModule(module) {
77 this.globalModules.add(module);
78 }
79 getModules() {
80 return this.modules;
81 }
82 getModuleByKey(moduleKey) {
83 return this.modules.get(moduleKey);
84 }
85 getInternalCoreModuleRef() {
86 return this.internalCoreModule;
87 }
88 async addImport(relatedModule, token) {
89 if (!this.modules.has(token)) {
90 return;
91 }
92 const module = this.modules.get(token);
93 const parent = module.metatype;
94 const scope = [].concat(module.scope, parent);
95 const { token: relatedModuleToken } = await this.moduleCompiler.compile(relatedModule, scope);
96 const related = this.modules.get(relatedModuleToken);
97 module.addRelatedModule(related);
98 }
99 addProvider(provider, token) {
100 if (!provider) {
101 throw new circular_dependency_exception_1.CircularDependencyException();
102 }
103 if (!this.modules.has(token)) {
104 throw new unknown_module_exception_1.UnknownModuleException();
105 }
106 const module = this.modules.get(token);
107 return module.addProvider(provider);
108 }
109 addInjectable(injectable, token, host) {
110 if (!this.modules.has(token)) {
111 throw new unknown_module_exception_1.UnknownModuleException();
112 }
113 const module = this.modules.get(token);
114 module.addInjectable(injectable, host);
115 }
116 addExportedProvider(provider, token) {
117 if (!this.modules.has(token)) {
118 throw new unknown_module_exception_1.UnknownModuleException();
119 }
120 const module = this.modules.get(token);
121 module.addExportedProvider(provider);
122 }
123 addController(controller, token) {
124 if (!this.modules.has(token)) {
125 throw new unknown_module_exception_1.UnknownModuleException();
126 }
127 const module = this.modules.get(token);
128 module.addController(controller);
129 }
130 clear() {
131 this.modules.clear();
132 }
133 replace(toReplace, options) {
134 this.modules.forEach(module => module.replace(toReplace, options));
135 }
136 bindGlobalScope() {
137 this.modules.forEach(module => this.bindGlobalsToImports(module));
138 }
139 bindGlobalsToImports(module) {
140 this.globalModules.forEach(globalModule => this.bindGlobalModuleToModule(module, globalModule));
141 }
142 bindGlobalModuleToModule(target, globalModule) {
143 target !== globalModule && target.addRelatedModule(globalModule);
144 }
145 getDynamicMetadataByToken(token, metadataKey) {
146 const metadata = this.dynamicModulesMetadata.get(token);
147 if (metadata && metadata[metadataKey]) {
148 return metadata[metadataKey];
149 }
150 return [];
151 }
152 createCoreModule() {
153 return internal_core_module_1.InternalCoreModule.register([
154 {
155 provide: external_context_creator_1.ExternalContextCreator,
156 useValue: external_context_creator_1.ExternalContextCreator.fromContainer(this),
157 },
158 {
159 provide: modules_container_1.ModulesContainer,
160 useValue: this.modules,
161 },
162 {
163 provide: http_adapter_host_1.HttpAdapterHost,
164 useValue: this.internalProvidersStorage.httpAdapterHost,
165 },
166 ]);
167 }
168 registerCoreModuleRef(moduleRef) {
169 this.internalCoreModule = moduleRef;
170 this.modules[internal_core_module_1.InternalCoreModule.name] = moduleRef;
171 }
172 getModuleTokenFactory() {
173 return this.moduleTokenFactory;
174 }
175 registerRequestProvider(request, contextId) {
176 const wrapper = this.internalCoreModule.getProviderByKey(request_constants_1.REQUEST);
177 wrapper.setInstanceByContextId(contextId, {
178 instance: request,
179 isResolved: true,
180 });
181 }
182}
183exports.NestContainer = NestContainer;