moost 0.2.26 → 0.2.28
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 +7 -3
- package/dist/index.cjs +633 -193
- package/dist/index.d.ts +254 -16
- package/dist/index.mjs +608 -176
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var mate = require('@prostojs/mate');
|
|
4
|
-
var infact = require('@prostojs/infact');
|
|
3
|
+
var mate$1 = require('@prostojs/mate');
|
|
4
|
+
var infact$1 = require('@prostojs/infact');
|
|
5
5
|
var eventCore = require('@wooksjs/event-core');
|
|
6
6
|
var valido$1 = require('@prostojs/valido');
|
|
7
7
|
var logger = require('@prostojs/logger');
|
|
@@ -20,6 +20,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
20
20
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
21
21
|
PERFORMANCE OF THIS SOFTWARE.
|
|
22
22
|
***************************************************************************** */
|
|
23
|
+
/* global Reflect, Promise */
|
|
24
|
+
|
|
23
25
|
|
|
24
26
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
25
27
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
@@ -32,7 +34,7 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
const METADATA_WORKSPACE = 'moost';
|
|
35
|
-
const moostMate = new mate.Mate(METADATA_WORKSPACE, {
|
|
37
|
+
const moostMate = new mate$1.Mate(METADATA_WORKSPACE, {
|
|
36
38
|
readType: true,
|
|
37
39
|
readReturnType: true,
|
|
38
40
|
collectPropKeys: true,
|
|
@@ -41,7 +43,7 @@ const moostMate = new mate.Mate(METADATA_WORKSPACE, {
|
|
|
41
43
|
return !!(classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit);
|
|
42
44
|
}
|
|
43
45
|
if (level === 'PROP') {
|
|
44
|
-
return !!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit) || !!((classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit) && !targetMeta);
|
|
46
|
+
return (!!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit) || !!((classMeta === null || classMeta === void 0 ? void 0 : classMeta.inherit) && !targetMeta));
|
|
45
47
|
}
|
|
46
48
|
return !!(targetMeta === null || targetMeta === void 0 ? void 0 : targetMeta.inherit);
|
|
47
49
|
},
|
|
@@ -66,7 +68,7 @@ function getMoostInfact() {
|
|
|
66
68
|
return sharedMoostInfact;
|
|
67
69
|
}
|
|
68
70
|
function getNewMoostInfact() {
|
|
69
|
-
return new infact.Infact({
|
|
71
|
+
return new infact$1.Infact({
|
|
70
72
|
describeClass(classConstructor) {
|
|
71
73
|
const meta = getMoostMate().read(classConstructor);
|
|
72
74
|
const infactMeta = {
|
|
@@ -75,22 +77,32 @@ function getNewMoostInfact() {
|
|
|
75
77
|
constructorParams: (meta === null || meta === void 0 ? void 0 : meta.params) || [],
|
|
76
78
|
provide: meta === null || meta === void 0 ? void 0 : meta.provide,
|
|
77
79
|
properties: (meta === null || meta === void 0 ? void 0 : meta.properties) || [],
|
|
78
|
-
scopeId: (meta === null || meta === void 0 ? void 0 : meta.injectable) === 'FOR_EVENT'
|
|
80
|
+
scopeId: (meta === null || meta === void 0 ? void 0 : meta.injectable) === 'FOR_EVENT'
|
|
81
|
+
? eventCore.useEventId().getId()
|
|
82
|
+
: undefined,
|
|
79
83
|
};
|
|
80
84
|
return infactMeta;
|
|
81
85
|
},
|
|
82
86
|
resolveParam({ paramMeta, classMeta, customData }) {
|
|
83
87
|
if (paramMeta && customData && customData.pipes) {
|
|
84
|
-
return runPipes(customData.pipes, undefined, {
|
|
88
|
+
return runPipes(customData.pipes, undefined, {
|
|
89
|
+
paramMeta,
|
|
90
|
+
classMeta: classMeta,
|
|
91
|
+
}, 'PARAM');
|
|
85
92
|
}
|
|
86
93
|
},
|
|
87
94
|
describeProp(classConstructor, key) {
|
|
88
95
|
const meta = getMoostMate().read(classConstructor, key);
|
|
89
96
|
return meta;
|
|
90
97
|
},
|
|
91
|
-
resolveProp({ instance, key, initialValue, propMeta, classMeta, customData }) {
|
|
98
|
+
resolveProp({ instance, key, initialValue, propMeta, classMeta, customData, }) {
|
|
92
99
|
if (propMeta && customData && customData.pipes) {
|
|
93
|
-
return runPipes(customData.pipes, initialValue, {
|
|
100
|
+
return runPipes(customData.pipes, initialValue, {
|
|
101
|
+
instance,
|
|
102
|
+
key,
|
|
103
|
+
propMeta,
|
|
104
|
+
classMeta: classMeta,
|
|
105
|
+
}, 'PROP');
|
|
94
106
|
}
|
|
95
107
|
},
|
|
96
108
|
storeProvideRegByInstance: true,
|
|
@@ -101,31 +113,41 @@ function getInstanceOwnMethods(instance) {
|
|
|
101
113
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
102
114
|
const proto = Object.getPrototypeOf(instance);
|
|
103
115
|
return [
|
|
104
|
-
...getParentProps(mate.getConstructor(instance)),
|
|
116
|
+
...getParentProps(mate$1.getConstructor(instance)),
|
|
105
117
|
...Object.getOwnPropertyNames(proto),
|
|
106
118
|
...Object.getOwnPropertyNames(instance),
|
|
107
|
-
].filter(m => typeof instance[m] === 'function');
|
|
119
|
+
].filter((m) => typeof instance[m] === 'function');
|
|
108
120
|
}
|
|
109
121
|
const fnProto = Object.getPrototypeOf(Function);
|
|
110
122
|
function getParentProps(constructor) {
|
|
111
123
|
const parent = Object.getPrototypeOf(constructor);
|
|
112
|
-
if (typeof parent === 'function' &&
|
|
113
|
-
|
|
124
|
+
if (typeof parent === 'function' &&
|
|
125
|
+
parent !== fnProto &&
|
|
126
|
+
parent !== constructor &&
|
|
127
|
+
parent.prototype) {
|
|
128
|
+
return [
|
|
129
|
+
...getParentProps(parent),
|
|
130
|
+
...Object.getOwnPropertyNames(parent.prototype),
|
|
131
|
+
];
|
|
114
132
|
}
|
|
115
133
|
return [];
|
|
116
134
|
}
|
|
117
135
|
|
|
118
136
|
function getCallableFn(targetInstance, fn, restoreCtx, pipes, logger) {
|
|
119
137
|
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
-
const mate
|
|
121
|
-
const meta = mate
|
|
138
|
+
const mate = getMoostMate();
|
|
139
|
+
const meta = mate.read(fn);
|
|
122
140
|
if (meta === null || meta === void 0 ? void 0 : meta.injectable) {
|
|
123
141
|
const infact = getMoostInfact();
|
|
124
142
|
infact.silent(true);
|
|
125
|
-
const instance = yield infact.getForInstance(targetInstance, fn, {
|
|
126
|
-
syncContextFn: () => {
|
|
127
|
-
|
|
128
|
-
|
|
143
|
+
const instance = (yield infact.getForInstance(targetInstance, fn, {
|
|
144
|
+
syncContextFn: () => {
|
|
145
|
+
restoreCtx && restoreCtx();
|
|
146
|
+
},
|
|
147
|
+
customData: {
|
|
148
|
+
pipes: [...(pipes || []), ...(meta.pipes || [])].sort((a, b) => a.priority - b.priority),
|
|
149
|
+
},
|
|
150
|
+
}));
|
|
129
151
|
infact.silent(false);
|
|
130
152
|
return ((...args) => {
|
|
131
153
|
return instance.handler(...args);
|
|
@@ -134,7 +156,7 @@ function getCallableFn(targetInstance, fn, restoreCtx, pipes, logger) {
|
|
|
134
156
|
if (typeof fn === 'function') {
|
|
135
157
|
return fn;
|
|
136
158
|
}
|
|
137
|
-
const e = new Error(`getCallableFn failed for "${mate.getConstructor(targetInstance).name}" because the passed arg is not a Function nor TClassFunction`);
|
|
159
|
+
const e = new Error(`getCallableFn failed for "${mate$1.getConstructor(targetInstance).name}" because the passed arg is not a Function nor TClassFunction`);
|
|
138
160
|
logger.error(e);
|
|
139
161
|
throw e;
|
|
140
162
|
});
|
|
@@ -157,7 +179,15 @@ class InterceptorHandler {
|
|
|
157
179
|
const { restoreCtx } = eventCore.useEventContext();
|
|
158
180
|
for (const handler of this.handlers) {
|
|
159
181
|
restoreCtx();
|
|
160
|
-
|
|
182
|
+
const response = yield handler((fn) => {
|
|
183
|
+
this.before.push(fn);
|
|
184
|
+
}, (fn) => {
|
|
185
|
+
this.after.unshift(fn);
|
|
186
|
+
}, (fn) => {
|
|
187
|
+
this.onError.unshift(fn);
|
|
188
|
+
});
|
|
189
|
+
if (typeof response !== 'undefined')
|
|
190
|
+
return response;
|
|
161
191
|
}
|
|
162
192
|
});
|
|
163
193
|
}
|
|
@@ -195,6 +225,28 @@ class InterceptorHandler {
|
|
|
195
225
|
}
|
|
196
226
|
}
|
|
197
227
|
|
|
228
|
+
const mate = getMoostMate();
|
|
229
|
+
function getIterceptorHandlerFactory(interceptors, getTargetInstance, pipes, logger) {
|
|
230
|
+
return () => {
|
|
231
|
+
const interceptorHandlers = [];
|
|
232
|
+
for (const { handler } of interceptors) {
|
|
233
|
+
const interceptorMeta = mate.read(handler);
|
|
234
|
+
if (interceptorMeta === null || interceptorMeta === void 0 ? void 0 : interceptorMeta.injectable) {
|
|
235
|
+
interceptorHandlers.push((...args) => __awaiter(this, void 0, void 0, function* () {
|
|
236
|
+
const { restoreCtx } = eventCore.useEventContext();
|
|
237
|
+
const targetInstance = yield getTargetInstance();
|
|
238
|
+
restoreCtx();
|
|
239
|
+
return (yield getCallableFn(targetInstance, handler, restoreCtx, pipes, logger))(...args);
|
|
240
|
+
}));
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
interceptorHandlers.push(handler);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return Promise.resolve(new InterceptorHandler(interceptorHandlers));
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
198
250
|
function bindControllerMethods(options) {
|
|
199
251
|
var _a;
|
|
200
252
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -209,7 +261,9 @@ function bindControllerMethods(options) {
|
|
|
209
261
|
const methods = getInstanceOwnMethods(fakeInstance);
|
|
210
262
|
const mate = getMoostMate();
|
|
211
263
|
const meta = mate.read(classConstructor) || {};
|
|
212
|
-
const ownPrefix = typeof opts.replaceOwnPrefix === 'string'
|
|
264
|
+
const ownPrefix = typeof opts.replaceOwnPrefix === 'string'
|
|
265
|
+
? opts.replaceOwnPrefix
|
|
266
|
+
: ((_a = meta.controller) === null || _a === void 0 ? void 0 : _a.prefix) || '';
|
|
213
267
|
const prefix = `${opts.globalPrefix}/${ownPrefix}`;
|
|
214
268
|
for (const method of methods) {
|
|
215
269
|
const methodMeta = getMoostMate().read(fakeInstance, method) || {};
|
|
@@ -217,25 +271,12 @@ function bindControllerMethods(options) {
|
|
|
217
271
|
continue;
|
|
218
272
|
const pipes = [...(opts.pipes || []), ...(methodMeta.pipes || [])].sort((a, b) => a.priority - b.priority);
|
|
219
273
|
// preparing interceptors
|
|
220
|
-
const interceptors = [
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
interceptorHandlers.push((...args) => __awaiter(this, void 0, void 0, function* () {
|
|
227
|
-
const { restoreCtx } = eventCore.useEventContext();
|
|
228
|
-
const targetInstance = yield getInstance();
|
|
229
|
-
restoreCtx();
|
|
230
|
-
return (yield getCallableFn(targetInstance, handler, restoreCtx, pipes, options.logger))(...args);
|
|
231
|
-
}));
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
interceptorHandlers.push(handler);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
return Promise.resolve(new InterceptorHandler(interceptorHandlers));
|
|
238
|
-
};
|
|
274
|
+
const interceptors = [
|
|
275
|
+
...(opts.interceptors || []),
|
|
276
|
+
...(meta.interceptors || []),
|
|
277
|
+
...(methodMeta.interceptors || []),
|
|
278
|
+
].sort((a, b) => a.priority - b.priority);
|
|
279
|
+
const getIterceptorHandler = getIterceptorHandlerFactory(interceptors, getInstance, pipes, options.logger);
|
|
239
280
|
// preparing pipes
|
|
240
281
|
const argsPipes = [];
|
|
241
282
|
for (const p of methodMeta.params || []) {
|
|
@@ -264,11 +305,6 @@ function bindControllerMethods(options) {
|
|
|
264
305
|
prefix,
|
|
265
306
|
fakeInstance,
|
|
266
307
|
getInstance,
|
|
267
|
-
registerEventScope: (scopeId) => {
|
|
268
|
-
const infact = getMoostInfact();
|
|
269
|
-
infact.registerScope(scopeId);
|
|
270
|
-
return () => infact.unregisterScope(scopeId);
|
|
271
|
-
},
|
|
272
308
|
method,
|
|
273
309
|
handlers: methodMeta.handlers,
|
|
274
310
|
getIterceptorHandler,
|
|
@@ -280,21 +316,71 @@ function bindControllerMethods(options) {
|
|
|
280
316
|
});
|
|
281
317
|
}
|
|
282
318
|
|
|
319
|
+
/**
|
|
320
|
+
* ## Label
|
|
321
|
+
* ### @Decorator
|
|
322
|
+
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
323
|
+
*
|
|
324
|
+
* Stores Label metadata
|
|
325
|
+
*/
|
|
283
326
|
function Label(value) {
|
|
284
327
|
return getMoostMate().decorate('label', value);
|
|
285
328
|
}
|
|
329
|
+
/**
|
|
330
|
+
* ## Description
|
|
331
|
+
* ### @Decorator
|
|
332
|
+
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
333
|
+
*
|
|
334
|
+
* Stores Description metadata
|
|
335
|
+
*/
|
|
336
|
+
function Description(value) {
|
|
337
|
+
return getMoostMate().decorate('description', value);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* ## Value
|
|
341
|
+
* ### @Decorator
|
|
342
|
+
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
343
|
+
*
|
|
344
|
+
* Stores Value metadata
|
|
345
|
+
*/
|
|
346
|
+
function Value(value) {
|
|
347
|
+
return getMoostMate().decorate('value', value);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* ## Id
|
|
351
|
+
* ### @Decorator
|
|
352
|
+
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
353
|
+
*
|
|
354
|
+
* Stores Id metadata
|
|
355
|
+
*/
|
|
286
356
|
function Id(value) {
|
|
287
357
|
return getMoostMate().decorate('id', value);
|
|
288
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* ## Optional
|
|
361
|
+
* ### @Decorator
|
|
362
|
+
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
363
|
+
*
|
|
364
|
+
* Stores Optional metadata
|
|
365
|
+
*/
|
|
289
366
|
function Optional() {
|
|
290
367
|
return getMoostMate().decorate('optional', true);
|
|
291
368
|
}
|
|
369
|
+
/**
|
|
370
|
+
* ## Required
|
|
371
|
+
* ### @Decorator
|
|
372
|
+
* _Common purpose decorator that may be used by various adapters for various purposes_
|
|
373
|
+
*
|
|
374
|
+
* Stores Required metadata
|
|
375
|
+
*/
|
|
292
376
|
function Required() {
|
|
293
377
|
const mate = getMoostMate();
|
|
294
378
|
return mate.apply(mate.decorate('required', true),
|
|
295
379
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
296
380
|
mate.decorateClass((meta, level, key, index) => {
|
|
297
|
-
if (typeof index !== 'number' &&
|
|
381
|
+
if (typeof index !== 'number' &&
|
|
382
|
+
meta &&
|
|
383
|
+
['string', 'symbol'].includes(typeof key)) {
|
|
298
384
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
299
385
|
meta.requiredProps = meta.requiredProps || [];
|
|
300
386
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
|
@@ -314,7 +400,7 @@ function Required() {
|
|
|
314
400
|
function Resolve(resolver, label) {
|
|
315
401
|
return (target, key, index) => {
|
|
316
402
|
const i = typeof index === 'number' ? index : undefined;
|
|
317
|
-
fillLabel(target, key, i, label);
|
|
403
|
+
fillLabel(target, key || '', i, label);
|
|
318
404
|
getMoostMate().decorate('resolver', resolver)(target, key, i);
|
|
319
405
|
};
|
|
320
406
|
}
|
|
@@ -325,7 +411,7 @@ function Resolve(resolver, label) {
|
|
|
325
411
|
* @paramType string
|
|
326
412
|
*/
|
|
327
413
|
function Param(name) {
|
|
328
|
-
return Resolve(() => eventCore.useRouteParams().get(name), name);
|
|
414
|
+
return getMoostMate().apply(getMoostMate().decorate('isRouteParam', name), Resolve(() => eventCore.useRouteParams().get(name), name));
|
|
329
415
|
}
|
|
330
416
|
/**
|
|
331
417
|
* Get Parsed Params from url parh
|
|
@@ -367,7 +453,9 @@ function fillLabel(target, key, index, name) {
|
|
|
367
453
|
if (name) {
|
|
368
454
|
const meta = getMoostMate().read(target, key);
|
|
369
455
|
if (typeof index === 'number') {
|
|
370
|
-
if (!(meta === null || meta === void 0 ? void 0 : meta.params) ||
|
|
456
|
+
if (!(meta === null || meta === void 0 ? void 0 : meta.params) ||
|
|
457
|
+
!(meta === null || meta === void 0 ? void 0 : meta.params[index]) ||
|
|
458
|
+
!(meta === null || meta === void 0 ? void 0 : meta.params[index].label)) {
|
|
371
459
|
Label(name)(target, key, index);
|
|
372
460
|
}
|
|
373
461
|
}
|
|
@@ -380,8 +468,9 @@ function fillLabel(target, key, index, name) {
|
|
|
380
468
|
}
|
|
381
469
|
|
|
382
470
|
/**
|
|
471
|
+
* ## Injectable
|
|
472
|
+
* ### @Decorator
|
|
383
473
|
* Mark the Class as Injectable to enable it to be used in dependency injection
|
|
384
|
-
* @decorator
|
|
385
474
|
* @param scope - Scope for injection ("FOR_EVENT" | "SINGLETON" | true)
|
|
386
475
|
* FOR_EVENT - will create a new instance for each incoming request
|
|
387
476
|
* SINGLETON | true - will create a new instance only once
|
|
@@ -397,8 +486,9 @@ const insureInjectable = getMoostMate().decorate((meta) => {
|
|
|
397
486
|
});
|
|
398
487
|
|
|
399
488
|
/**
|
|
489
|
+
* ## Controller
|
|
490
|
+
* ### @Decorator
|
|
400
491
|
* Set Class as a Controller
|
|
401
|
-
* @decorator
|
|
402
492
|
* @param prefix - define the prefix for all the paths of this controller
|
|
403
493
|
*/
|
|
404
494
|
function Controller(prefix) {
|
|
@@ -408,8 +498,12 @@ function Controller(prefix) {
|
|
|
408
498
|
function ImportController(prefix, controller, provide) {
|
|
409
499
|
return getMoostMate().decorate('importController', {
|
|
410
500
|
prefix: typeof prefix === 'string' ? prefix : undefined,
|
|
411
|
-
typeResolver: typeof prefix === 'string'
|
|
412
|
-
|
|
501
|
+
typeResolver: typeof prefix === 'string'
|
|
502
|
+
? controller
|
|
503
|
+
: prefix,
|
|
504
|
+
provide: typeof prefix === 'string'
|
|
505
|
+
? provide || undefined
|
|
506
|
+
: controller || undefined,
|
|
413
507
|
}, true);
|
|
414
508
|
}
|
|
415
509
|
|
|
@@ -427,26 +521,40 @@ exports.TInterceptorPriority = void 0;
|
|
|
427
521
|
TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
|
|
428
522
|
TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
|
|
429
523
|
})(exports.TInterceptorPriority || (exports.TInterceptorPriority = {}));
|
|
524
|
+
/**
|
|
525
|
+
* ## Intercept
|
|
526
|
+
* ### @Decorator
|
|
527
|
+
* Set interceptor
|
|
528
|
+
* @param handler interceptor fn (use defineInterceptorFn)
|
|
529
|
+
* @param priority interceptor priority
|
|
530
|
+
* @returns
|
|
531
|
+
*/
|
|
430
532
|
function Intercept(handler, priority) {
|
|
431
533
|
return getMoostMate().decorate('interceptors', {
|
|
432
534
|
handler,
|
|
433
|
-
priority: priority ||
|
|
535
|
+
priority: priority ||
|
|
536
|
+
handler.priority ||
|
|
537
|
+
exports.TInterceptorPriority.INTERCEPTOR,
|
|
434
538
|
}, true);
|
|
435
539
|
}
|
|
436
540
|
|
|
437
541
|
/**
|
|
542
|
+
* ## Provide
|
|
543
|
+
* ### @Decorator
|
|
438
544
|
* Defines provide registry for class (and all the children)
|
|
439
545
|
* @param type - string or class constructor
|
|
440
546
|
* @param fn - factory function for provided value
|
|
441
547
|
*/
|
|
442
548
|
function Provide(type, fn) {
|
|
443
|
-
return getMoostMate().decorate(meta => {
|
|
549
|
+
return getMoostMate().decorate((meta) => {
|
|
444
550
|
meta.provide = meta.provide || {};
|
|
445
|
-
Object.assign(meta.provide, infact.createProvideRegistry([type, fn]));
|
|
551
|
+
Object.assign(meta.provide, infact$1.createProvideRegistry([type, fn]));
|
|
446
552
|
return meta;
|
|
447
553
|
});
|
|
448
554
|
}
|
|
449
555
|
/**
|
|
556
|
+
* ## Inject
|
|
557
|
+
* ### @Decorator
|
|
450
558
|
* Defines a key from provide registry to inject value
|
|
451
559
|
* (For optional values use with @Nullable())
|
|
452
560
|
* @param type - string or class constructor
|
|
@@ -455,6 +563,8 @@ function Inject(type) {
|
|
|
455
563
|
return getMoostMate().decorate('inject', type);
|
|
456
564
|
}
|
|
457
565
|
/**
|
|
566
|
+
* ## Nullable
|
|
567
|
+
* ### @Decorator
|
|
458
568
|
* Makes injectable value optional
|
|
459
569
|
* @param value default true
|
|
460
570
|
*/
|
|
@@ -496,6 +606,12 @@ function IsBoolean(...args) {
|
|
|
496
606
|
return Validate(valido$1.validoIsBoolean(...args));
|
|
497
607
|
}
|
|
498
608
|
|
|
609
|
+
/**
|
|
610
|
+
* ## Inherit
|
|
611
|
+
* ### @Decorator
|
|
612
|
+
* Inherit metadata from super class
|
|
613
|
+
* @returns
|
|
614
|
+
*/
|
|
499
615
|
const Inherit = () => getMoostMate().decorate('inherit', true);
|
|
500
616
|
|
|
501
617
|
exports.TPipePriority = void 0;
|
|
@@ -511,7 +627,202 @@ exports.TPipePriority = void 0;
|
|
|
511
627
|
TPipePriority[TPipePriority["AFTER_VALIDATE"] = 8] = "AFTER_VALIDATE";
|
|
512
628
|
})(exports.TPipePriority || (exports.TPipePriority = {}));
|
|
513
629
|
|
|
514
|
-
const
|
|
630
|
+
const genericTypesCastPipe = (strict) => {
|
|
631
|
+
const handler = (value, { paramMeta: meta }) => {
|
|
632
|
+
if (meta === null || meta === void 0 ? void 0 : meta.type) {
|
|
633
|
+
if ((value === undefined ||
|
|
634
|
+
value === null ||
|
|
635
|
+
(meta.type !== String && value === '')) &&
|
|
636
|
+
meta.optional) {
|
|
637
|
+
return undefined;
|
|
638
|
+
}
|
|
639
|
+
switch (meta.type) {
|
|
640
|
+
case Date: {
|
|
641
|
+
let d;
|
|
642
|
+
if (typeof value === 'string') {
|
|
643
|
+
d = new Date(/^\d+$/.test(value) ? Number(value) : value);
|
|
644
|
+
}
|
|
645
|
+
else {
|
|
646
|
+
d = new Date(value);
|
|
647
|
+
}
|
|
648
|
+
if (strict && Number.isNaN(d.getTime())) {
|
|
649
|
+
typeError(value, 'Date', meta.label);
|
|
650
|
+
}
|
|
651
|
+
return Number.isNaN(d.getTime()) ? value : d;
|
|
652
|
+
}
|
|
653
|
+
case Boolean:
|
|
654
|
+
if ([
|
|
655
|
+
true,
|
|
656
|
+
'true',
|
|
657
|
+
'TRUE',
|
|
658
|
+
'True',
|
|
659
|
+
1,
|
|
660
|
+
'1',
|
|
661
|
+
'X',
|
|
662
|
+
'x',
|
|
663
|
+
].includes(value)) {
|
|
664
|
+
return true;
|
|
665
|
+
}
|
|
666
|
+
if ([
|
|
667
|
+
false,
|
|
668
|
+
'false',
|
|
669
|
+
'FALSE',
|
|
670
|
+
'False',
|
|
671
|
+
0,
|
|
672
|
+
'0',
|
|
673
|
+
'',
|
|
674
|
+
' ',
|
|
675
|
+
null,
|
|
676
|
+
undefined,
|
|
677
|
+
].includes(value)) {
|
|
678
|
+
return false;
|
|
679
|
+
}
|
|
680
|
+
if (strict) {
|
|
681
|
+
typeError(value, 'boolean', meta.label);
|
|
682
|
+
}
|
|
683
|
+
return value;
|
|
684
|
+
case Number: {
|
|
685
|
+
if (strict && !value && value !== 0) {
|
|
686
|
+
typeError(value, 'numeric', meta.label);
|
|
687
|
+
}
|
|
688
|
+
const n = typeof value === 'string' && value.length > 0
|
|
689
|
+
? Number(value)
|
|
690
|
+
: NaN;
|
|
691
|
+
if (strict && Number.isNaN(n)) {
|
|
692
|
+
typeError(value, 'numeric', meta.label);
|
|
693
|
+
}
|
|
694
|
+
return Number.isNaN(n) ? value : n;
|
|
695
|
+
}
|
|
696
|
+
case String:
|
|
697
|
+
if (strict &&
|
|
698
|
+
['object', 'function'].includes(typeof value)) {
|
|
699
|
+
typeError(value, 'string', meta.label);
|
|
700
|
+
}
|
|
701
|
+
return (value && String(value)) || value;
|
|
702
|
+
default:
|
|
703
|
+
return value;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
handler.priority = exports.TPipePriority.AFTER_TRANSFORM;
|
|
708
|
+
return handler;
|
|
709
|
+
};
|
|
710
|
+
function typeError(value, targetType, label) {
|
|
711
|
+
const prefix = label ? `Argument "${label}" with value ` : '';
|
|
712
|
+
throw new Error(`${prefix}${JSON.stringify(value)} is not a ${targetType} type`);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
const valido = new valido$1.Valido({
|
|
716
|
+
getDtoMeta(value, _type) {
|
|
717
|
+
let type = _type;
|
|
718
|
+
if (!type) {
|
|
719
|
+
type = mate$1.getConstructor(value);
|
|
720
|
+
}
|
|
721
|
+
const mate = getMoostMate();
|
|
722
|
+
return mate.read(type);
|
|
723
|
+
},
|
|
724
|
+
getDtoParamMeta(value, type, key) {
|
|
725
|
+
const mate = getMoostMate();
|
|
726
|
+
return mate.read(type, key);
|
|
727
|
+
},
|
|
728
|
+
});
|
|
729
|
+
function getMoostValido() {
|
|
730
|
+
return valido;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
const DEFAULT_ERROR_LIMIT = 10;
|
|
734
|
+
function firstString(errors) {
|
|
735
|
+
const keys = Object.keys(errors);
|
|
736
|
+
for (const key of keys) {
|
|
737
|
+
if (typeof errors[key] === 'string')
|
|
738
|
+
return errors[key];
|
|
739
|
+
return firstString(errors[key]);
|
|
740
|
+
}
|
|
741
|
+
return '';
|
|
742
|
+
}
|
|
743
|
+
const validatePipe = (opts) => {
|
|
744
|
+
const pipe = (_value, metas, level) => __awaiter(void 0, void 0, void 0, function* () {
|
|
745
|
+
const { restoreCtx } = eventCore.useEventContext();
|
|
746
|
+
const valido = getMoostValido();
|
|
747
|
+
let meta = {};
|
|
748
|
+
if (level === 'PARAM') {
|
|
749
|
+
meta = metas.paramMeta || {};
|
|
750
|
+
}
|
|
751
|
+
else if (level === 'PROP') {
|
|
752
|
+
meta = metas.propMeta || {};
|
|
753
|
+
}
|
|
754
|
+
else if (level === 'METHOD') {
|
|
755
|
+
meta = metas.methodMeta || {};
|
|
756
|
+
}
|
|
757
|
+
else if (level === 'CLASS') {
|
|
758
|
+
meta = metas.classMeta || {};
|
|
759
|
+
}
|
|
760
|
+
const result = yield valido.validateParam(_value, meta, metas.key, undefined, metas.instance, undefined, 0, 0, (opts === null || opts === void 0 ? void 0 : opts.errorLimit) || DEFAULT_ERROR_LIMIT, restoreCtx);
|
|
761
|
+
if (result !== true) {
|
|
762
|
+
const message = typeof result === 'string' ? result : firstString(result);
|
|
763
|
+
if (opts === null || opts === void 0 ? void 0 : opts.errorCb) {
|
|
764
|
+
opts.errorCb(message, result);
|
|
765
|
+
}
|
|
766
|
+
else {
|
|
767
|
+
throw new Error('Validation error: ' + message);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return _value;
|
|
771
|
+
});
|
|
772
|
+
pipe.priority = exports.TPipePriority.VALIDATE;
|
|
773
|
+
return pipe;
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* ### Define Interceptor Function
|
|
778
|
+
*
|
|
779
|
+
* ```ts
|
|
780
|
+
* defineInterceptorFn((before, after, onError) => {
|
|
781
|
+
* //init
|
|
782
|
+
* before(() => {
|
|
783
|
+
* // before handler
|
|
784
|
+
* })
|
|
785
|
+
* after((response, reply) => {
|
|
786
|
+
* // after handler
|
|
787
|
+
* })
|
|
788
|
+
* onError((error, reply) => {
|
|
789
|
+
* // when error occured
|
|
790
|
+
* })
|
|
791
|
+
* },
|
|
792
|
+
* TInterceptorPriority.INTERCEPTOR,
|
|
793
|
+
* )
|
|
794
|
+
* ```
|
|
795
|
+
*
|
|
796
|
+
* @param fn interceptor function
|
|
797
|
+
* @param priority priority of the interceptor where BEFORE_ALL = 0, BEFORE_GUARD = 1, GUARD = 2, AFTER_GUARD = 3, INTERCEPTOR = 4, CATCH_ERROR = 5, AFTER_ALL = 6
|
|
798
|
+
* @returns
|
|
799
|
+
*/
|
|
800
|
+
function defineInterceptorFn(fn, priority = exports.TInterceptorPriority.INTERCEPTOR) {
|
|
801
|
+
fn.priority = priority;
|
|
802
|
+
return fn;
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* ### Define Pipe Function
|
|
806
|
+
*
|
|
807
|
+
* ```ts
|
|
808
|
+
* // example of a transform pipe
|
|
809
|
+
* const uppercaseTransformPipe = definePipeFn((value, metas, level) => {
|
|
810
|
+
* return typeof value === 'string' ? value.toUpperCase() : value
|
|
811
|
+
* },
|
|
812
|
+
* TPipePriority.TRANSFORM,
|
|
813
|
+
* )
|
|
814
|
+
* ```
|
|
815
|
+
*
|
|
816
|
+
* @param fn interceptor function
|
|
817
|
+
* @param priority priority of the pipe where BEFORE_RESOLVE = 0, RESOLVE = 1, AFTER_RESOLVE = 2, BEFORE_TRANSFORM = 3, TRANSFORM = 4, AFTER_TRANSFORM = 5, BEFORE_VALIDATE = 6, VALIDATE = 7, AFTER_VALIDATE = 8
|
|
818
|
+
* @returns
|
|
819
|
+
*/
|
|
820
|
+
function definePipeFn(fn, priority = exports.TPipePriority.TRANSFORM) {
|
|
821
|
+
fn.priority = priority;
|
|
822
|
+
return fn;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
const resolvePipe = definePipeFn((_value, metas, level) => {
|
|
515
826
|
var _a, _b, _c, _d;
|
|
516
827
|
let resolver;
|
|
517
828
|
if (level === 'PARAM') {
|
|
@@ -530,8 +841,7 @@ const resolvePipe = (_value, metas, level) => {
|
|
|
530
841
|
return resolver(metas, level);
|
|
531
842
|
}
|
|
532
843
|
return undefined;
|
|
533
|
-
};
|
|
534
|
-
resolvePipe.priority = exports.TPipePriority.RESOLVE;
|
|
844
|
+
}, exports.TPipePriority.RESOLVE);
|
|
535
845
|
|
|
536
846
|
const sharedPipes = [
|
|
537
847
|
{
|
|
@@ -540,24 +850,6 @@ const sharedPipes = [
|
|
|
540
850
|
},
|
|
541
851
|
];
|
|
542
852
|
|
|
543
|
-
const valido = new valido$1.Valido({
|
|
544
|
-
getDtoMeta(value, _type) {
|
|
545
|
-
let type = _type;
|
|
546
|
-
if (!type) {
|
|
547
|
-
type = mate.getConstructor(value);
|
|
548
|
-
}
|
|
549
|
-
const mate$1 = getMoostMate();
|
|
550
|
-
return mate$1.read(type);
|
|
551
|
-
},
|
|
552
|
-
getDtoParamMeta(value, type, key) {
|
|
553
|
-
const mate = getMoostMate();
|
|
554
|
-
return mate.read(type, key);
|
|
555
|
-
},
|
|
556
|
-
});
|
|
557
|
-
function getMoostValido() {
|
|
558
|
-
return valido;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
853
|
function getDefaultLogger(topic) {
|
|
562
854
|
return new logger.ProstoLogger({
|
|
563
855
|
level: 4,
|
|
@@ -569,19 +861,80 @@ function getDefaultLogger(topic) {
|
|
|
569
861
|
}, topic);
|
|
570
862
|
}
|
|
571
863
|
|
|
864
|
+
/**
|
|
865
|
+
* ## Moost
|
|
866
|
+
* Main moostjs class that serves as a shell for Moost Adapters
|
|
867
|
+
*
|
|
868
|
+
* ### Usage with HTTP Adapter
|
|
869
|
+
* ```ts
|
|
870
|
+
* │ // HTTP server example
|
|
871
|
+
* │ import { MoostHttp, Get } from '@moostjs/event-http'
|
|
872
|
+
* │ import { Moost, Param } from 'moost'
|
|
873
|
+
* │
|
|
874
|
+
* │ class MyServer extends Moost {
|
|
875
|
+
* │ @Get('test/:name')
|
|
876
|
+
* │ test(@Param('name') name: string) {
|
|
877
|
+
* │ return { message: `Hello ${name}!` }
|
|
878
|
+
* │ }
|
|
879
|
+
* │ }
|
|
880
|
+
* │
|
|
881
|
+
* │ const app = new MyServer()
|
|
882
|
+
* │ const http = new MoostHttp()
|
|
883
|
+
* │ app.adapter(http).listen(3000, () => {
|
|
884
|
+
* │ app.getLogger('MyApp').log('Up on port 3000')
|
|
885
|
+
* │ })
|
|
886
|
+
* │ app.init()
|
|
887
|
+
* ```
|
|
888
|
+
* ### Usage with CLI Adapter
|
|
889
|
+
* ```ts
|
|
890
|
+
* │ // CLI example
|
|
891
|
+
* │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
|
|
892
|
+
* │ import { Moost, Param } from 'moost'
|
|
893
|
+
* │
|
|
894
|
+
* │ class MyApp extends Moost {
|
|
895
|
+
* │ @Cli('command/:arg')
|
|
896
|
+
* │ command(
|
|
897
|
+
* │ @Param('arg')
|
|
898
|
+
* │ arg: string,
|
|
899
|
+
* │ @CliOption('test', 't')
|
|
900
|
+
* │ test: boolean,
|
|
901
|
+
* │ ) {
|
|
902
|
+
* │ return `command run with flag arg=${ arg }, test=${ test }`
|
|
903
|
+
* │ }
|
|
904
|
+
* │ }
|
|
905
|
+
* │
|
|
906
|
+
* │ const app = new MyApp()
|
|
907
|
+
* │ app.applyGlobalInterceptors(cliHelpInterceptor())
|
|
908
|
+
* │
|
|
909
|
+
* │ const cli = new MoostCli()
|
|
910
|
+
* │ app.adapter(cli)
|
|
911
|
+
* │ app.init()
|
|
912
|
+
* ```
|
|
913
|
+
*/
|
|
572
914
|
class Moost {
|
|
573
915
|
constructor(options) {
|
|
574
916
|
this.options = options;
|
|
575
917
|
this.pipes = [...sharedPipes];
|
|
576
918
|
this.interceptors = [];
|
|
577
919
|
this.adapters = [];
|
|
578
|
-
this.provide = infact.createProvideRegistry([infact.Infact, getMoostInfact], [mate.Mate, getMoostMate], [valido$1.Valido, getMoostValido]);
|
|
920
|
+
this.provide = infact$1.createProvideRegistry([infact$1.Infact, getMoostInfact], [mate$1.Mate, getMoostMate], [valido$1.Valido, getMoostValido]);
|
|
579
921
|
this.unregisteredControllers = [];
|
|
580
922
|
this.logger = (options === null || options === void 0 ? void 0 : options.logger) || getDefaultLogger('moost');
|
|
581
923
|
getMoostInfact().setLogger(this.getLogger('infact'));
|
|
582
|
-
const mate
|
|
583
|
-
Object.assign(mate
|
|
924
|
+
const mate = getMoostMate();
|
|
925
|
+
Object.assign(mate, { logger: this.getLogger('mate') });
|
|
584
926
|
}
|
|
927
|
+
/**
|
|
928
|
+
* ### getLogger
|
|
929
|
+
* Provides application logger
|
|
930
|
+
* ```js
|
|
931
|
+
* // get logger with topic = "App"
|
|
932
|
+
* const logger = app.getLogger('App')
|
|
933
|
+
* logger.log('...')
|
|
934
|
+
* ```
|
|
935
|
+
* @param topic
|
|
936
|
+
* @returns
|
|
937
|
+
*/
|
|
585
938
|
getLogger(topic) {
|
|
586
939
|
if (this.logger instanceof logger.ProstoLogger) {
|
|
587
940
|
return this.logger.createTopic(topic);
|
|
@@ -592,13 +945,20 @@ class Moost {
|
|
|
592
945
|
this.adapters.push(a);
|
|
593
946
|
return a;
|
|
594
947
|
}
|
|
948
|
+
/**
|
|
949
|
+
* ### init
|
|
950
|
+
* Ititializes adapter. Must be called after adapters are attached.
|
|
951
|
+
*/
|
|
595
952
|
init() {
|
|
596
953
|
return __awaiter(this, void 0, void 0, function* () {
|
|
597
|
-
this.setProvideRegistry(infact.createProvideRegistry([Moost, () => this]));
|
|
954
|
+
this.setProvideRegistry(infact$1.createProvideRegistry([Moost, () => this]));
|
|
598
955
|
for (const a of this.adapters) {
|
|
599
|
-
const constructor = mate.getConstructor(a);
|
|
956
|
+
const constructor = mate$1.getConstructor(a);
|
|
600
957
|
if (constructor) {
|
|
601
|
-
this.setProvideRegistry(infact.createProvideRegistry([
|
|
958
|
+
this.setProvideRegistry(infact$1.createProvideRegistry([
|
|
959
|
+
constructor,
|
|
960
|
+
() => a,
|
|
961
|
+
]));
|
|
602
962
|
}
|
|
603
963
|
if (typeof a.getProvideRegistry === 'function') {
|
|
604
964
|
this.setProvideRegistry(a.getProvideRegistry());
|
|
@@ -607,7 +967,7 @@ class Moost {
|
|
|
607
967
|
this.unregisteredControllers.unshift(this);
|
|
608
968
|
yield this.bindControllers();
|
|
609
969
|
for (const a of this.adapters) {
|
|
610
|
-
yield (a.onInit && a.onInit());
|
|
970
|
+
yield (a.onInit && a.onInit(this));
|
|
611
971
|
}
|
|
612
972
|
});
|
|
613
973
|
}
|
|
@@ -628,14 +988,16 @@ class Moost {
|
|
|
628
988
|
bindController(controller, provide, globalPrefix, replaceOwnPrefix) {
|
|
629
989
|
var _a;
|
|
630
990
|
return __awaiter(this, void 0, void 0, function* () {
|
|
631
|
-
const mate
|
|
632
|
-
const classMeta = mate
|
|
991
|
+
const mate = getMoostMate();
|
|
992
|
+
const classMeta = mate.read(controller);
|
|
633
993
|
const infact = getMoostInfact();
|
|
634
|
-
const isControllerConsructor = mate.isConstructor(controller);
|
|
994
|
+
const isControllerConsructor = mate$1.isConstructor(controller);
|
|
635
995
|
const pipes = [...this.pipes, ...((classMeta === null || classMeta === void 0 ? void 0 : classMeta.pipes) || [])].sort((a, b) => a.priority - b.priority);
|
|
636
996
|
let instance;
|
|
637
997
|
const infactOpts = { provide, customData: { pipes } };
|
|
638
|
-
if (isControllerConsructor &&
|
|
998
|
+
if (isControllerConsructor &&
|
|
999
|
+
((classMeta === null || classMeta === void 0 ? void 0 : classMeta.injectable) === 'SINGLETON' ||
|
|
1000
|
+
(classMeta === null || classMeta === void 0 ? void 0 : classMeta.injectable) === true)) {
|
|
639
1001
|
instance = (yield infact.get(controller, infactOpts));
|
|
640
1002
|
}
|
|
641
1003
|
else if (!isControllerConsructor) {
|
|
@@ -643,16 +1005,20 @@ class Moost {
|
|
|
643
1005
|
infact.setProvideRegByInstance(instance, provide);
|
|
644
1006
|
}
|
|
645
1007
|
// getInstance - instance factory for resolving SINGLETON and FOR_EVENT instance
|
|
646
|
-
const getInstance = instance
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
1008
|
+
const getInstance = instance
|
|
1009
|
+
? () => Promise.resolve(instance)
|
|
1010
|
+
: () => __awaiter(this, void 0, void 0, function* () {
|
|
1011
|
+
// if (!instance) {
|
|
1012
|
+
infact.silent(true);
|
|
1013
|
+
const { restoreCtx } = eventCore.useEventContext();
|
|
1014
|
+
const instance = (yield infact.get(controller, Object.assign(Object.assign({}, infactOpts), { syncContextFn: restoreCtx })));
|
|
1015
|
+
infact.silent(false);
|
|
1016
|
+
// }
|
|
1017
|
+
return instance;
|
|
1018
|
+
});
|
|
1019
|
+
const classConstructor = mate$1.isConstructor(controller)
|
|
1020
|
+
? controller
|
|
1021
|
+
: mate$1.getConstructor(controller);
|
|
656
1022
|
yield bindControllerMethods({
|
|
657
1023
|
getInstance,
|
|
658
1024
|
classConstructor,
|
|
@@ -665,15 +1031,22 @@ class Moost {
|
|
|
665
1031
|
logger: this.logger,
|
|
666
1032
|
});
|
|
667
1033
|
if (classMeta && classMeta.importController) {
|
|
668
|
-
const prefix = typeof replaceOwnPrefix === 'string'
|
|
1034
|
+
const prefix = typeof replaceOwnPrefix === 'string'
|
|
1035
|
+
? replaceOwnPrefix
|
|
1036
|
+
: (_a = classMeta === null || classMeta === void 0 ? void 0 : classMeta.controller) === null || _a === void 0 ? void 0 : _a.prefix;
|
|
669
1037
|
const mergedProvide = Object.assign(Object.assign({}, provide), ((classMeta === null || classMeta === void 0 ? void 0 : classMeta.provide) || {}));
|
|
670
1038
|
for (const ic of classMeta.importController) {
|
|
671
1039
|
if (ic.typeResolver) {
|
|
672
|
-
const isConstr = mate.isConstructor(ic.typeResolver);
|
|
1040
|
+
const isConstr = mate$1.isConstructor(ic.typeResolver);
|
|
673
1041
|
const isFunc = typeof ic.typeResolver === 'function';
|
|
674
1042
|
yield this.bindController(
|
|
675
1043
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
676
|
-
isConstr
|
|
1044
|
+
isConstr
|
|
1045
|
+
? ic.typeResolver
|
|
1046
|
+
: isFunc
|
|
1047
|
+
? yield ic.typeResolver()
|
|
1048
|
+
: ic.typeResolver, ic.provide
|
|
1049
|
+
? Object.assign(Object.assign({}, mergedProvide), ic.provide) : mergedProvide, `${globalPrefix}/${prefix || ''}`, ic.prefix);
|
|
677
1050
|
}
|
|
678
1051
|
}
|
|
679
1052
|
}
|
|
@@ -684,7 +1057,9 @@ class Moost {
|
|
|
684
1057
|
if (typeof item === 'function') {
|
|
685
1058
|
this.pipes.push({
|
|
686
1059
|
handler: item,
|
|
687
|
-
priority: typeof item.priority === 'number'
|
|
1060
|
+
priority: typeof item.priority === 'number'
|
|
1061
|
+
? item.priority
|
|
1062
|
+
: exports.TPipePriority.TRANSFORM,
|
|
688
1063
|
});
|
|
689
1064
|
}
|
|
690
1065
|
else {
|
|
@@ -694,14 +1069,35 @@ class Moost {
|
|
|
694
1069
|
});
|
|
695
1070
|
}
|
|
696
1071
|
}
|
|
1072
|
+
this.globalInterceptorHandler = undefined;
|
|
697
1073
|
return this;
|
|
698
1074
|
}
|
|
1075
|
+
/**
|
|
1076
|
+
* Provides InterceptorHandler with global interceptors and pipes.
|
|
1077
|
+
* Used to process interceptors when event handler was not found.
|
|
1078
|
+
*
|
|
1079
|
+
* @returns array of interceptors
|
|
1080
|
+
*/
|
|
1081
|
+
getGlobalInterceptorHandler() {
|
|
1082
|
+
if (!this.globalInterceptorHandler) {
|
|
1083
|
+
const mate = getMoostMate();
|
|
1084
|
+
const thisMeta = mate.read(this);
|
|
1085
|
+
const pipes = [...(this.pipes || []), ...((thisMeta === null || thisMeta === void 0 ? void 0 : thisMeta.pipes) || [])].sort((a, b) => a.priority - b.priority);
|
|
1086
|
+
const interceptors = [...this.interceptors, ...((thisMeta === null || thisMeta === void 0 ? void 0 : thisMeta.interceptors) || [])]
|
|
1087
|
+
.sort((a, b) => a.priority - b.priority);
|
|
1088
|
+
this.globalInterceptorHandler = getIterceptorHandlerFactory(interceptors, () => Promise.resolve(this), pipes, this.logger)();
|
|
1089
|
+
}
|
|
1090
|
+
return this.globalInterceptorHandler;
|
|
1091
|
+
}
|
|
699
1092
|
applyGlobalInterceptors(...items) {
|
|
700
1093
|
for (const item of items) {
|
|
701
1094
|
if (typeof item === 'function') {
|
|
702
1095
|
this.interceptors.push({
|
|
703
1096
|
handler: item,
|
|
704
|
-
priority: typeof item.priority === 'number'
|
|
1097
|
+
priority: typeof item.priority === 'number'
|
|
1098
|
+
? item
|
|
1099
|
+
.priority
|
|
1100
|
+
: exports.TInterceptorPriority.INTERCEPTOR,
|
|
705
1101
|
});
|
|
706
1102
|
}
|
|
707
1103
|
else {
|
|
@@ -711,6 +1107,7 @@ class Moost {
|
|
|
711
1107
|
});
|
|
712
1108
|
}
|
|
713
1109
|
}
|
|
1110
|
+
this.globalInterceptorHandler = undefined;
|
|
714
1111
|
return this;
|
|
715
1112
|
}
|
|
716
1113
|
/**
|
|
@@ -733,112 +1130,148 @@ class Moost {
|
|
|
733
1130
|
}
|
|
734
1131
|
}
|
|
735
1132
|
|
|
736
|
-
|
|
737
|
-
const
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
case Boolean:
|
|
757
|
-
if ([true, 'true', 'TRUE', 'True', 1, '1', 'X', 'x'].includes(value)) {
|
|
758
|
-
return true;
|
|
759
|
-
}
|
|
760
|
-
if ([false, 'false', 'FALSE', 'False', 0, '0', '', ' ', null, undefined].includes(value)) {
|
|
761
|
-
return false;
|
|
762
|
-
}
|
|
763
|
-
if (strict) {
|
|
764
|
-
typeError(value, 'boolean', meta.label);
|
|
765
|
-
}
|
|
766
|
-
return value;
|
|
767
|
-
case Number: {
|
|
768
|
-
if (strict && !value && value !== 0) {
|
|
769
|
-
typeError(value, 'numeric', meta.label);
|
|
770
|
-
}
|
|
771
|
-
const n = typeof value === 'string' && value.length > 0 ? Number(value) : NaN;
|
|
772
|
-
if (strict && Number.isNaN(n)) {
|
|
773
|
-
typeError(value, 'numeric', meta.label);
|
|
774
|
-
}
|
|
775
|
-
return Number.isNaN(n) ? value : n;
|
|
776
|
-
}
|
|
777
|
-
case String:
|
|
778
|
-
if (strict && ['object', 'function'].includes(typeof value)) {
|
|
779
|
-
typeError(value, 'string', meta.label);
|
|
780
|
-
}
|
|
781
|
-
return value && String(value) || value;
|
|
782
|
-
default:
|
|
783
|
-
return value;
|
|
784
|
-
}
|
|
785
|
-
}
|
|
1133
|
+
function setControllerContext(controller, method) {
|
|
1134
|
+
const { store } = eventCore.useEventContext();
|
|
1135
|
+
const { set } = store('controller');
|
|
1136
|
+
set('instance', controller);
|
|
1137
|
+
set('method', method);
|
|
1138
|
+
}
|
|
1139
|
+
function useControllerContext() {
|
|
1140
|
+
const { store } = eventCore.useEventContext();
|
|
1141
|
+
const { get } = store('controller');
|
|
1142
|
+
const getController = () => get('instance');
|
|
1143
|
+
const getMethod = () => get('method');
|
|
1144
|
+
// todo: add generic types to getControllerMeta
|
|
1145
|
+
const getControllerMeta = () => getMoostMate().read(getController());
|
|
1146
|
+
// todo: add generic types to getMethodMeta
|
|
1147
|
+
const getMethodMeta = () => getMoostMate().read(getController(), getMethod());
|
|
1148
|
+
return {
|
|
1149
|
+
getController,
|
|
1150
|
+
getMethod,
|
|
1151
|
+
getControllerMeta,
|
|
1152
|
+
getMethodMeta,
|
|
786
1153
|
};
|
|
787
|
-
handler.priority = exports.TPipePriority.AFTER_TRANSFORM;
|
|
788
|
-
return handler;
|
|
789
|
-
};
|
|
790
|
-
function typeError(value, targetType, label) {
|
|
791
|
-
const prefix = label ? `Argument "${label}" with value ` : '';
|
|
792
|
-
throw new Error(`${prefix}${JSON.stringify(value)} is not a ${targetType} type`);
|
|
793
1154
|
}
|
|
794
1155
|
|
|
795
|
-
const
|
|
796
|
-
function
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
if (typeof errors[key] === 'string')
|
|
800
|
-
return errors[key];
|
|
801
|
-
return firstString(errors[key]);
|
|
802
|
-
}
|
|
803
|
-
return '';
|
|
1156
|
+
const infact = getMoostInfact();
|
|
1157
|
+
function registerEventScope(scopeId) {
|
|
1158
|
+
infact.registerScope(scopeId);
|
|
1159
|
+
return () => infact.unregisterScope(scopeId);
|
|
804
1160
|
}
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
const
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
1161
|
+
function defineMoostEventHandler(options) {
|
|
1162
|
+
return () => __awaiter(this, void 0, void 0, function* () {
|
|
1163
|
+
var _a;
|
|
1164
|
+
const { restoreCtx } = eventCore.useEventContext(options.contextType);
|
|
1165
|
+
const scopeId = eventCore.useEventId().getId();
|
|
1166
|
+
const logger = eventCore.useEventLogger(options.loggerTitle);
|
|
1167
|
+
const unscope = registerEventScope(scopeId);
|
|
1168
|
+
let response;
|
|
1169
|
+
const hookOptions = {
|
|
1170
|
+
restoreCtx,
|
|
1171
|
+
scopeId,
|
|
1172
|
+
logger,
|
|
1173
|
+
unscope,
|
|
1174
|
+
method: options.controllerMethod,
|
|
1175
|
+
getResponse: () => response,
|
|
1176
|
+
reply: (r) => response = r,
|
|
1177
|
+
};
|
|
1178
|
+
if ((_a = options.hooks) === null || _a === void 0 ? void 0 : _a.init) {
|
|
1179
|
+
yield options.hooks.init(hookOptions);
|
|
1180
|
+
restoreCtx();
|
|
812
1181
|
}
|
|
813
|
-
|
|
814
|
-
|
|
1182
|
+
const instance = yield options.getControllerInstance();
|
|
1183
|
+
restoreCtx();
|
|
1184
|
+
if (instance) {
|
|
1185
|
+
setControllerContext(instance, options.controllerMethod || '');
|
|
815
1186
|
}
|
|
816
|
-
|
|
817
|
-
|
|
1187
|
+
const interceptorHandler = yield options.getIterceptorHandler();
|
|
1188
|
+
if (interceptorHandler) {
|
|
1189
|
+
restoreCtx();
|
|
1190
|
+
try {
|
|
1191
|
+
response = yield interceptorHandler.init();
|
|
1192
|
+
if (typeof response !== 'undefined')
|
|
1193
|
+
return endWithResponse();
|
|
1194
|
+
}
|
|
1195
|
+
catch (e) {
|
|
1196
|
+
options.logErrors && logger.error(e);
|
|
1197
|
+
response = e;
|
|
1198
|
+
return endWithResponse();
|
|
1199
|
+
}
|
|
818
1200
|
}
|
|
819
|
-
|
|
820
|
-
|
|
1201
|
+
let args = [];
|
|
1202
|
+
if (options.resolveArgs) {
|
|
1203
|
+
// params
|
|
1204
|
+
restoreCtx();
|
|
1205
|
+
try {
|
|
1206
|
+
// logger.trace(`resolving method args for "${ opts.method as string }"`)
|
|
1207
|
+
args = yield options.resolveArgs();
|
|
1208
|
+
// logger.trace(`args for method "${ opts.method as string }" resolved (count ${String(args.length)})`)
|
|
1209
|
+
}
|
|
1210
|
+
catch (e) {
|
|
1211
|
+
options.logErrors && logger.error(e);
|
|
1212
|
+
response = e;
|
|
1213
|
+
return endWithResponse();
|
|
1214
|
+
}
|
|
821
1215
|
}
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
if (
|
|
826
|
-
|
|
1216
|
+
if (interceptorHandler) {
|
|
1217
|
+
restoreCtx();
|
|
1218
|
+
response = yield interceptorHandler.fireBefore(response);
|
|
1219
|
+
if (typeof response !== 'undefined')
|
|
1220
|
+
return endWithResponse();
|
|
1221
|
+
}
|
|
1222
|
+
// fire request handler
|
|
1223
|
+
const callControllerMethod = () => {
|
|
1224
|
+
restoreCtx();
|
|
1225
|
+
if (options.callControllerMethod) {
|
|
1226
|
+
return options.callControllerMethod(args);
|
|
827
1227
|
}
|
|
828
|
-
else {
|
|
829
|
-
|
|
1228
|
+
else if (instance && options.controllerMethod && typeof instance[options.controllerMethod] === 'function') {
|
|
1229
|
+
return instance[options.controllerMethod](...args);
|
|
830
1230
|
}
|
|
1231
|
+
};
|
|
1232
|
+
try {
|
|
1233
|
+
response = callControllerMethod();
|
|
831
1234
|
}
|
|
832
|
-
|
|
1235
|
+
catch (e) {
|
|
1236
|
+
options.logErrors && logger.error(e);
|
|
1237
|
+
response = e;
|
|
1238
|
+
}
|
|
1239
|
+
function endWithResponse() {
|
|
1240
|
+
var _a;
|
|
1241
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1242
|
+
// fire after interceptors
|
|
1243
|
+
if (interceptorHandler) {
|
|
1244
|
+
restoreCtx();
|
|
1245
|
+
try {
|
|
1246
|
+
// logger.trace('firing after interceptors')
|
|
1247
|
+
response = yield interceptorHandler.fireAfter(response);
|
|
1248
|
+
}
|
|
1249
|
+
catch (e) {
|
|
1250
|
+
options.logErrors && logger.error(e);
|
|
1251
|
+
if (!options.manualUnscope) {
|
|
1252
|
+
unscope();
|
|
1253
|
+
}
|
|
1254
|
+
throw e;
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
if (!options.manualUnscope) {
|
|
1258
|
+
unscope();
|
|
1259
|
+
}
|
|
1260
|
+
if ((_a = options.hooks) === null || _a === void 0 ? void 0 : _a.end) {
|
|
1261
|
+
yield options.hooks.end(hookOptions);
|
|
1262
|
+
}
|
|
1263
|
+
return response;
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1266
|
+
return yield endWithResponse();
|
|
833
1267
|
});
|
|
834
|
-
|
|
835
|
-
return pipe;
|
|
836
|
-
};
|
|
1268
|
+
}
|
|
837
1269
|
|
|
838
1270
|
exports.Circular = Circular;
|
|
839
1271
|
exports.Const = Const;
|
|
840
1272
|
exports.ConstFactory = ConstFactory;
|
|
841
1273
|
exports.Controller = Controller;
|
|
1274
|
+
exports.Description = Description;
|
|
842
1275
|
exports.Dto = Dto;
|
|
843
1276
|
exports.Id = Id;
|
|
844
1277
|
exports.ImportController = ImportController;
|
|
@@ -863,9 +1296,16 @@ exports.Provide = Provide;
|
|
|
863
1296
|
exports.Required = Required;
|
|
864
1297
|
exports.Resolve = Resolve;
|
|
865
1298
|
exports.Validate = Validate;
|
|
1299
|
+
exports.Value = Value;
|
|
1300
|
+
exports.defineInterceptorFn = defineInterceptorFn;
|
|
1301
|
+
exports.defineMoostEventHandler = defineMoostEventHandler;
|
|
1302
|
+
exports.definePipeFn = definePipeFn;
|
|
866
1303
|
exports.genericTypesCastPipe = genericTypesCastPipe;
|
|
867
1304
|
exports.getMoostInfact = getMoostInfact;
|
|
868
1305
|
exports.getMoostMate = getMoostMate;
|
|
869
1306
|
exports.getNewMoostInfact = getNewMoostInfact;
|
|
1307
|
+
exports.registerEventScope = registerEventScope;
|
|
870
1308
|
exports.resolvePipe = resolvePipe;
|
|
1309
|
+
exports.setControllerContext = setControllerContext;
|
|
1310
|
+
exports.useControllerContext = useControllerContext;
|
|
871
1311
|
exports.validatePipe = validatePipe;
|