moost 0.3.22 → 0.3.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.cjs +292 -310
- package/dist/index.d.ts +189 -197
- package/dist/index.mjs +283 -301
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1,31 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var mate$1 = require('@prostojs/mate');
|
|
4
|
-
var infact$1 = require('@prostojs/infact');
|
|
5
3
|
var eventCore = require('@wooksjs/event-core');
|
|
4
|
+
var infact$1 = require('@prostojs/infact');
|
|
5
|
+
var mate$1 = require('@prostojs/mate');
|
|
6
6
|
var logger = require('@prostojs/logger');
|
|
7
7
|
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
...Object.getOwnPropertyNames(instance),
|
|
14
|
-
].filter((m) => typeof instance[m] === 'function');
|
|
15
|
-
}
|
|
16
|
-
const fnProto = Object.getPrototypeOf(Function);
|
|
17
|
-
function getParentProps(constructor) {
|
|
18
|
-
const parent = Object.getPrototypeOf(constructor);
|
|
19
|
-
if (typeof parent === 'function' &&
|
|
20
|
-
parent !== fnProto &&
|
|
21
|
-
parent !== constructor &&
|
|
22
|
-
parent.prototype) {
|
|
23
|
-
return [
|
|
24
|
-
...getParentProps(parent),
|
|
25
|
-
...Object.getOwnPropertyNames(parent.prototype),
|
|
26
|
-
];
|
|
8
|
+
async function runPipes(pipes, initialValue, metas, level, restoreCtx) {
|
|
9
|
+
let v = initialValue;
|
|
10
|
+
for (const pipe of pipes) {
|
|
11
|
+
restoreCtx?.();
|
|
12
|
+
v = await pipe.handler(v, metas, level);
|
|
27
13
|
}
|
|
28
|
-
return
|
|
14
|
+
return v;
|
|
29
15
|
}
|
|
30
16
|
|
|
31
17
|
const METADATA_WORKSPACE = 'moost';
|
|
@@ -38,7 +24,7 @@ const moostMate = new mate$1.Mate(METADATA_WORKSPACE, {
|
|
|
38
24
|
return !!classMeta?.inherit;
|
|
39
25
|
}
|
|
40
26
|
if (level === 'PROP') {
|
|
41
|
-
return
|
|
27
|
+
return !!targetMeta?.inherit || !!(classMeta?.inherit && !targetMeta);
|
|
42
28
|
}
|
|
43
29
|
return !!targetMeta?.inherit;
|
|
44
30
|
},
|
|
@@ -47,15 +33,6 @@ function getMoostMate() {
|
|
|
47
33
|
return moostMate;
|
|
48
34
|
}
|
|
49
35
|
|
|
50
|
-
async function runPipes(pipes, initialValue, metas, level, restoreCtx) {
|
|
51
|
-
let v = initialValue;
|
|
52
|
-
for (const pipe of pipes) {
|
|
53
|
-
restoreCtx && restoreCtx();
|
|
54
|
-
v = await pipe.handler(v, metas, level);
|
|
55
|
-
}
|
|
56
|
-
return v;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
36
|
const sharedMoostInfact = getNewMoostInfact();
|
|
60
37
|
function getMoostInfact() {
|
|
61
38
|
return sharedMoostInfact;
|
|
@@ -64,20 +41,17 @@ function getNewMoostInfact() {
|
|
|
64
41
|
return new infact$1.Infact({
|
|
65
42
|
describeClass(classConstructor) {
|
|
66
43
|
const meta = getMoostMate().read(classConstructor);
|
|
67
|
-
|
|
44
|
+
return {
|
|
68
45
|
injectable: !!meta?.injectable,
|
|
69
46
|
global: false,
|
|
70
47
|
constructorParams: meta?.params || [],
|
|
71
48
|
provide: meta?.provide,
|
|
72
49
|
properties: meta?.properties || [],
|
|
73
|
-
scopeId: meta?.injectable === 'FOR_EVENT'
|
|
74
|
-
? eventCore.useEventId().getId()
|
|
75
|
-
: undefined,
|
|
50
|
+
scopeId: meta?.injectable === 'FOR_EVENT' ? eventCore.useEventId().getId() : undefined,
|
|
76
51
|
};
|
|
77
|
-
return infactMeta;
|
|
78
52
|
},
|
|
79
53
|
resolveParam({ paramMeta, classMeta, customData, classConstructor, index }) {
|
|
80
|
-
if (paramMeta && customData
|
|
54
|
+
if (paramMeta && customData?.pipes) {
|
|
81
55
|
return runPipes(customData.pipes, undefined, {
|
|
82
56
|
paramMeta,
|
|
83
57
|
type: classConstructor,
|
|
@@ -93,7 +67,7 @@ function getNewMoostInfact() {
|
|
|
93
67
|
return meta;
|
|
94
68
|
},
|
|
95
69
|
resolveProp({ instance, key, initialValue, propMeta, classMeta, customData, classConstructor, }) {
|
|
96
|
-
if (propMeta && customData
|
|
70
|
+
if (propMeta && customData?.pipes) {
|
|
97
71
|
return runPipes(customData.pipes, initialValue, {
|
|
98
72
|
instance,
|
|
99
73
|
type: classConstructor,
|
|
@@ -108,6 +82,180 @@ function getNewMoostInfact() {
|
|
|
108
82
|
});
|
|
109
83
|
}
|
|
110
84
|
|
|
85
|
+
function setControllerContext(controller, method) {
|
|
86
|
+
const { store } = eventCore.useEventContext();
|
|
87
|
+
const { set } = store('controller');
|
|
88
|
+
set('instance', controller);
|
|
89
|
+
set('method', method);
|
|
90
|
+
}
|
|
91
|
+
function useControllerContext() {
|
|
92
|
+
const { store } = eventCore.useEventContext();
|
|
93
|
+
const { get } = store('controller');
|
|
94
|
+
const getController = () => get('instance');
|
|
95
|
+
const getMethod = () => get('method');
|
|
96
|
+
const getControllerMeta = () => getMoostMate().read(getController());
|
|
97
|
+
const getMethodMeta = (name) => getMoostMate().read(getController(), name || getMethod());
|
|
98
|
+
function instantiate(c) {
|
|
99
|
+
return getMoostInfact().getForInstance(getController(), c);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
instantiate,
|
|
103
|
+
getController,
|
|
104
|
+
getMethod,
|
|
105
|
+
getControllerMeta,
|
|
106
|
+
getMethodMeta,
|
|
107
|
+
getPropertiesList: () => getControllerMeta()?.properties || [],
|
|
108
|
+
getScope: () => getControllerMeta()?.injectable || 'SINGLETON',
|
|
109
|
+
getParamsMeta: () => getMethodMeta()?.params || [],
|
|
110
|
+
getPropMeta: (name) => getMethodMeta(name),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const infact = getMoostInfact();
|
|
115
|
+
function registerEventScope(scopeId) {
|
|
116
|
+
infact.registerScope(scopeId);
|
|
117
|
+
return () => {
|
|
118
|
+
infact.unregisterScope(scopeId);
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function defineMoostEventHandler(options) {
|
|
122
|
+
return async () => {
|
|
123
|
+
const { restoreCtx } = eventCore.useEventContext(options.contextType);
|
|
124
|
+
const scopeId = eventCore.useEventId().getId();
|
|
125
|
+
const logger = eventCore.useEventLogger(options.loggerTitle);
|
|
126
|
+
const unscope = registerEventScope(scopeId);
|
|
127
|
+
let response;
|
|
128
|
+
const hookOptions = {
|
|
129
|
+
restoreCtx,
|
|
130
|
+
scopeId,
|
|
131
|
+
logger,
|
|
132
|
+
unscope,
|
|
133
|
+
method: options.controllerMethod,
|
|
134
|
+
getResponse: () => response,
|
|
135
|
+
reply: (r) => (response = r),
|
|
136
|
+
};
|
|
137
|
+
if (options.hooks?.init) {
|
|
138
|
+
await options.hooks.init(hookOptions);
|
|
139
|
+
restoreCtx();
|
|
140
|
+
}
|
|
141
|
+
const instance = await options.getControllerInstance();
|
|
142
|
+
restoreCtx();
|
|
143
|
+
if (instance) {
|
|
144
|
+
setControllerContext(instance, options.controllerMethod || '');
|
|
145
|
+
}
|
|
146
|
+
const interceptorHandler = await options.getIterceptorHandler();
|
|
147
|
+
if (interceptorHandler) {
|
|
148
|
+
restoreCtx();
|
|
149
|
+
try {
|
|
150
|
+
response = await interceptorHandler.init();
|
|
151
|
+
if (response !== undefined) {
|
|
152
|
+
return await endWithResponse();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
options.logErrors && logger.error(error);
|
|
157
|
+
response = error;
|
|
158
|
+
return endWithResponse(true);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
let args = [];
|
|
162
|
+
if (options.resolveArgs) {
|
|
163
|
+
restoreCtx();
|
|
164
|
+
try {
|
|
165
|
+
args = await options.resolveArgs();
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
options.logErrors && logger.error(error);
|
|
169
|
+
response = error;
|
|
170
|
+
return endWithResponse(true);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (interceptorHandler) {
|
|
174
|
+
restoreCtx();
|
|
175
|
+
response = await interceptorHandler.fireBefore(response);
|
|
176
|
+
if (response !== undefined) {
|
|
177
|
+
return endWithResponse();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const callControllerMethod = () => {
|
|
181
|
+
restoreCtx();
|
|
182
|
+
if (options.callControllerMethod) {
|
|
183
|
+
return options.callControllerMethod(args);
|
|
184
|
+
}
|
|
185
|
+
else if (instance &&
|
|
186
|
+
options.controllerMethod &&
|
|
187
|
+
typeof instance[options.controllerMethod] === 'function') {
|
|
188
|
+
return instance[options.controllerMethod](...args);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
try {
|
|
192
|
+
response = callControllerMethod();
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
options.logErrors && logger.error(error);
|
|
196
|
+
response = error;
|
|
197
|
+
return endWithResponse(true);
|
|
198
|
+
}
|
|
199
|
+
async function endWithResponse(raise = false) {
|
|
200
|
+
if (interceptorHandler) {
|
|
201
|
+
restoreCtx();
|
|
202
|
+
try {
|
|
203
|
+
response = await interceptorHandler.fireAfter(response);
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
options.logErrors && logger.error(error);
|
|
207
|
+
if (!options.manualUnscope) {
|
|
208
|
+
unscope();
|
|
209
|
+
}
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (!options.manualUnscope) {
|
|
214
|
+
unscope();
|
|
215
|
+
}
|
|
216
|
+
if (options.hooks?.end) {
|
|
217
|
+
await options.hooks.end(hookOptions);
|
|
218
|
+
}
|
|
219
|
+
if (raise) {
|
|
220
|
+
throw response;
|
|
221
|
+
}
|
|
222
|
+
return response;
|
|
223
|
+
}
|
|
224
|
+
return endWithResponse();
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function getInstanceOwnMethods(instance) {
|
|
229
|
+
const proto = Object.getPrototypeOf(instance);
|
|
230
|
+
return [
|
|
231
|
+
...getParentProps(mate$1.getConstructor(instance)),
|
|
232
|
+
...Object.getOwnPropertyNames(proto),
|
|
233
|
+
...Object.getOwnPropertyNames(instance),
|
|
234
|
+
].filter(m => typeof instance[m] === 'function');
|
|
235
|
+
}
|
|
236
|
+
const fnProto = Object.getPrototypeOf(Function);
|
|
237
|
+
function getParentProps(constructor) {
|
|
238
|
+
const parent = Object.getPrototypeOf(constructor);
|
|
239
|
+
if (typeof parent === 'function' &&
|
|
240
|
+
parent !== fnProto &&
|
|
241
|
+
parent !== constructor &&
|
|
242
|
+
parent.prototype) {
|
|
243
|
+
return [...getParentProps(parent), ...Object.getOwnPropertyNames(parent.prototype)];
|
|
244
|
+
}
|
|
245
|
+
return [];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function getDefaultLogger(topic) {
|
|
249
|
+
return new logger.ProstoLogger({
|
|
250
|
+
level: 4,
|
|
251
|
+
transports: [
|
|
252
|
+
logger.createConsoleTransort({
|
|
253
|
+
format: logger.coloredConsole,
|
|
254
|
+
}),
|
|
255
|
+
],
|
|
256
|
+
}, topic);
|
|
257
|
+
}
|
|
258
|
+
|
|
111
259
|
async function getCallableFn(targetInstance, fn, restoreCtx, pipes, logger) {
|
|
112
260
|
const mate = getMoostMate();
|
|
113
261
|
const meta = mate.read(fn);
|
|
@@ -116,16 +264,14 @@ async function getCallableFn(targetInstance, fn, restoreCtx, pipes, logger) {
|
|
|
116
264
|
infact.silent(true);
|
|
117
265
|
const instance = (await infact.getForInstance(targetInstance, fn, {
|
|
118
266
|
syncContextFn: () => {
|
|
119
|
-
restoreCtx
|
|
267
|
+
restoreCtx();
|
|
120
268
|
},
|
|
121
269
|
customData: {
|
|
122
270
|
pipes: [...(pipes || []), ...(meta.pipes || [])].sort((a, b) => a.priority - b.priority),
|
|
123
271
|
},
|
|
124
272
|
}));
|
|
125
273
|
infact.silent(false);
|
|
126
|
-
return ((...args) =>
|
|
127
|
-
return instance.handler(...args);
|
|
128
|
-
});
|
|
274
|
+
return ((...args) => instance.handler(...args));
|
|
129
275
|
}
|
|
130
276
|
if (typeof fn === 'function') {
|
|
131
277
|
return fn;
|
|
@@ -151,15 +297,16 @@ class InterceptorHandler {
|
|
|
151
297
|
const { restoreCtx } = eventCore.useEventContext();
|
|
152
298
|
for (const handler of this.handlers) {
|
|
153
299
|
restoreCtx();
|
|
154
|
-
const response = await handler(
|
|
300
|
+
const response = await handler(fn => {
|
|
155
301
|
this.before.push(fn);
|
|
156
|
-
},
|
|
302
|
+
}, fn => {
|
|
157
303
|
this.after.unshift(fn);
|
|
158
|
-
},
|
|
304
|
+
}, fn => {
|
|
159
305
|
this.onError.unshift(fn);
|
|
160
306
|
});
|
|
161
|
-
if (
|
|
307
|
+
if (response !== undefined) {
|
|
162
308
|
return response;
|
|
309
|
+
}
|
|
163
310
|
}
|
|
164
311
|
}
|
|
165
312
|
async fireBefore(response) {
|
|
@@ -168,8 +315,9 @@ class InterceptorHandler {
|
|
|
168
315
|
for (const handler of this.before) {
|
|
169
316
|
restoreCtx();
|
|
170
317
|
await handler(this.replyFn.bind(this));
|
|
171
|
-
if (this.responseOverwritten)
|
|
318
|
+
if (this.responseOverwritten) {
|
|
172
319
|
break;
|
|
320
|
+
}
|
|
173
321
|
}
|
|
174
322
|
return this.response;
|
|
175
323
|
}
|
|
@@ -237,9 +385,9 @@ async function bindControllerMethods(options) {
|
|
|
237
385
|
};
|
|
238
386
|
for (const method of methods) {
|
|
239
387
|
const methodMeta = getMoostMate().read(fakeInstance, method) || {};
|
|
240
|
-
methodMeta.handlers
|
|
241
|
-
if (!methodMeta.handlers || !methodMeta.handlers.length)
|
|
388
|
+
if (!methodMeta.handlers?.length) {
|
|
242
389
|
continue;
|
|
390
|
+
}
|
|
243
391
|
const pipes = [...(opts.pipes || []), ...(methodMeta.pipes || [])].sort((a, b) => a.priority - b.priority);
|
|
244
392
|
const interceptors = [
|
|
245
393
|
...(opts.interceptors || []),
|
|
@@ -257,8 +405,7 @@ async function bindControllerMethods(options) {
|
|
|
257
405
|
const resolveArgs = async () => {
|
|
258
406
|
const args = [];
|
|
259
407
|
const { restoreCtx } = eventCore.useEventContext();
|
|
260
|
-
for (
|
|
261
|
-
const { pipes, meta: paramMeta } = argsPipes[i];
|
|
408
|
+
for (const [i, { pipes, meta: paramMeta }] of argsPipes.entries()) {
|
|
262
409
|
args[i] = await runPipes(pipes, undefined, {
|
|
263
410
|
classMeta: meta,
|
|
264
411
|
methodMeta,
|
|
@@ -293,7 +440,9 @@ async function bindControllerMethods(options) {
|
|
|
293
440
|
handlers: methodMeta.handlers,
|
|
294
441
|
getIterceptorHandler,
|
|
295
442
|
resolveArgs,
|
|
296
|
-
logHandler: (eventName) =>
|
|
443
|
+
logHandler: (eventName) => {
|
|
444
|
+
options.logger.info(`• ${eventName} ${'[0m' + '[2m' + '[32m'}→ ${classConstructor.name}.${'[36m'}${method}${'[32m'}()`);
|
|
445
|
+
},
|
|
297
446
|
register(h, path, args) {
|
|
298
447
|
const data = wm.get(h);
|
|
299
448
|
if (data) {
|
|
@@ -309,6 +458,10 @@ async function bindControllerMethods(options) {
|
|
|
309
458
|
return controllerOverview;
|
|
310
459
|
}
|
|
311
460
|
|
|
461
|
+
function Circular(resolver) {
|
|
462
|
+
return getMoostMate().decorate('circular', resolver);
|
|
463
|
+
}
|
|
464
|
+
|
|
312
465
|
function Label(value) {
|
|
313
466
|
return getMoostMate().decorate('label', value);
|
|
314
467
|
}
|
|
@@ -327,9 +480,7 @@ function Optional() {
|
|
|
327
480
|
function Required() {
|
|
328
481
|
const mate = getMoostMate();
|
|
329
482
|
return mate.apply(mate.decorate('required', true), mate.decorateClass((meta, level, key, index) => {
|
|
330
|
-
if (typeof index !== 'number' &&
|
|
331
|
-
meta &&
|
|
332
|
-
['string', 'symbol'].includes(typeof key)) {
|
|
483
|
+
if (typeof index !== 'number' && meta && ['string', 'symbol'].includes(typeof key)) {
|
|
333
484
|
meta.requiredProps = meta.requiredProps || [];
|
|
334
485
|
meta.requiredProps.push(key);
|
|
335
486
|
}
|
|
@@ -337,58 +488,13 @@ function Required() {
|
|
|
337
488
|
}));
|
|
338
489
|
}
|
|
339
490
|
|
|
340
|
-
function Resolve(resolver, label) {
|
|
341
|
-
return (target, key, index) => {
|
|
342
|
-
const i = typeof index === 'number' ? index : undefined;
|
|
343
|
-
getMoostMate().decorate('resolver', (metas, level) => {
|
|
344
|
-
let newLabel = label;
|
|
345
|
-
if (!newLabel && level === 'PROP' && typeof metas.key === 'string') {
|
|
346
|
-
newLabel = metas.key;
|
|
347
|
-
}
|
|
348
|
-
fillLabel(target, key || '', i, newLabel);
|
|
349
|
-
return resolver(metas, level);
|
|
350
|
-
})(target, key, i);
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
function Param(name) {
|
|
354
|
-
return getMoostMate().apply(getMoostMate().decorate('paramSource', 'ROUTE'), getMoostMate().decorate('paramName', name), Resolve(() => eventCore.useRouteParams().get(name), name));
|
|
355
|
-
}
|
|
356
|
-
function Params() {
|
|
357
|
-
return Resolve(() => eventCore.useRouteParams().params, 'params');
|
|
358
|
-
}
|
|
359
|
-
function Const(value, label) {
|
|
360
|
-
return Resolve(() => value, label);
|
|
361
|
-
}
|
|
362
|
-
function ConstFactory(factory, label) {
|
|
363
|
-
return Resolve(async () => await factory(), label);
|
|
364
|
-
}
|
|
365
|
-
function InjectEventLogger(topic) {
|
|
366
|
-
return Resolve(() => eventCore.useEventLogger(topic));
|
|
367
|
-
}
|
|
368
|
-
function fillLabel(target, key, index, name) {
|
|
369
|
-
if (name) {
|
|
370
|
-
const meta = getMoostMate().read(target, key);
|
|
371
|
-
if (typeof index === 'number') {
|
|
372
|
-
if (!meta?.params ||
|
|
373
|
-
!meta?.params[index] ||
|
|
374
|
-
!meta?.params[index].label) {
|
|
375
|
-
Label(name)(target, key, index);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
else {
|
|
379
|
-
if (!meta?.label) {
|
|
380
|
-
Label(name)(target, key);
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
491
|
function Injectable(scope = true) {
|
|
387
492
|
return getMoostMate().decorate('injectable', scope);
|
|
388
493
|
}
|
|
389
|
-
const insureInjectable = getMoostMate().decorate(
|
|
390
|
-
if (!meta.injectable)
|
|
494
|
+
const insureInjectable = getMoostMate().decorate(meta => {
|
|
495
|
+
if (!meta.injectable) {
|
|
391
496
|
meta.injectable = true;
|
|
497
|
+
}
|
|
392
498
|
return meta;
|
|
393
499
|
});
|
|
394
500
|
|
|
@@ -408,9 +514,7 @@ function ImportController(prefix, controller, provide) {
|
|
|
408
514
|
}, true);
|
|
409
515
|
}
|
|
410
516
|
|
|
411
|
-
|
|
412
|
-
return getMoostMate().decorate('circular', resolver);
|
|
413
|
-
}
|
|
517
|
+
const Inherit = () => getMoostMate().decorate('inherit', true);
|
|
414
518
|
|
|
415
519
|
exports.TInterceptorPriority = void 0;
|
|
416
520
|
(function (TInterceptorPriority) {
|
|
@@ -425,25 +529,10 @@ exports.TInterceptorPriority = void 0;
|
|
|
425
529
|
function Intercept(handler, priority) {
|
|
426
530
|
return getMoostMate().decorate('interceptors', {
|
|
427
531
|
handler,
|
|
428
|
-
priority: priority ||
|
|
429
|
-
handler.priority ||
|
|
430
|
-
exports.TInterceptorPriority.INTERCEPTOR,
|
|
532
|
+
priority: priority || handler.priority || exports.TInterceptorPriority.INTERCEPTOR,
|
|
431
533
|
}, true);
|
|
432
534
|
}
|
|
433
535
|
|
|
434
|
-
function Provide(type, fn) {
|
|
435
|
-
return getMoostMate().decorate((meta) => {
|
|
436
|
-
meta.provide = meta.provide || {};
|
|
437
|
-
Object.assign(meta.provide, infact$1.createProvideRegistry([type, fn]));
|
|
438
|
-
return meta;
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
function Inject(type) {
|
|
442
|
-
return getMoostMate().decorate('inject', type);
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
const Inherit = () => getMoostMate().decorate('inherit', true);
|
|
446
|
-
|
|
447
536
|
exports.TPipePriority = void 0;
|
|
448
537
|
(function (TPipePriority) {
|
|
449
538
|
TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
|
|
@@ -459,14 +548,64 @@ exports.TPipePriority = void 0;
|
|
|
459
548
|
|
|
460
549
|
function Pipe(handler, priority) {
|
|
461
550
|
if (typeof priority !== 'number') {
|
|
462
|
-
priority =
|
|
463
|
-
typeof handler.priority === 'number'
|
|
464
|
-
? handler.priority
|
|
465
|
-
: exports.TPipePriority.TRANSFORM;
|
|
551
|
+
priority = typeof handler.priority === 'number' ? handler.priority : exports.TPipePriority.TRANSFORM;
|
|
466
552
|
}
|
|
467
553
|
return getMoostMate().decorate('pipes', { handler, priority }, true);
|
|
468
554
|
}
|
|
469
555
|
|
|
556
|
+
function Provide(type, fn) {
|
|
557
|
+
return getMoostMate().decorate(meta => {
|
|
558
|
+
meta.provide = meta.provide || {};
|
|
559
|
+
Object.assign(meta.provide, infact$1.createProvideRegistry([type, fn]));
|
|
560
|
+
return meta;
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
function Inject(type) {
|
|
564
|
+
return getMoostMate().decorate('inject', type);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
function Resolve(resolver, label) {
|
|
568
|
+
return (target, key, index) => {
|
|
569
|
+
const i = typeof index === 'number' ? index : undefined;
|
|
570
|
+
getMoostMate().decorate('resolver', (metas, level) => {
|
|
571
|
+
let newLabel = label;
|
|
572
|
+
if (!newLabel && level === 'PROP' && typeof metas.key === 'string') {
|
|
573
|
+
newLabel = metas.key;
|
|
574
|
+
}
|
|
575
|
+
fillLabel(target, key || '', i, newLabel);
|
|
576
|
+
return resolver(metas, level);
|
|
577
|
+
})(target, key, i);
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
function Param(name) {
|
|
581
|
+
return getMoostMate().apply(getMoostMate().decorate('paramSource', 'ROUTE'), getMoostMate().decorate('paramName', name), Resolve(() => eventCore.useRouteParams().get(name), name));
|
|
582
|
+
}
|
|
583
|
+
function Params() {
|
|
584
|
+
return Resolve(() => eventCore.useRouteParams().params, 'params');
|
|
585
|
+
}
|
|
586
|
+
function Const(value, label) {
|
|
587
|
+
return Resolve(() => value, label);
|
|
588
|
+
}
|
|
589
|
+
function ConstFactory(factory, label) {
|
|
590
|
+
return Resolve(async () => factory(), label);
|
|
591
|
+
}
|
|
592
|
+
function InjectEventLogger(topic) {
|
|
593
|
+
return Resolve(() => eventCore.useEventLogger(topic));
|
|
594
|
+
}
|
|
595
|
+
function fillLabel(target, key, index, name) {
|
|
596
|
+
if (name) {
|
|
597
|
+
const meta = getMoostMate().read(target, key);
|
|
598
|
+
if (typeof index === 'number') {
|
|
599
|
+
if (!meta?.params || !meta.params[index] || !meta.params[index].label) {
|
|
600
|
+
Label(name)(target, key, index);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
else if (!meta?.label) {
|
|
604
|
+
Label(name)(target, key);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
470
609
|
function defineInterceptorFn(fn, priority = exports.TInterceptorPriority.INTERCEPTOR) {
|
|
471
610
|
fn.priority = priority;
|
|
472
611
|
return fn;
|
|
@@ -491,21 +630,10 @@ const sharedPipes = [
|
|
|
491
630
|
},
|
|
492
631
|
];
|
|
493
632
|
|
|
494
|
-
function getDefaultLogger(topic) {
|
|
495
|
-
return new logger.ProstoLogger({
|
|
496
|
-
level: 4,
|
|
497
|
-
transports: [
|
|
498
|
-
logger.createConsoleTransort({
|
|
499
|
-
format: logger.coloredConsole,
|
|
500
|
-
}),
|
|
501
|
-
],
|
|
502
|
-
}, topic);
|
|
503
|
-
}
|
|
504
|
-
|
|
505
633
|
class Moost {
|
|
506
634
|
constructor(options) {
|
|
507
635
|
this.options = options;
|
|
508
|
-
this.pipes =
|
|
636
|
+
this.pipes = Array.from(sharedPipes);
|
|
509
637
|
this.interceptors = [];
|
|
510
638
|
this.adapters = [];
|
|
511
639
|
this.controllersOverview = [];
|
|
@@ -534,10 +662,7 @@ class Moost {
|
|
|
534
662
|
for (const a of this.adapters) {
|
|
535
663
|
const constructor = mate$1.getConstructor(a);
|
|
536
664
|
if (constructor) {
|
|
537
|
-
this.setProvideRegistry(infact$1.createProvideRegistry([
|
|
538
|
-
constructor,
|
|
539
|
-
() => a,
|
|
540
|
-
]));
|
|
665
|
+
this.setProvideRegistry(infact$1.createProvideRegistry([constructor, () => a]));
|
|
541
666
|
}
|
|
542
667
|
if (typeof a.getProvideRegistry === 'function') {
|
|
543
668
|
this.setProvideRegistry(a.getProvideRegistry());
|
|
@@ -554,9 +679,9 @@ class Moost {
|
|
|
554
679
|
infact.setLogger(this.logger);
|
|
555
680
|
const meta = getMoostMate();
|
|
556
681
|
const thisMeta = meta.read(this);
|
|
557
|
-
const provide = { ...
|
|
682
|
+
const provide = { ...thisMeta?.provide, ...this.provide };
|
|
558
683
|
for (const _controller of this.unregisteredControllers) {
|
|
559
|
-
let newPrefix
|
|
684
|
+
let newPrefix;
|
|
560
685
|
let controller = _controller;
|
|
561
686
|
if (Array.isArray(_controller) && typeof _controller[0] === 'string') {
|
|
562
687
|
newPrefix = _controller[0];
|
|
@@ -575,8 +700,7 @@ class Moost {
|
|
|
575
700
|
let instance;
|
|
576
701
|
const infactOpts = { provide, customData: { pipes } };
|
|
577
702
|
if (isControllerConsructor &&
|
|
578
|
-
(classMeta?.injectable === 'SINGLETON' ||
|
|
579
|
-
classMeta?.injectable === true)) {
|
|
703
|
+
(classMeta?.injectable === 'SINGLETON' || classMeta?.injectable === true)) {
|
|
580
704
|
instance = (await infact.get(controller, infactOpts));
|
|
581
705
|
}
|
|
582
706
|
else if (!isControllerConsructor) {
|
|
@@ -588,7 +712,10 @@ class Moost {
|
|
|
588
712
|
: async () => {
|
|
589
713
|
infact.silent(true);
|
|
590
714
|
const { restoreCtx } = eventCore.useEventContext();
|
|
591
|
-
const instance = (await infact.get(controller, {
|
|
715
|
+
const instance = (await infact.get(controller, {
|
|
716
|
+
...infactOpts,
|
|
717
|
+
syncContextFn: restoreCtx,
|
|
718
|
+
}));
|
|
592
719
|
infact.silent(false);
|
|
593
720
|
return instance;
|
|
594
721
|
};
|
|
@@ -601,16 +728,14 @@ class Moost {
|
|
|
601
728
|
adapters: this.adapters,
|
|
602
729
|
globalPrefix,
|
|
603
730
|
replaceOwnPrefix,
|
|
604
|
-
interceptors:
|
|
731
|
+
interceptors: Array.from(this.interceptors),
|
|
605
732
|
pipes,
|
|
606
733
|
provide: classMeta?.provide || {},
|
|
607
734
|
logger: this.logger,
|
|
608
735
|
}));
|
|
609
736
|
if (classMeta && classMeta.importController) {
|
|
610
|
-
const prefix = typeof replaceOwnPrefix === 'string'
|
|
611
|
-
|
|
612
|
-
: classMeta?.controller?.prefix;
|
|
613
|
-
const mergedProvide = { ...provide, ...(classMeta?.provide || {}) };
|
|
737
|
+
const prefix = typeof replaceOwnPrefix === 'string' ? replaceOwnPrefix : classMeta.controller?.prefix;
|
|
738
|
+
const mergedProvide = { ...provide, ...classMeta.provide };
|
|
614
739
|
for (const ic of classMeta.importController) {
|
|
615
740
|
if (ic.typeResolver) {
|
|
616
741
|
const isConstr = mate$1.isConstructor(ic.typeResolver);
|
|
@@ -619,9 +744,7 @@ class Moost {
|
|
|
619
744
|
? ic.typeResolver
|
|
620
745
|
: isFunc
|
|
621
746
|
? await ic.typeResolver()
|
|
622
|
-
: ic.typeResolver, ic.provide
|
|
623
|
-
? { ...mergedProvide, ...ic.provide }
|
|
624
|
-
: mergedProvide, `${globalPrefix}/${prefix || ''}`, ic.prefix);
|
|
747
|
+
: ic.typeResolver, ic.provide ? { ...mergedProvide, ...ic.provide } : mergedProvide, `${globalPrefix}/${prefix || ''}`, ic.prefix);
|
|
625
748
|
}
|
|
626
749
|
}
|
|
627
750
|
}
|
|
@@ -631,9 +754,7 @@ class Moost {
|
|
|
631
754
|
if (typeof item === 'function') {
|
|
632
755
|
this.pipes.push({
|
|
633
756
|
handler: item,
|
|
634
|
-
priority: typeof item.priority === 'number'
|
|
635
|
-
? item.priority
|
|
636
|
-
: exports.TPipePriority.TRANSFORM,
|
|
757
|
+
priority: typeof item.priority === 'number' ? item.priority : exports.TPipePriority.TRANSFORM,
|
|
637
758
|
});
|
|
638
759
|
}
|
|
639
760
|
else {
|
|
@@ -651,8 +772,7 @@ class Moost {
|
|
|
651
772
|
const mate = getMoostMate();
|
|
652
773
|
const thisMeta = mate.read(this);
|
|
653
774
|
const pipes = [...(this.pipes || []), ...(thisMeta?.pipes || [])].sort((a, b) => a.priority - b.priority);
|
|
654
|
-
const interceptors = [...this.interceptors, ...(thisMeta?.interceptors || [])]
|
|
655
|
-
.sort((a, b) => a.priority - b.priority);
|
|
775
|
+
const interceptors = [...this.interceptors, ...(thisMeta?.interceptors || [])].sort((a, b) => a.priority - b.priority);
|
|
656
776
|
this.globalInterceptorHandler = getIterceptorHandlerFactory(interceptors, () => Promise.resolve(this), pipes, this.logger);
|
|
657
777
|
}
|
|
658
778
|
return this.globalInterceptorHandler();
|
|
@@ -663,8 +783,7 @@ class Moost {
|
|
|
663
783
|
this.interceptors.push({
|
|
664
784
|
handler: item,
|
|
665
785
|
priority: typeof item.priority === 'number'
|
|
666
|
-
? item
|
|
667
|
-
.priority
|
|
786
|
+
? item.priority
|
|
668
787
|
: exports.TInterceptorPriority.INTERCEPTOR,
|
|
669
788
|
});
|
|
670
789
|
}
|
|
@@ -688,158 +807,21 @@ class Moost {
|
|
|
688
807
|
}
|
|
689
808
|
}
|
|
690
809
|
|
|
691
|
-
function setControllerContext(controller, method) {
|
|
692
|
-
const { store } = eventCore.useEventContext();
|
|
693
|
-
const { set } = store('controller');
|
|
694
|
-
set('instance', controller);
|
|
695
|
-
set('method', method);
|
|
696
|
-
}
|
|
697
|
-
function useControllerContext() {
|
|
698
|
-
const { store } = eventCore.useEventContext();
|
|
699
|
-
const { get } = store('controller');
|
|
700
|
-
const getController = () => get('instance');
|
|
701
|
-
const getMethod = () => get('method');
|
|
702
|
-
const getControllerMeta = () => getMoostMate().read(getController());
|
|
703
|
-
const getMethodMeta = (name) => getMoostMate().read(getController(), name || getMethod());
|
|
704
|
-
function instantiate(c) {
|
|
705
|
-
return getMoostInfact().getForInstance(getController(), c);
|
|
706
|
-
}
|
|
707
|
-
return {
|
|
708
|
-
instantiate,
|
|
709
|
-
getController,
|
|
710
|
-
getMethod,
|
|
711
|
-
getControllerMeta,
|
|
712
|
-
getMethodMeta,
|
|
713
|
-
getPropertiesList: () => getControllerMeta()?.properties || [],
|
|
714
|
-
getScope: () => getControllerMeta()?.injectable || 'SINGLETON',
|
|
715
|
-
getParamsMeta: () => getMethodMeta()?.params || [],
|
|
716
|
-
getPropMeta: (name) => getMethodMeta(name),
|
|
717
|
-
};
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
const infact = getMoostInfact();
|
|
721
|
-
function registerEventScope(scopeId) {
|
|
722
|
-
infact.registerScope(scopeId);
|
|
723
|
-
return () => infact.unregisterScope(scopeId);
|
|
724
|
-
}
|
|
725
|
-
function defineMoostEventHandler(options) {
|
|
726
|
-
return async () => {
|
|
727
|
-
const { restoreCtx } = eventCore.useEventContext(options.contextType);
|
|
728
|
-
const scopeId = eventCore.useEventId().getId();
|
|
729
|
-
const logger = eventCore.useEventLogger(options.loggerTitle);
|
|
730
|
-
const unscope = registerEventScope(scopeId);
|
|
731
|
-
let response;
|
|
732
|
-
const hookOptions = {
|
|
733
|
-
restoreCtx,
|
|
734
|
-
scopeId,
|
|
735
|
-
logger,
|
|
736
|
-
unscope,
|
|
737
|
-
method: options.controllerMethod,
|
|
738
|
-
getResponse: () => response,
|
|
739
|
-
reply: (r) => response = r,
|
|
740
|
-
};
|
|
741
|
-
if (options.hooks?.init) {
|
|
742
|
-
await options.hooks.init(hookOptions);
|
|
743
|
-
restoreCtx();
|
|
744
|
-
}
|
|
745
|
-
const instance = await options.getControllerInstance();
|
|
746
|
-
restoreCtx();
|
|
747
|
-
if (instance) {
|
|
748
|
-
setControllerContext(instance, options.controllerMethod || '');
|
|
749
|
-
}
|
|
750
|
-
const interceptorHandler = await options.getIterceptorHandler();
|
|
751
|
-
if (interceptorHandler) {
|
|
752
|
-
restoreCtx();
|
|
753
|
-
try {
|
|
754
|
-
response = await interceptorHandler.init();
|
|
755
|
-
if (typeof response !== 'undefined')
|
|
756
|
-
return endWithResponse();
|
|
757
|
-
}
|
|
758
|
-
catch (e) {
|
|
759
|
-
options.logErrors && logger.error(e);
|
|
760
|
-
response = e;
|
|
761
|
-
return endWithResponse(true);
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
let args = [];
|
|
765
|
-
if (options.resolveArgs) {
|
|
766
|
-
restoreCtx();
|
|
767
|
-
try {
|
|
768
|
-
args = await options.resolveArgs();
|
|
769
|
-
}
|
|
770
|
-
catch (e) {
|
|
771
|
-
options.logErrors && logger.error(e);
|
|
772
|
-
response = e;
|
|
773
|
-
return endWithResponse(true);
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
if (interceptorHandler) {
|
|
777
|
-
restoreCtx();
|
|
778
|
-
response = await interceptorHandler.fireBefore(response);
|
|
779
|
-
if (typeof response !== 'undefined')
|
|
780
|
-
return endWithResponse();
|
|
781
|
-
}
|
|
782
|
-
const callControllerMethod = () => {
|
|
783
|
-
restoreCtx();
|
|
784
|
-
if (options.callControllerMethod) {
|
|
785
|
-
return options.callControllerMethod(args);
|
|
786
|
-
}
|
|
787
|
-
else if (instance && options.controllerMethod && typeof instance[options.controllerMethod] === 'function') {
|
|
788
|
-
return instance[options.controllerMethod](...args);
|
|
789
|
-
}
|
|
790
|
-
};
|
|
791
|
-
try {
|
|
792
|
-
response = callControllerMethod();
|
|
793
|
-
}
|
|
794
|
-
catch (e) {
|
|
795
|
-
options.logErrors && logger.error(e);
|
|
796
|
-
response = e;
|
|
797
|
-
return endWithResponse(true);
|
|
798
|
-
}
|
|
799
|
-
async function endWithResponse(raise = false) {
|
|
800
|
-
if (interceptorHandler) {
|
|
801
|
-
restoreCtx();
|
|
802
|
-
try {
|
|
803
|
-
response = await interceptorHandler.fireAfter(response);
|
|
804
|
-
}
|
|
805
|
-
catch (e) {
|
|
806
|
-
options.logErrors && logger.error(e);
|
|
807
|
-
if (!options.manualUnscope) {
|
|
808
|
-
unscope();
|
|
809
|
-
}
|
|
810
|
-
throw e;
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
if (!options.manualUnscope) {
|
|
814
|
-
unscope();
|
|
815
|
-
}
|
|
816
|
-
if (options.hooks?.end) {
|
|
817
|
-
await options.hooks.end(hookOptions);
|
|
818
|
-
}
|
|
819
|
-
if (raise) {
|
|
820
|
-
throw response;
|
|
821
|
-
}
|
|
822
|
-
return response;
|
|
823
|
-
}
|
|
824
|
-
return await endWithResponse();
|
|
825
|
-
};
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
Object.defineProperty(exports, "createProvideRegistry", {
|
|
829
|
-
enumerable: true,
|
|
830
|
-
get: function () { return infact$1.createProvideRegistry; }
|
|
831
|
-
});
|
|
832
810
|
Object.defineProperty(exports, "EventLogger", {
|
|
833
|
-
|
|
834
|
-
|
|
811
|
+
enumerable: true,
|
|
812
|
+
get: function () { return eventCore.EventLogger; }
|
|
835
813
|
});
|
|
836
814
|
Object.defineProperty(exports, "useEventContext", {
|
|
837
|
-
|
|
838
|
-
|
|
815
|
+
enumerable: true,
|
|
816
|
+
get: function () { return eventCore.useEventContext; }
|
|
839
817
|
});
|
|
840
818
|
Object.defineProperty(exports, "useEventLogger", {
|
|
841
|
-
|
|
842
|
-
|
|
819
|
+
enumerable: true,
|
|
820
|
+
get: function () { return eventCore.useEventLogger; }
|
|
821
|
+
});
|
|
822
|
+
Object.defineProperty(exports, "createProvideRegistry", {
|
|
823
|
+
enumerable: true,
|
|
824
|
+
get: function () { return infact$1.createProvideRegistry; }
|
|
843
825
|
});
|
|
844
826
|
exports.Circular = Circular;
|
|
845
827
|
exports.Const = Const;
|