moost 0.2.18 → 0.2.20
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/dist/index.cjs +76 -32
- package/dist/index.d.ts +38 -3
- package/dist/index.mjs +75 -34
- package/package.json +6 -5
package/dist/index.cjs
CHANGED
|
@@ -4,6 +4,7 @@ var mate = require('@prostojs/mate');
|
|
|
4
4
|
var infact = require('@prostojs/infact');
|
|
5
5
|
var eventCore = require('@wooksjs/event-core');
|
|
6
6
|
var valido$1 = require('@prostojs/valido');
|
|
7
|
+
var logger = require('@prostojs/logger');
|
|
7
8
|
|
|
8
9
|
/******************************************************************************
|
|
9
10
|
Copyright (c) Microsoft Corporation.
|
|
@@ -114,33 +115,18 @@ function getParentProps(constructor) {
|
|
|
114
115
|
return [];
|
|
115
116
|
}
|
|
116
117
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
/* istanbul ignore file */
|
|
120
|
-
function log(text) {
|
|
121
|
-
console.log('[32m' + '[2m' + banner() + text + '[0m');
|
|
122
|
-
}
|
|
123
|
-
function logError(error) {
|
|
124
|
-
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function panic(error) {
|
|
128
|
-
logError(error);
|
|
129
|
-
return new Error(error);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function getCallableFn(targetInstance, fn, restoreCtx, pipes, silent) {
|
|
118
|
+
function getCallableFn(targetInstance, fn, restoreCtx, pipes, logger) {
|
|
133
119
|
return __awaiter(this, void 0, void 0, function* () {
|
|
134
120
|
const mate$1 = getMoostMate();
|
|
135
121
|
const meta = mate$1.read(fn);
|
|
136
122
|
if (meta === null || meta === void 0 ? void 0 : meta.injectable) {
|
|
137
123
|
const infact = getMoostInfact();
|
|
138
|
-
infact.silent(
|
|
124
|
+
infact.silent(true);
|
|
139
125
|
const instance = yield infact.getForInstance(targetInstance, fn, {
|
|
140
126
|
syncContextFn: () => { restoreCtx && restoreCtx(); },
|
|
141
127
|
customData: { pipes: [...(pipes || []), ...(meta.pipes || [])].sort((a, b) => a.priority - b.priority) },
|
|
142
128
|
});
|
|
143
|
-
infact.silent(
|
|
129
|
+
infact.silent(false);
|
|
144
130
|
return ((...args) => {
|
|
145
131
|
return instance.handler(...args);
|
|
146
132
|
});
|
|
@@ -148,7 +134,9 @@ function getCallableFn(targetInstance, fn, restoreCtx, pipes, silent) {
|
|
|
148
134
|
if (typeof fn === 'function') {
|
|
149
135
|
return fn;
|
|
150
136
|
}
|
|
151
|
-
|
|
137
|
+
const e = new Error(`getCallableFn failed for "${mate.getConstructor(targetInstance).name}" because the passed arg is not a Function nor TClassFunction`);
|
|
138
|
+
logger.error(e);
|
|
139
|
+
throw e;
|
|
152
140
|
});
|
|
153
141
|
}
|
|
154
142
|
|
|
@@ -239,7 +227,7 @@ function bindControllerMethods(options) {
|
|
|
239
227
|
const { restoreCtx } = eventCore.useEventContext();
|
|
240
228
|
const targetInstance = yield getInstance();
|
|
241
229
|
restoreCtx();
|
|
242
|
-
return (yield getCallableFn(targetInstance, handler, restoreCtx, pipes, options.
|
|
230
|
+
return (yield getCallableFn(targetInstance, handler, restoreCtx, pipes, options.logger))(...args);
|
|
243
231
|
}));
|
|
244
232
|
}
|
|
245
233
|
else {
|
|
@@ -275,7 +263,6 @@ function bindControllerMethods(options) {
|
|
|
275
263
|
yield adapter.bindHandler({
|
|
276
264
|
prefix,
|
|
277
265
|
fakeInstance,
|
|
278
|
-
silent: opts.silent,
|
|
279
266
|
getInstance,
|
|
280
267
|
registerEventScope: (scopeId) => {
|
|
281
268
|
const infact = getMoostInfact();
|
|
@@ -286,7 +273,7 @@ function bindControllerMethods(options) {
|
|
|
286
273
|
handlers: methodMeta.handlers,
|
|
287
274
|
getIterceptorHandler,
|
|
288
275
|
resolveArgs,
|
|
289
|
-
logHandler:
|
|
276
|
+
logHandler: (eventName) => options.logger.info(`• ${eventName} ${'[0m' + '[2m' + '[32m'}→ ${classConstructor.name}.${'[36m'}${method}${'[32m'}()`),
|
|
290
277
|
});
|
|
291
278
|
}
|
|
292
279
|
}
|
|
@@ -358,6 +345,24 @@ function Params() {
|
|
|
358
345
|
function Const(value, label) {
|
|
359
346
|
return Resolve(() => value, label);
|
|
360
347
|
}
|
|
348
|
+
/**
|
|
349
|
+
* Provide Const Value from Factory fn
|
|
350
|
+
* @decorator
|
|
351
|
+
* @param factory - value Factory fn
|
|
352
|
+
* @param label - label of the field
|
|
353
|
+
* @paramType unknown
|
|
354
|
+
*/
|
|
355
|
+
function ConstFactory(factory, label) {
|
|
356
|
+
return Resolve(() => __awaiter(this, void 0, void 0, function* () { return yield factory(); }), label);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Resolves event logger from event context
|
|
360
|
+
* @param topic
|
|
361
|
+
* @returns Resolver to '@wooksjs/event-core' (EventLogger)
|
|
362
|
+
*/
|
|
363
|
+
function EventLogger(topic) {
|
|
364
|
+
return Resolve(() => eventCore.useEventLogger(topic));
|
|
365
|
+
}
|
|
361
366
|
function fillLabel(target, key, index, name) {
|
|
362
367
|
if (name) {
|
|
363
368
|
const meta = getMoostMate().read(target, key);
|
|
@@ -429,6 +434,11 @@ function Intercept(handler, priority) {
|
|
|
429
434
|
}, true);
|
|
430
435
|
}
|
|
431
436
|
|
|
437
|
+
/**
|
|
438
|
+
* Defines provide registry for class (and all the children)
|
|
439
|
+
* @param type - string or class constructor
|
|
440
|
+
* @param fn - factory function for provided value
|
|
441
|
+
*/
|
|
432
442
|
function Provide(type, fn) {
|
|
433
443
|
return getMoostMate().decorate(meta => {
|
|
434
444
|
meta.provide = meta.provide || {};
|
|
@@ -436,8 +446,20 @@ function Provide(type, fn) {
|
|
|
436
446
|
return meta;
|
|
437
447
|
});
|
|
438
448
|
}
|
|
449
|
+
/**
|
|
450
|
+
* Defines a key from provide registry to inject value
|
|
451
|
+
* (For optional values use with @Nullable())
|
|
452
|
+
* @param type - string or class constructor
|
|
453
|
+
*/
|
|
439
454
|
function Inject(type) {
|
|
440
455
|
return getMoostMate().decorate('inject', type);
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Makes injectable value optional
|
|
459
|
+
* @param value default true
|
|
460
|
+
*/
|
|
461
|
+
function Nullable(v = true) {
|
|
462
|
+
return getMoostMate().decorate('nullable', v);
|
|
441
463
|
}
|
|
442
464
|
|
|
443
465
|
function Dto(dtoOptions = {}) {
|
|
@@ -536,6 +558,17 @@ function getMoostValido() {
|
|
|
536
558
|
return valido;
|
|
537
559
|
}
|
|
538
560
|
|
|
561
|
+
function getDefaultLogger(topic) {
|
|
562
|
+
return new logger.ProstoLogger({
|
|
563
|
+
level: 4,
|
|
564
|
+
transports: [
|
|
565
|
+
logger.createConsoleTransort({
|
|
566
|
+
format: logger.coloredConsole,
|
|
567
|
+
}),
|
|
568
|
+
],
|
|
569
|
+
}, topic);
|
|
570
|
+
}
|
|
571
|
+
|
|
539
572
|
class Moost {
|
|
540
573
|
constructor(options) {
|
|
541
574
|
this.options = options;
|
|
@@ -544,9 +577,16 @@ class Moost {
|
|
|
544
577
|
this.adapters = [];
|
|
545
578
|
this.provide = infact.createProvideRegistry([infact.Infact, getMoostInfact], [mate.Mate, getMoostMate], [valido$1.Valido, getMoostValido]);
|
|
546
579
|
this.unregisteredControllers = [];
|
|
547
|
-
|
|
548
|
-
|
|
580
|
+
this.logger = (options === null || options === void 0 ? void 0 : options.logger) || getDefaultLogger('moost');
|
|
581
|
+
getMoostInfact().setLogger(this.getLogger('infact'));
|
|
582
|
+
// const mate = getMoostMate()
|
|
583
|
+
// Object.assign(mate, { logger: this.getLogger('mate') })
|
|
584
|
+
}
|
|
585
|
+
getLogger(topic) {
|
|
586
|
+
if (this.logger instanceof logger.ProstoLogger) {
|
|
587
|
+
return this.logger.createTopic(topic);
|
|
549
588
|
}
|
|
589
|
+
return this.logger;
|
|
550
590
|
}
|
|
551
591
|
adapter(a) {
|
|
552
592
|
this.adapters.push(a);
|
|
@@ -574,6 +614,8 @@ class Moost {
|
|
|
574
614
|
bindControllers() {
|
|
575
615
|
var _a;
|
|
576
616
|
return __awaiter(this, void 0, void 0, function* () {
|
|
617
|
+
const infact = getMoostInfact();
|
|
618
|
+
infact.setLogger(this.logger);
|
|
577
619
|
const meta = getMoostMate();
|
|
578
620
|
const thisMeta = meta.read(this);
|
|
579
621
|
const provide = Object.assign(Object.assign({}, ((thisMeta === null || thisMeta === void 0 ? void 0 : thisMeta.provide) || {})), this.provide);
|
|
@@ -584,10 +626,10 @@ class Moost {
|
|
|
584
626
|
});
|
|
585
627
|
}
|
|
586
628
|
bindController(controller, provide, globalPrefix, replaceOwnPrefix) {
|
|
587
|
-
var _a
|
|
629
|
+
var _a;
|
|
588
630
|
return __awaiter(this, void 0, void 0, function* () {
|
|
589
|
-
const
|
|
590
|
-
const classMeta =
|
|
631
|
+
const mate$1 = getMoostMate();
|
|
632
|
+
const classMeta = mate$1.read(controller);
|
|
591
633
|
const infact = getMoostInfact();
|
|
592
634
|
const isControllerConsructor = mate.isConstructor(controller);
|
|
593
635
|
const pipes = [...this.pipes, ...((classMeta === null || classMeta === void 0 ? void 0 : classMeta.pipes) || [])].sort((a, b) => a.priority - b.priority);
|
|
@@ -602,12 +644,11 @@ class Moost {
|
|
|
602
644
|
}
|
|
603
645
|
// getInstance - instance factory for resolving SINGLETON and FOR_EVENT instance
|
|
604
646
|
const getInstance = instance ? () => Promise.resolve(instance) : () => __awaiter(this, void 0, void 0, function* () {
|
|
605
|
-
var _c, _d;
|
|
606
647
|
// if (!instance) {
|
|
607
|
-
infact.silent(
|
|
648
|
+
infact.silent(true);
|
|
608
649
|
const { restoreCtx } = eventCore.useEventContext();
|
|
609
650
|
const instance = yield infact.get(controller, Object.assign(Object.assign({}, infactOpts), { syncContextFn: restoreCtx }));
|
|
610
|
-
infact.silent(
|
|
651
|
+
infact.silent(false);
|
|
611
652
|
// }
|
|
612
653
|
return instance;
|
|
613
654
|
});
|
|
@@ -616,15 +657,15 @@ class Moost {
|
|
|
616
657
|
getInstance,
|
|
617
658
|
classConstructor,
|
|
618
659
|
adapters: this.adapters,
|
|
619
|
-
silent: !!((_a = this.options) === null || _a === void 0 ? void 0 : _a.silent),
|
|
620
660
|
globalPrefix,
|
|
621
661
|
replaceOwnPrefix,
|
|
622
662
|
interceptors: [...this.interceptors],
|
|
623
663
|
pipes,
|
|
624
664
|
provide: (classMeta === null || classMeta === void 0 ? void 0 : classMeta.provide) || {},
|
|
665
|
+
logger: this.logger,
|
|
625
666
|
});
|
|
626
667
|
if (classMeta && classMeta.importController) {
|
|
627
|
-
const prefix = typeof replaceOwnPrefix === 'string' ? replaceOwnPrefix : (
|
|
668
|
+
const prefix = typeof replaceOwnPrefix === 'string' ? replaceOwnPrefix : (_a = classMeta === null || classMeta === void 0 ? void 0 : classMeta.controller) === null || _a === void 0 ? void 0 : _a.prefix;
|
|
628
669
|
const mergedProvide = Object.assign(Object.assign({}, provide), ((classMeta === null || classMeta === void 0 ? void 0 : classMeta.provide) || {}));
|
|
629
670
|
for (const ic of classMeta.importController) {
|
|
630
671
|
if (ic.typeResolver) {
|
|
@@ -796,8 +837,10 @@ const validatePipe = (opts) => {
|
|
|
796
837
|
|
|
797
838
|
exports.Circular = Circular;
|
|
798
839
|
exports.Const = Const;
|
|
840
|
+
exports.ConstFactory = ConstFactory;
|
|
799
841
|
exports.Controller = Controller;
|
|
800
842
|
exports.Dto = Dto;
|
|
843
|
+
exports.EventLogger = EventLogger;
|
|
801
844
|
exports.Id = Id;
|
|
802
845
|
exports.ImportController = ImportController;
|
|
803
846
|
exports.Inherit = Inherit;
|
|
@@ -812,6 +855,7 @@ exports.IsString = IsString;
|
|
|
812
855
|
exports.IsTypeOf = IsTypeOf;
|
|
813
856
|
exports.Label = Label;
|
|
814
857
|
exports.Moost = Moost;
|
|
858
|
+
exports.Nullable = Nullable;
|
|
815
859
|
exports.Optional = Optional;
|
|
816
860
|
exports.Param = Param;
|
|
817
861
|
exports.Params = Params;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Infact } from '@prostojs/infact';
|
|
2
2
|
import { Mate } from '@prostojs/mate';
|
|
3
|
+
import { TConsoleBase } from '@prostojs/logger';
|
|
3
4
|
import { TProvideFn } from '@prostojs/infact';
|
|
4
5
|
import { TProvideRegistry } from '@prostojs/infact';
|
|
5
6
|
import { TValidoDtoOptions } from '@prostojs/valido';
|
|
@@ -17,7 +18,16 @@ export declare function Circular<T = unknown>(resolver: () => TClassConstructor<
|
|
|
17
18
|
* @param label - label of the field
|
|
18
19
|
* @paramType unknown
|
|
19
20
|
*/
|
|
20
|
-
export declare function Const(value:
|
|
21
|
+
export declare function Const<T>(value: T, label?: string): ParameterDecorator & PropertyDecorator;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Provide Const Value from Factory fn
|
|
25
|
+
* @decorator
|
|
26
|
+
* @param factory - value Factory fn
|
|
27
|
+
* @param label - label of the field
|
|
28
|
+
* @paramType unknown
|
|
29
|
+
*/
|
|
30
|
+
export declare function ConstFactory<T>(factory: () => T | Promise<T>, label?: string): ParameterDecorator & PropertyDecorator;
|
|
21
31
|
|
|
22
32
|
/**
|
|
23
33
|
* Set Class as a Controller
|
|
@@ -28,6 +38,13 @@ export declare function Controller(prefix?: string): ClassDecorator;
|
|
|
28
38
|
|
|
29
39
|
export declare function Dto(dtoOptions?: TValidoDtoOptions): ClassDecorator;
|
|
30
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Resolves event logger from event context
|
|
43
|
+
* @param topic
|
|
44
|
+
* @returns Resolver to '@wooksjs/event-core' (EventLogger)
|
|
45
|
+
*/
|
|
46
|
+
export declare function EventLogger(topic?: string): ParameterDecorator & PropertyDecorator;
|
|
47
|
+
|
|
31
48
|
export declare const genericTypesCastPipe: (strict?: boolean) => TPipeFn;
|
|
32
49
|
|
|
33
50
|
export declare function getMoostInfact(): Infact<TMoostMetadata, TMoostMetadata, TMoostParamsMetadata, TCustom>;
|
|
@@ -57,6 +74,11 @@ export declare function ImportController(prefix: string, controller: TFunction |
|
|
|
57
74
|
|
|
58
75
|
export declare const Inherit: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
59
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Defines a key from provide registry to inject value
|
|
79
|
+
* (For optional values use with @Nullable())
|
|
80
|
+
* @param type - string or class constructor
|
|
81
|
+
*/
|
|
60
82
|
export declare function Inject(type: string | TClassConstructor): ParameterDecorator;
|
|
61
83
|
|
|
62
84
|
/**
|
|
@@ -99,12 +121,14 @@ export declare function Label(value: string): MethodDecorator & ClassDecorator &
|
|
|
99
121
|
|
|
100
122
|
export declare class Moost {
|
|
101
123
|
protected options?: TMoostOptions | undefined;
|
|
124
|
+
protected logger: TConsoleBase;
|
|
102
125
|
protected pipes: TPipeData[];
|
|
103
126
|
protected interceptors: TInterceptorData[];
|
|
104
127
|
protected adapters: TMoostAdapter<TAny>[];
|
|
105
128
|
protected provide: TProvideRegistry;
|
|
106
129
|
protected unregisteredControllers: (TObject | TFunction)[];
|
|
107
130
|
constructor(options?: TMoostOptions | undefined);
|
|
131
|
+
getLogger(topic: string): TConsoleBase;
|
|
108
132
|
adapter<T extends object, A extends TMoostAdapter<T>>(a: A): A;
|
|
109
133
|
init(): Promise<void>;
|
|
110
134
|
protected bindControllers(): Promise<void>;
|
|
@@ -125,6 +149,12 @@ export declare class Moost {
|
|
|
125
149
|
registerControllers(...controllers: (TObject | TFunction)[]): this;
|
|
126
150
|
}
|
|
127
151
|
|
|
152
|
+
/**
|
|
153
|
+
* Makes injectable value optional
|
|
154
|
+
* @param value default true
|
|
155
|
+
*/
|
|
156
|
+
export declare function Nullable(v?: boolean): ParameterDecorator;
|
|
157
|
+
|
|
128
158
|
export declare function Optional(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
129
159
|
|
|
130
160
|
/**
|
|
@@ -142,6 +172,11 @@ export declare function Param(name: string): ParameterDecorator & PropertyDecora
|
|
|
142
172
|
*/
|
|
143
173
|
export declare function Params(): ParameterDecorator & PropertyDecorator;
|
|
144
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Defines provide registry for class (and all the children)
|
|
177
|
+
* @param type - string or class constructor
|
|
178
|
+
* @param fn - factory function for provided value
|
|
179
|
+
*/
|
|
145
180
|
export declare function Provide(type: string | TClassConstructor, fn: TProvideFn): ClassDecorator;
|
|
146
181
|
|
|
147
182
|
declare function Required_2(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
@@ -242,7 +277,6 @@ export declare interface TMoostAdapterOptions<H extends object, T extends object
|
|
|
242
277
|
prefix: string;
|
|
243
278
|
fakeInstance: T;
|
|
244
279
|
getInstance: () => Promise<T>;
|
|
245
|
-
silent: boolean;
|
|
246
280
|
method: keyof T;
|
|
247
281
|
handlers: TMoostHandler<H>[];
|
|
248
282
|
getIterceptorHandler: () => Promise<InterceptorHandler>;
|
|
@@ -275,12 +309,13 @@ export declare interface TMoostMetadata extends TCommonMetaFields, TCommonMoostM
|
|
|
275
309
|
|
|
276
310
|
export declare interface TMoostOptions {
|
|
277
311
|
globalPrefix?: string;
|
|
278
|
-
|
|
312
|
+
logger?: TConsoleBase;
|
|
279
313
|
}
|
|
280
314
|
|
|
281
315
|
export declare interface TMoostParamsMetadata extends TCommonMetaFields, TCommonMoostMeta {
|
|
282
316
|
circular?: () => TAny;
|
|
283
317
|
inject?: string | symbol | TClassConstructor;
|
|
318
|
+
nullable?: boolean;
|
|
284
319
|
}
|
|
285
320
|
|
|
286
321
|
declare type TObject = object;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Mate, getConstructor, isConstructor } from '@prostojs/mate';
|
|
2
2
|
import { Infact, createProvideRegistry } from '@prostojs/infact';
|
|
3
|
-
import { useEventId, useEventContext, useRouteParams } from '@wooksjs/event-core';
|
|
3
|
+
import { useEventId, useEventContext, useRouteParams, useEventLogger } from '@wooksjs/event-core';
|
|
4
4
|
import { validoIsTypeOf, validoIsString, validoIsNumber, validoIsBoolean, Valido } from '@prostojs/valido';
|
|
5
|
+
import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
|
|
5
6
|
|
|
6
7
|
/******************************************************************************
|
|
7
8
|
Copyright (c) Microsoft Corporation.
|
|
@@ -112,33 +113,18 @@ function getParentProps(constructor) {
|
|
|
112
113
|
return [];
|
|
113
114
|
}
|
|
114
115
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
/* istanbul ignore file */
|
|
118
|
-
function log(text) {
|
|
119
|
-
console.log('[32m' + '[2m' + banner() + text + '[0m');
|
|
120
|
-
}
|
|
121
|
-
function logError(error) {
|
|
122
|
-
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function panic(error) {
|
|
126
|
-
logError(error);
|
|
127
|
-
return new Error(error);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function getCallableFn(targetInstance, fn, restoreCtx, pipes, silent) {
|
|
116
|
+
function getCallableFn(targetInstance, fn, restoreCtx, pipes, logger) {
|
|
131
117
|
return __awaiter(this, void 0, void 0, function* () {
|
|
132
118
|
const mate = getMoostMate();
|
|
133
119
|
const meta = mate.read(fn);
|
|
134
120
|
if (meta === null || meta === void 0 ? void 0 : meta.injectable) {
|
|
135
121
|
const infact = getMoostInfact();
|
|
136
|
-
infact.silent(
|
|
122
|
+
infact.silent(true);
|
|
137
123
|
const instance = yield infact.getForInstance(targetInstance, fn, {
|
|
138
124
|
syncContextFn: () => { restoreCtx && restoreCtx(); },
|
|
139
125
|
customData: { pipes: [...(pipes || []), ...(meta.pipes || [])].sort((a, b) => a.priority - b.priority) },
|
|
140
126
|
});
|
|
141
|
-
infact.silent(
|
|
127
|
+
infact.silent(false);
|
|
142
128
|
return ((...args) => {
|
|
143
129
|
return instance.handler(...args);
|
|
144
130
|
});
|
|
@@ -146,7 +132,9 @@ function getCallableFn(targetInstance, fn, restoreCtx, pipes, silent) {
|
|
|
146
132
|
if (typeof fn === 'function') {
|
|
147
133
|
return fn;
|
|
148
134
|
}
|
|
149
|
-
|
|
135
|
+
const e = new Error(`getCallableFn failed for "${getConstructor(targetInstance).name}" because the passed arg is not a Function nor TClassFunction`);
|
|
136
|
+
logger.error(e);
|
|
137
|
+
throw e;
|
|
150
138
|
});
|
|
151
139
|
}
|
|
152
140
|
|
|
@@ -237,7 +225,7 @@ function bindControllerMethods(options) {
|
|
|
237
225
|
const { restoreCtx } = useEventContext();
|
|
238
226
|
const targetInstance = yield getInstance();
|
|
239
227
|
restoreCtx();
|
|
240
|
-
return (yield getCallableFn(targetInstance, handler, restoreCtx, pipes, options.
|
|
228
|
+
return (yield getCallableFn(targetInstance, handler, restoreCtx, pipes, options.logger))(...args);
|
|
241
229
|
}));
|
|
242
230
|
}
|
|
243
231
|
else {
|
|
@@ -273,7 +261,6 @@ function bindControllerMethods(options) {
|
|
|
273
261
|
yield adapter.bindHandler({
|
|
274
262
|
prefix,
|
|
275
263
|
fakeInstance,
|
|
276
|
-
silent: opts.silent,
|
|
277
264
|
getInstance,
|
|
278
265
|
registerEventScope: (scopeId) => {
|
|
279
266
|
const infact = getMoostInfact();
|
|
@@ -284,7 +271,7 @@ function bindControllerMethods(options) {
|
|
|
284
271
|
handlers: methodMeta.handlers,
|
|
285
272
|
getIterceptorHandler,
|
|
286
273
|
resolveArgs,
|
|
287
|
-
logHandler:
|
|
274
|
+
logHandler: (eventName) => options.logger.info(`• ${eventName} ${'[0m' + '[2m' + '[32m'}→ ${classConstructor.name}.${'[36m'}${method}${'[32m'}()`),
|
|
288
275
|
});
|
|
289
276
|
}
|
|
290
277
|
}
|
|
@@ -356,6 +343,24 @@ function Params() {
|
|
|
356
343
|
function Const(value, label) {
|
|
357
344
|
return Resolve(() => value, label);
|
|
358
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* Provide Const Value from Factory fn
|
|
348
|
+
* @decorator
|
|
349
|
+
* @param factory - value Factory fn
|
|
350
|
+
* @param label - label of the field
|
|
351
|
+
* @paramType unknown
|
|
352
|
+
*/
|
|
353
|
+
function ConstFactory(factory, label) {
|
|
354
|
+
return Resolve(() => __awaiter(this, void 0, void 0, function* () { return yield factory(); }), label);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Resolves event logger from event context
|
|
358
|
+
* @param topic
|
|
359
|
+
* @returns Resolver to '@wooksjs/event-core' (EventLogger)
|
|
360
|
+
*/
|
|
361
|
+
function EventLogger(topic) {
|
|
362
|
+
return Resolve(() => useEventLogger(topic));
|
|
363
|
+
}
|
|
359
364
|
function fillLabel(target, key, index, name) {
|
|
360
365
|
if (name) {
|
|
361
366
|
const meta = getMoostMate().read(target, key);
|
|
@@ -427,6 +432,11 @@ function Intercept(handler, priority) {
|
|
|
427
432
|
}, true);
|
|
428
433
|
}
|
|
429
434
|
|
|
435
|
+
/**
|
|
436
|
+
* Defines provide registry for class (and all the children)
|
|
437
|
+
* @param type - string or class constructor
|
|
438
|
+
* @param fn - factory function for provided value
|
|
439
|
+
*/
|
|
430
440
|
function Provide(type, fn) {
|
|
431
441
|
return getMoostMate().decorate(meta => {
|
|
432
442
|
meta.provide = meta.provide || {};
|
|
@@ -434,8 +444,20 @@ function Provide(type, fn) {
|
|
|
434
444
|
return meta;
|
|
435
445
|
});
|
|
436
446
|
}
|
|
447
|
+
/**
|
|
448
|
+
* Defines a key from provide registry to inject value
|
|
449
|
+
* (For optional values use with @Nullable())
|
|
450
|
+
* @param type - string or class constructor
|
|
451
|
+
*/
|
|
437
452
|
function Inject(type) {
|
|
438
453
|
return getMoostMate().decorate('inject', type);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Makes injectable value optional
|
|
457
|
+
* @param value default true
|
|
458
|
+
*/
|
|
459
|
+
function Nullable(v = true) {
|
|
460
|
+
return getMoostMate().decorate('nullable', v);
|
|
439
461
|
}
|
|
440
462
|
|
|
441
463
|
function Dto(dtoOptions = {}) {
|
|
@@ -534,6 +556,17 @@ function getMoostValido() {
|
|
|
534
556
|
return valido;
|
|
535
557
|
}
|
|
536
558
|
|
|
559
|
+
function getDefaultLogger(topic) {
|
|
560
|
+
return new ProstoLogger({
|
|
561
|
+
level: 4,
|
|
562
|
+
transports: [
|
|
563
|
+
createConsoleTransort({
|
|
564
|
+
format: coloredConsole,
|
|
565
|
+
}),
|
|
566
|
+
],
|
|
567
|
+
}, topic);
|
|
568
|
+
}
|
|
569
|
+
|
|
537
570
|
class Moost {
|
|
538
571
|
constructor(options) {
|
|
539
572
|
this.options = options;
|
|
@@ -542,9 +575,16 @@ class Moost {
|
|
|
542
575
|
this.adapters = [];
|
|
543
576
|
this.provide = createProvideRegistry([Infact, getMoostInfact], [Mate, getMoostMate], [Valido, getMoostValido]);
|
|
544
577
|
this.unregisteredControllers = [];
|
|
545
|
-
|
|
546
|
-
|
|
578
|
+
this.logger = (options === null || options === void 0 ? void 0 : options.logger) || getDefaultLogger('moost');
|
|
579
|
+
getMoostInfact().setLogger(this.getLogger('infact'));
|
|
580
|
+
// const mate = getMoostMate()
|
|
581
|
+
// Object.assign(mate, { logger: this.getLogger('mate') })
|
|
582
|
+
}
|
|
583
|
+
getLogger(topic) {
|
|
584
|
+
if (this.logger instanceof ProstoLogger) {
|
|
585
|
+
return this.logger.createTopic(topic);
|
|
547
586
|
}
|
|
587
|
+
return this.logger;
|
|
548
588
|
}
|
|
549
589
|
adapter(a) {
|
|
550
590
|
this.adapters.push(a);
|
|
@@ -572,6 +612,8 @@ class Moost {
|
|
|
572
612
|
bindControllers() {
|
|
573
613
|
var _a;
|
|
574
614
|
return __awaiter(this, void 0, void 0, function* () {
|
|
615
|
+
const infact = getMoostInfact();
|
|
616
|
+
infact.setLogger(this.logger);
|
|
575
617
|
const meta = getMoostMate();
|
|
576
618
|
const thisMeta = meta.read(this);
|
|
577
619
|
const provide = Object.assign(Object.assign({}, ((thisMeta === null || thisMeta === void 0 ? void 0 : thisMeta.provide) || {})), this.provide);
|
|
@@ -582,10 +624,10 @@ class Moost {
|
|
|
582
624
|
});
|
|
583
625
|
}
|
|
584
626
|
bindController(controller, provide, globalPrefix, replaceOwnPrefix) {
|
|
585
|
-
var _a
|
|
627
|
+
var _a;
|
|
586
628
|
return __awaiter(this, void 0, void 0, function* () {
|
|
587
|
-
const
|
|
588
|
-
const classMeta =
|
|
629
|
+
const mate = getMoostMate();
|
|
630
|
+
const classMeta = mate.read(controller);
|
|
589
631
|
const infact = getMoostInfact();
|
|
590
632
|
const isControllerConsructor = isConstructor(controller);
|
|
591
633
|
const pipes = [...this.pipes, ...((classMeta === null || classMeta === void 0 ? void 0 : classMeta.pipes) || [])].sort((a, b) => a.priority - b.priority);
|
|
@@ -600,12 +642,11 @@ class Moost {
|
|
|
600
642
|
}
|
|
601
643
|
// getInstance - instance factory for resolving SINGLETON and FOR_EVENT instance
|
|
602
644
|
const getInstance = instance ? () => Promise.resolve(instance) : () => __awaiter(this, void 0, void 0, function* () {
|
|
603
|
-
var _c, _d;
|
|
604
645
|
// if (!instance) {
|
|
605
|
-
infact.silent(
|
|
646
|
+
infact.silent(true);
|
|
606
647
|
const { restoreCtx } = useEventContext();
|
|
607
648
|
const instance = yield infact.get(controller, Object.assign(Object.assign({}, infactOpts), { syncContextFn: restoreCtx }));
|
|
608
|
-
infact.silent(
|
|
649
|
+
infact.silent(false);
|
|
609
650
|
// }
|
|
610
651
|
return instance;
|
|
611
652
|
});
|
|
@@ -614,15 +655,15 @@ class Moost {
|
|
|
614
655
|
getInstance,
|
|
615
656
|
classConstructor,
|
|
616
657
|
adapters: this.adapters,
|
|
617
|
-
silent: !!((_a = this.options) === null || _a === void 0 ? void 0 : _a.silent),
|
|
618
658
|
globalPrefix,
|
|
619
659
|
replaceOwnPrefix,
|
|
620
660
|
interceptors: [...this.interceptors],
|
|
621
661
|
pipes,
|
|
622
662
|
provide: (classMeta === null || classMeta === void 0 ? void 0 : classMeta.provide) || {},
|
|
663
|
+
logger: this.logger,
|
|
623
664
|
});
|
|
624
665
|
if (classMeta && classMeta.importController) {
|
|
625
|
-
const prefix = typeof replaceOwnPrefix === 'string' ? replaceOwnPrefix : (
|
|
666
|
+
const prefix = typeof replaceOwnPrefix === 'string' ? replaceOwnPrefix : (_a = classMeta === null || classMeta === void 0 ? void 0 : classMeta.controller) === null || _a === void 0 ? void 0 : _a.prefix;
|
|
626
667
|
const mergedProvide = Object.assign(Object.assign({}, provide), ((classMeta === null || classMeta === void 0 ? void 0 : classMeta.provide) || {}));
|
|
627
668
|
for (const ic of classMeta.importController) {
|
|
628
669
|
if (ic.typeResolver) {
|
|
@@ -792,4 +833,4 @@ const validatePipe = (opts) => {
|
|
|
792
833
|
return pipe;
|
|
793
834
|
};
|
|
794
835
|
|
|
795
|
-
export { Circular, Const, Controller, Dto, Id, ImportController, Inherit, Inject, Injectable, Intercept, InterceptorHandler, IsArray, IsBoolean, IsNumber, IsString, IsTypeOf, Label, Moost, Optional, Param, Params, Provide, Required, Resolve, TInterceptorPriority, TPipePriority, Validate, genericTypesCastPipe, getMoostInfact, getMoostMate, getNewMoostInfact, resolvePipe, validatePipe };
|
|
836
|
+
export { Circular, Const, ConstFactory, Controller, Dto, EventLogger, Id, ImportController, Inherit, Inject, Injectable, Intercept, InterceptorHandler, IsArray, IsBoolean, IsNumber, IsString, IsTypeOf, Label, Moost, Nullable, Optional, Param, Params, Provide, Required, Resolve, TInterceptorPriority, TPipePriority, Validate, genericTypesCastPipe, getMoostInfact, getMoostMate, getNewMoostInfact, resolvePipe, validatePipe };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "moost",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.20",
|
|
4
4
|
"description": "moost",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -22,11 +22,12 @@
|
|
|
22
22
|
"prostojs"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@prostojs/infact": "^0.1.
|
|
26
|
-
"@prostojs/mate": "^0.1.
|
|
25
|
+
"@prostojs/infact": "^0.1.11",
|
|
26
|
+
"@prostojs/mate": "^0.1.14",
|
|
27
|
+
"@prostojs/logger": "^0.3.2",
|
|
27
28
|
"@prostojs/valido": "^0.0.2",
|
|
28
|
-
"@wooksjs/event-core": "^0.2.
|
|
29
|
-
"wooks": "^0.2.
|
|
29
|
+
"@wooksjs/event-core": "^0.2.14",
|
|
30
|
+
"wooks": "^0.2.14"
|
|
30
31
|
},
|
|
31
32
|
"author": "Artem Maltsev",
|
|
32
33
|
"license": "MIT",
|