1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Module = void 0;
|
4 | const random_string_generator_util_1 = require("@nestjs/common/utils/random-string-generator.util");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | const iterare_1 = require("iterare");
|
7 | const application_config_1 = require("../application-config");
|
8 | const invalid_class_exception_1 = require("../errors/exceptions/invalid-class.exception");
|
9 | const runtime_exception_1 = require("../errors/exceptions/runtime.exception");
|
10 | const unknown_export_exception_1 = require("../errors/exceptions/unknown-export.exception");
|
11 | const context_id_factory_1 = require("../helpers/context-id-factory");
|
12 | const get_class_scope_1 = require("../helpers/get-class-scope");
|
13 | const constants_1 = require("./constants");
|
14 | const instance_wrapper_1 = require("./instance-wrapper");
|
15 | const module_ref_1 = require("./module-ref");
|
16 | class Module {
|
17 | constructor(_metatype, container) {
|
18 | this._metatype = _metatype;
|
19 | this.container = container;
|
20 | this._imports = new Set();
|
21 | this._providers = new Map();
|
22 | this._injectables = new Map();
|
23 | this._middlewares = new Map();
|
24 | this._controllers = new Map();
|
25 | this._exports = new Set();
|
26 | this._distance = 0;
|
27 | this.addCoreProviders();
|
28 | this._id = (0, random_string_generator_util_1.randomStringGenerator)();
|
29 | }
|
30 | get id() {
|
31 | return this._id;
|
32 | }
|
33 | get token() {
|
34 | return this._token;
|
35 | }
|
36 | set token(token) {
|
37 | this._token = token;
|
38 | }
|
39 | get providers() {
|
40 | return this._providers;
|
41 | }
|
42 | get middlewares() {
|
43 | return this._middlewares;
|
44 | }
|
45 | get imports() {
|
46 | return this._imports;
|
47 | }
|
48 | |
49 |
|
50 |
|
51 | get relatedModules() {
|
52 | return this._imports;
|
53 | }
|
54 | |
55 |
|
56 |
|
57 | get components() {
|
58 | return this._providers;
|
59 | }
|
60 | |
61 |
|
62 |
|
63 | get routes() {
|
64 | return this._controllers;
|
65 | }
|
66 | get injectables() {
|
67 | return this._injectables;
|
68 | }
|
69 | get controllers() {
|
70 | return this._controllers;
|
71 | }
|
72 | get exports() {
|
73 | return this._exports;
|
74 | }
|
75 | get instance() {
|
76 | if (!this._providers.has(this._metatype)) {
|
77 | throw new runtime_exception_1.RuntimeException();
|
78 | }
|
79 | const module = this._providers.get(this._metatype);
|
80 | return module.instance;
|
81 | }
|
82 | get metatype() {
|
83 | return this._metatype;
|
84 | }
|
85 | get distance() {
|
86 | return this._distance;
|
87 | }
|
88 | set distance(value) {
|
89 | this._distance = value;
|
90 | }
|
91 | addCoreProviders() {
|
92 | this.addModuleAsProvider();
|
93 | this.addModuleRef();
|
94 | this.addApplicationConfig();
|
95 | }
|
96 | addModuleRef() {
|
97 | const moduleRef = this.createModuleReferenceType();
|
98 | this._providers.set(module_ref_1.ModuleRef, new instance_wrapper_1.InstanceWrapper({
|
99 | token: module_ref_1.ModuleRef,
|
100 | name: module_ref_1.ModuleRef.name,
|
101 | metatype: module_ref_1.ModuleRef,
|
102 | isResolved: true,
|
103 | instance: new moduleRef(),
|
104 | host: this,
|
105 | }));
|
106 | }
|
107 | addModuleAsProvider() {
|
108 | this._providers.set(this._metatype, new instance_wrapper_1.InstanceWrapper({
|
109 | token: this._metatype,
|
110 | name: this._metatype.name,
|
111 | metatype: this._metatype,
|
112 | isResolved: false,
|
113 | instance: null,
|
114 | host: this,
|
115 | }));
|
116 | }
|
117 | addApplicationConfig() {
|
118 | this._providers.set(application_config_1.ApplicationConfig, new instance_wrapper_1.InstanceWrapper({
|
119 | token: application_config_1.ApplicationConfig,
|
120 | name: application_config_1.ApplicationConfig.name,
|
121 | isResolved: true,
|
122 | instance: this.container.applicationConfig,
|
123 | host: this,
|
124 | }));
|
125 | }
|
126 | addInjectable(injectable, host) {
|
127 | if (this.isCustomProvider(injectable)) {
|
128 | return this.addCustomProvider(injectable, this._injectables);
|
129 | }
|
130 | let instanceWrapper = this.injectables.get(injectable);
|
131 | if (!instanceWrapper) {
|
132 | instanceWrapper = new instance_wrapper_1.InstanceWrapper({
|
133 | token: injectable,
|
134 | name: injectable.name,
|
135 | metatype: injectable,
|
136 | instance: null,
|
137 | isResolved: false,
|
138 | scope: (0, get_class_scope_1.getClassScope)(injectable),
|
139 | host: this,
|
140 | });
|
141 | this._injectables.set(injectable, instanceWrapper);
|
142 | }
|
143 | if (host) {
|
144 | const hostWrapper = this._controllers.get(host) || this._providers.get(host);
|
145 | hostWrapper && hostWrapper.addEnhancerMetadata(instanceWrapper);
|
146 | }
|
147 | }
|
148 | addProvider(provider) {
|
149 | if (this.isCustomProvider(provider)) {
|
150 | return this.addCustomProvider(provider, this._providers);
|
151 | }
|
152 | this._providers.set(provider, new instance_wrapper_1.InstanceWrapper({
|
153 | token: provider,
|
154 | name: provider.name,
|
155 | metatype: provider,
|
156 | instance: null,
|
157 | isResolved: false,
|
158 | scope: (0, get_class_scope_1.getClassScope)(provider),
|
159 | host: this,
|
160 | }));
|
161 | return provider;
|
162 | }
|
163 | isCustomProvider(provider) {
|
164 | return !(0, shared_utils_1.isNil)(provider.provide);
|
165 | }
|
166 | addCustomProvider(provider, collection) {
|
167 | if (this.isCustomClass(provider)) {
|
168 | this.addCustomClass(provider, collection);
|
169 | }
|
170 | else if (this.isCustomValue(provider)) {
|
171 | this.addCustomValue(provider, collection);
|
172 | }
|
173 | else if (this.isCustomFactory(provider)) {
|
174 | this.addCustomFactory(provider, collection);
|
175 | }
|
176 | else if (this.isCustomUseExisting(provider)) {
|
177 | this.addCustomUseExisting(provider, collection);
|
178 | }
|
179 | return provider.provide;
|
180 | }
|
181 | isCustomClass(provider) {
|
182 | return !(0, shared_utils_1.isUndefined)(provider.useClass);
|
183 | }
|
184 | isCustomValue(provider) {
|
185 | return !(0, shared_utils_1.isUndefined)(provider.useValue);
|
186 | }
|
187 | isCustomFactory(provider) {
|
188 | return !(0, shared_utils_1.isUndefined)(provider.useFactory);
|
189 | }
|
190 | isCustomUseExisting(provider) {
|
191 | return !(0, shared_utils_1.isUndefined)(provider.useExisting);
|
192 | }
|
193 | isDynamicModule(exported) {
|
194 | return exported && exported.module;
|
195 | }
|
196 | addCustomClass(provider, collection) {
|
197 | let { scope } = provider;
|
198 | const { useClass } = provider;
|
199 | if ((0, shared_utils_1.isUndefined)(scope)) {
|
200 | scope = (0, get_class_scope_1.getClassScope)(useClass);
|
201 | }
|
202 | collection.set(provider.provide, new instance_wrapper_1.InstanceWrapper({
|
203 | token: provider.provide,
|
204 | name: (useClass === null || useClass === void 0 ? void 0 : useClass.name) || useClass,
|
205 | metatype: useClass,
|
206 | instance: null,
|
207 | isResolved: false,
|
208 | scope,
|
209 | host: this,
|
210 | }));
|
211 | }
|
212 | addCustomValue(provider, collection) {
|
213 | const { useValue: value, provide: providerToken } = provider;
|
214 | collection.set(providerToken, new instance_wrapper_1.InstanceWrapper({
|
215 | token: providerToken,
|
216 | name: (providerToken === null || providerToken === void 0 ? void 0 : providerToken.name) || providerToken,
|
217 | metatype: null,
|
218 | instance: value,
|
219 | isResolved: true,
|
220 | async: value instanceof Promise,
|
221 | host: this,
|
222 | }));
|
223 | }
|
224 | addCustomFactory(provider, collection) {
|
225 | const { useFactory: factory, inject, scope, provide: providerToken, } = provider;
|
226 | collection.set(providerToken, new instance_wrapper_1.InstanceWrapper({
|
227 | token: providerToken,
|
228 | name: (providerToken === null || providerToken === void 0 ? void 0 : providerToken.name) || providerToken,
|
229 | metatype: factory,
|
230 | instance: null,
|
231 | isResolved: false,
|
232 | inject: inject || [],
|
233 | scope,
|
234 | host: this,
|
235 | }));
|
236 | }
|
237 | addCustomUseExisting(provider, collection) {
|
238 | const { useExisting, provide: providerToken } = provider;
|
239 | collection.set(providerToken, new instance_wrapper_1.InstanceWrapper({
|
240 | token: providerToken,
|
241 | name: (providerToken === null || providerToken === void 0 ? void 0 : providerToken.name) || providerToken,
|
242 | metatype: (instance => instance),
|
243 | instance: null,
|
244 | isResolved: false,
|
245 | inject: [useExisting],
|
246 | host: this,
|
247 | isAlias: true,
|
248 | }));
|
249 | }
|
250 | addExportedProvider(provider) {
|
251 | const addExportedUnit = (token) => this._exports.add(this.validateExportedProvider(token));
|
252 | if (this.isCustomProvider(provider)) {
|
253 | return this.addCustomExportedProvider(provider);
|
254 | }
|
255 | else if ((0, shared_utils_1.isString)(provider) || (0, shared_utils_1.isSymbol)(provider)) {
|
256 | return addExportedUnit(provider);
|
257 | }
|
258 | else if (this.isDynamicModule(provider)) {
|
259 | const { module: moduleClassRef } = provider;
|
260 | return addExportedUnit(moduleClassRef);
|
261 | }
|
262 | addExportedUnit(provider);
|
263 | }
|
264 | addCustomExportedProvider(provider) {
|
265 | const provide = provider.provide;
|
266 | if ((0, shared_utils_1.isString)(provide) || (0, shared_utils_1.isSymbol)(provide)) {
|
267 | return this._exports.add(this.validateExportedProvider(provide));
|
268 | }
|
269 | this._exports.add(this.validateExportedProvider(provide));
|
270 | }
|
271 | validateExportedProvider(token) {
|
272 | if (this._providers.has(token)) {
|
273 | return token;
|
274 | }
|
275 | const imports = (0, iterare_1.iterate)(this._imports.values())
|
276 | .filter(item => !!item)
|
277 | .map(({ metatype }) => metatype)
|
278 | .filter(metatype => !!metatype)
|
279 | .toArray();
|
280 | if (!imports.includes(token)) {
|
281 | const { name } = this.metatype;
|
282 | const providerName = (0, shared_utils_1.isFunction)(token) ? token.name : token;
|
283 | throw new unknown_export_exception_1.UnknownExportException(providerName, name);
|
284 | }
|
285 | return token;
|
286 | }
|
287 | addController(controller) {
|
288 | this._controllers.set(controller, new instance_wrapper_1.InstanceWrapper({
|
289 | token: controller,
|
290 | name: controller.name,
|
291 | metatype: controller,
|
292 | instance: null,
|
293 | isResolved: false,
|
294 | scope: (0, get_class_scope_1.getClassScope)(controller),
|
295 | host: this,
|
296 | }));
|
297 | this.assignControllerUniqueId(controller);
|
298 | }
|
299 | assignControllerUniqueId(controller) {
|
300 | Object.defineProperty(controller, constants_1.CONTROLLER_ID_KEY, {
|
301 | enumerable: false,
|
302 | writable: false,
|
303 | configurable: true,
|
304 | value: (0, random_string_generator_util_1.randomStringGenerator)(),
|
305 | });
|
306 | }
|
307 | addRelatedModule(module) {
|
308 | this._imports.add(module);
|
309 | }
|
310 | replace(toReplace, options) {
|
311 | if (options.isProvider && this.hasProvider(toReplace)) {
|
312 | const originalProvider = this._providers.get(toReplace);
|
313 | return originalProvider.mergeWith(Object.assign({ provide: toReplace }, options));
|
314 | }
|
315 | else if (!options.isProvider && this.hasInjectable(toReplace)) {
|
316 | const originalInjectable = this._injectables.get(toReplace);
|
317 | return originalInjectable.mergeWith(Object.assign({ provide: toReplace }, options));
|
318 | }
|
319 | }
|
320 | hasProvider(token) {
|
321 | return this._providers.has(token);
|
322 | }
|
323 | hasInjectable(token) {
|
324 | return this._injectables.has(token);
|
325 | }
|
326 | getProviderByKey(name) {
|
327 | return this._providers.get(name);
|
328 | }
|
329 | getNonAliasProviders() {
|
330 | return [...this._providers].filter(([_, wrapper]) => !wrapper.isAlias);
|
331 | }
|
332 | createModuleReferenceType() {
|
333 |
|
334 | const self = this;
|
335 | return class extends module_ref_1.ModuleRef {
|
336 | constructor() {
|
337 | super(self.container);
|
338 | }
|
339 | get(typeOrToken, options = { strict: true }) {
|
340 | return !(options && options.strict)
|
341 | ? this.find(typeOrToken)
|
342 | : this.find(typeOrToken, self);
|
343 | }
|
344 | resolve(typeOrToken, contextId = (0, context_id_factory_1.createContextId)(), options = { strict: true }) {
|
345 | return this.resolvePerContext(typeOrToken, self, contextId, options);
|
346 | }
|
347 | async create(type) {
|
348 | if (!(type && (0, shared_utils_1.isFunction)(type) && type.prototype)) {
|
349 | throw new invalid_class_exception_1.InvalidClassException(type);
|
350 | }
|
351 | return this.instantiateClass(type, self);
|
352 | }
|
353 | };
|
354 | }
|
355 | }
|
356 | exports.Module = Module;
|