1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.AbstractInstanceResolver = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const exceptions_1 = require("../errors/exceptions");
|
6 | class AbstractInstanceResolver {
|
7 | find(typeOrToken, options) {
|
8 | const instanceLinkOrArray = this.instanceLinksHost.get(typeOrToken, options);
|
9 | const pluckInstance = ({ wrapperRef }) => {
|
10 | if (wrapperRef.scope === common_1.Scope.REQUEST ||
|
11 | wrapperRef.scope === common_1.Scope.TRANSIENT) {
|
12 | throw new exceptions_1.InvalidClassScopeException(typeOrToken);
|
13 | }
|
14 | return wrapperRef.instance;
|
15 | };
|
16 | if (Array.isArray(instanceLinkOrArray)) {
|
17 | return instanceLinkOrArray.map(pluckInstance);
|
18 | }
|
19 | return pluckInstance(instanceLinkOrArray);
|
20 | }
|
21 | async resolvePerContext(typeOrToken, contextModule, contextId, options) {
|
22 | const instanceLinkOrArray = options?.strict
|
23 | ? this.instanceLinksHost.get(typeOrToken, {
|
24 | moduleId: contextModule.id,
|
25 | each: options.each,
|
26 | })
|
27 | : this.instanceLinksHost.get(typeOrToken, {
|
28 | each: options.each,
|
29 | });
|
30 | const pluckInstance = async (instanceLink) => {
|
31 | const { wrapperRef, collection } = instanceLink;
|
32 | if (wrapperRef.isDependencyTreeStatic() && !wrapperRef.isTransient) {
|
33 | return this.get(typeOrToken, { strict: options.strict });
|
34 | }
|
35 | const ctorHost = wrapperRef.instance || { constructor: typeOrToken };
|
36 | const instance = await this.injector.loadPerContext(ctorHost, wrapperRef.host, collection, contextId, wrapperRef);
|
37 | if (!instance) {
|
38 | throw new exceptions_1.UnknownElementException();
|
39 | }
|
40 | return instance;
|
41 | };
|
42 | if (Array.isArray(instanceLinkOrArray)) {
|
43 | return Promise.all(instanceLinkOrArray.map(instanceLink => pluckInstance(instanceLink)));
|
44 | }
|
45 | return pluckInstance(instanceLinkOrArray);
|
46 | }
|
47 | }
|
48 | exports.AbstractInstanceResolver = AbstractInstanceResolver;
|