1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ModuleRef = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const invalid_class_scope_exception_1 = require("../errors/exceptions/invalid-class-scope.exception");
|
6 | const unknown_element_exception_1 = require("../errors/exceptions/unknown-element.exception");
|
7 | const get_class_scope_1 = require("../helpers/get-class-scope");
|
8 | const injector_1 = require("./injector");
|
9 | const instance_links_host_1 = require("./instance-links-host");
|
10 | const instance_wrapper_1 = require("./instance-wrapper");
|
11 | class ModuleRef {
|
12 | constructor(container) {
|
13 | this.container = container;
|
14 | this.injector = new injector_1.Injector();
|
15 | }
|
16 | get instanceLinksHost() {
|
17 | if (!this._instanceLinksHost) {
|
18 | this._instanceLinksHost = new instance_links_host_1.InstanceLinksHost(this.container);
|
19 | }
|
20 | return this._instanceLinksHost;
|
21 | }
|
22 | introspect(token) {
|
23 | const { wrapperRef } = this.instanceLinksHost.get(token);
|
24 | let scope = common_1.Scope.DEFAULT;
|
25 | if (!wrapperRef.isDependencyTreeStatic()) {
|
26 | scope = common_1.Scope.REQUEST;
|
27 | }
|
28 | else if (wrapperRef.isTransient) {
|
29 | scope = common_1.Scope.TRANSIENT;
|
30 | }
|
31 | return { scope };
|
32 | }
|
33 | registerRequestByContextId(request, contextId) {
|
34 | this.container.registerRequestProvider(request, contextId);
|
35 | }
|
36 | find(typeOrToken, contextModule) {
|
37 | const moduleId = contextModule && contextModule.id;
|
38 | const { wrapperRef } = this.instanceLinksHost.get(typeOrToken, moduleId);
|
39 | if (wrapperRef.scope === common_1.Scope.REQUEST ||
|
40 | wrapperRef.scope === common_1.Scope.TRANSIENT) {
|
41 | throw new invalid_class_scope_exception_1.InvalidClassScopeException(typeOrToken);
|
42 | }
|
43 | return wrapperRef.instance;
|
44 | }
|
45 | async resolvePerContext(typeOrToken, contextModule, contextId, options) {
|
46 | const isStrictModeEnabled = options && options.strict;
|
47 | const instanceLink = isStrictModeEnabled
|
48 | ? this.instanceLinksHost.get(typeOrToken, contextModule.id)
|
49 | : this.instanceLinksHost.get(typeOrToken);
|
50 | const { wrapperRef, collection } = instanceLink;
|
51 | if (wrapperRef.isDependencyTreeStatic() && !wrapperRef.isTransient) {
|
52 | return this.get(typeOrToken, options);
|
53 | }
|
54 | const ctorHost = wrapperRef.instance || { constructor: typeOrToken };
|
55 | const instance = await this.injector.loadPerContext(ctorHost, wrapperRef.host, collection, contextId, wrapperRef);
|
56 | if (!instance) {
|
57 | throw new unknown_element_exception_1.UnknownElementException();
|
58 | }
|
59 | return instance;
|
60 | }
|
61 | async instantiateClass(type, moduleRef) {
|
62 | const wrapper = new instance_wrapper_1.InstanceWrapper({
|
63 | name: type && type.name,
|
64 | metatype: type,
|
65 | isResolved: false,
|
66 | scope: (0, get_class_scope_1.getClassScope)(type),
|
67 | host: moduleRef,
|
68 | });
|
69 | return new Promise(async (resolve, reject) => {
|
70 | try {
|
71 | const callback = async (instances) => {
|
72 | const properties = await this.injector.resolveProperties(wrapper, moduleRef);
|
73 | const instance = new type(...instances);
|
74 | this.injector.applyProperties(instance, properties);
|
75 | resolve(instance);
|
76 | };
|
77 | await this.injector.resolveConstructorParams(wrapper, moduleRef, undefined, callback);
|
78 | }
|
79 | catch (err) {
|
80 | reject(err);
|
81 | }
|
82 | });
|
83 | }
|
84 | }
|
85 | exports.ModuleRef = ModuleRef;
|