UNPKG

2.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ModuleRef = void 0;
4const common_1 = require("@nestjs/common");
5const get_class_scope_1 = require("../helpers/get-class-scope");
6const is_durable_1 = require("../helpers/is-durable");
7const abstract_instance_resolver_1 = require("./abstract-instance-resolver");
8const injector_1 = require("./injector");
9const instance_links_host_1 = require("./instance-links-host");
10const instance_wrapper_1 = require("./instance-wrapper");
11class ModuleRef extends abstract_instance_resolver_1.AbstractInstanceResolver {
12 get instanceLinksHost() {
13 if (!this._instanceLinksHost) {
14 this._instanceLinksHost = new instance_links_host_1.InstanceLinksHost(this.container);
15 }
16 return this._instanceLinksHost;
17 }
18 constructor(container) {
19 super();
20 this.container = container;
21 this.injector = new injector_1.Injector();
22 }
23 introspect(token) {
24 const { wrapperRef } = this.instanceLinksHost.get(token);
25 let scope = common_1.Scope.DEFAULT;
26 if (!wrapperRef.isDependencyTreeStatic()) {
27 scope = common_1.Scope.REQUEST;
28 }
29 else if (wrapperRef.isTransient) {
30 scope = common_1.Scope.TRANSIENT;
31 }
32 return { scope };
33 }
34 registerRequestByContextId(request, contextId) {
35 this.container.registerRequestProvider(request, contextId);
36 }
37 async instantiateClass(type, moduleRef, contextId) {
38 const wrapper = new instance_wrapper_1.InstanceWrapper({
39 name: type && type.name,
40 metatype: type,
41 isResolved: false,
42 scope: (0, get_class_scope_1.getClassScope)(type),
43 durable: (0, is_durable_1.isDurable)(type),
44 host: moduleRef,
45 });
46 return new Promise(async (resolve, reject) => {
47 try {
48 const callback = async (instances) => {
49 const properties = await this.injector.resolveProperties(wrapper, moduleRef, undefined, contextId);
50 const instance = new type(...instances);
51 this.injector.applyProperties(instance, properties);
52 resolve(instance);
53 };
54 await this.injector.resolveConstructorParams(wrapper, moduleRef, undefined, callback, contextId);
55 }
56 catch (err) {
57 reject(err);
58 }
59 });
60 }
61}
62exports.ModuleRef = ModuleRef;