1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.invokeMethod = exports.InvocationContext = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const metadata_1 = require("@loopback/metadata");
|
10 | const assert_1 = tslib_1.__importDefault(require("assert"));
|
11 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
12 | const context_1 = require("./context");
|
13 | const interceptor_1 = require("./interceptor");
|
14 | const resolver_1 = require("./resolver");
|
15 | const value_promise_1 = require("./value-promise");
|
16 | const debug = (0, debug_1.default)('loopback:context:invocation');
|
17 | const getTargetName = metadata_1.DecoratorFactory.getTargetName;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | class InvocationContext extends context_1.Context {
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | constructor(parent, target, methodName, args, source) {
|
33 | super(parent);
|
34 | this.target = target;
|
35 | this.methodName = methodName;
|
36 | this.args = args;
|
37 | this.source = source;
|
38 | }
|
39 | |
40 |
|
41 |
|
42 | get targetClass() {
|
43 | return typeof this.target === 'function'
|
44 | ? this.target
|
45 | : this.target.constructor;
|
46 | }
|
47 | |
48 |
|
49 |
|
50 | get targetName() {
|
51 | return getTargetName(this.target, this.methodName);
|
52 | }
|
53 | |
54 |
|
55 |
|
56 | get description() {
|
57 | const source = this.source == null ? '' : `${this.source} => `;
|
58 | return `InvocationContext(${this.name}): ${source}${this.targetName}`;
|
59 | }
|
60 | toString() {
|
61 | return this.description;
|
62 | }
|
63 | |
64 |
|
65 |
|
66 |
|
67 | assertMethodExists() {
|
68 | const targetWithMethods = this.target;
|
69 | if (typeof targetWithMethods[this.methodName] !== 'function') {
|
70 | const targetName = getTargetName(this.target, this.methodName);
|
71 | (0, assert_1.default)(false, `Method ${targetName} not found`);
|
72 | }
|
73 | return targetWithMethods;
|
74 | }
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 | invokeTargetMethod(options = { skipParameterInjection: true }) {
|
81 | const targetWithMethods = this.assertMethodExists();
|
82 | if (!options.skipParameterInjection) {
|
83 | return invokeTargetMethodWithInjection(this, targetWithMethods, this.methodName, this.args, options.session);
|
84 | }
|
85 | return invokeTargetMethod(this, targetWithMethods, this.methodName, this.args);
|
86 | }
|
87 | }
|
88 | exports.InvocationContext = InvocationContext;
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | function invokeMethod(target, method, ctx, nonInjectedArgs = [], options = {}) {
|
100 | if (options.skipInterceptors) {
|
101 | if (options.skipParameterInjection) {
|
102 |
|
103 | return invokeTargetMethod(ctx, target, method, nonInjectedArgs);
|
104 | }
|
105 | else {
|
106 | return invokeTargetMethodWithInjection(ctx, target, method, nonInjectedArgs, options.session);
|
107 | }
|
108 | }
|
109 |
|
110 | return (0, interceptor_1.invokeMethodWithInterceptors)(ctx, target, method, nonInjectedArgs, options);
|
111 | }
|
112 | exports.invokeMethod = invokeMethod;
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | function invokeTargetMethodWithInjection(ctx, target, method, nonInjectedArgs, session) {
|
122 | const methodName = getTargetName(target, method);
|
123 |
|
124 | if (debug.enabled) {
|
125 | debug('Invoking method %s', methodName);
|
126 | if (nonInjectedArgs === null || nonInjectedArgs === void 0 ? void 0 : nonInjectedArgs.length) {
|
127 | debug('Non-injected arguments:', nonInjectedArgs);
|
128 | }
|
129 | }
|
130 | const argsOrPromise = (0, resolver_1.resolveInjectedArguments)(target, method, ctx, session, nonInjectedArgs);
|
131 | const targetWithMethods = target;
|
132 | (0, assert_1.default)(typeof targetWithMethods[method] === 'function', `Method ${method} not found`);
|
133 | return (0, value_promise_1.transformValueOrPromise)(argsOrPromise, args => {
|
134 |
|
135 | if (debug.enabled) {
|
136 | debug('Injected arguments for %s:', methodName, args);
|
137 | }
|
138 | return invokeTargetMethod(ctx, targetWithMethods, method, args);
|
139 | });
|
140 | }
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | function invokeTargetMethod(ctx, // Not used
|
149 | target, methodName, args) {
|
150 | const targetWithMethods = target;
|
151 |
|
152 | if (debug.enabled) {
|
153 | debug('Invoking method %s', getTargetName(target, methodName), args);
|
154 | }
|
155 |
|
156 | const result = targetWithMethods[methodName](...args);
|
157 |
|
158 | if (debug.enabled) {
|
159 | debug('Method invoked: %s', getTargetName(target, methodName), result);
|
160 | }
|
161 | return result;
|
162 | }
|
163 |
|
\ | No newline at end of file |