moost 0.5.0 → 0.5.2
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 +130 -32
- package/dist/index.d.ts +15 -12
- package/dist/index.mjs +125 -34
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -7,6 +7,26 @@ var logger = require('@prostojs/logger');
|
|
|
7
7
|
var hookable = require('hookable');
|
|
8
8
|
var wooks = require('wooks');
|
|
9
9
|
|
|
10
|
+
let defaultLogger;
|
|
11
|
+
function setDefaultLogger(logger) {
|
|
12
|
+
defaultLogger = logger;
|
|
13
|
+
}
|
|
14
|
+
function getDefaultLogger(topic) {
|
|
15
|
+
if (!defaultLogger) {
|
|
16
|
+
defaultLogger = new logger.ProstoLogger({
|
|
17
|
+
level: 4,
|
|
18
|
+
transports: [
|
|
19
|
+
logger.createConsoleTransort({
|
|
20
|
+
format: logger.coloredConsole,
|
|
21
|
+
}),
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return topic && defaultLogger instanceof logger.ProstoLogger
|
|
26
|
+
? defaultLogger.createTopic(topic)
|
|
27
|
+
: defaultLogger;
|
|
28
|
+
}
|
|
29
|
+
|
|
10
30
|
async function runPipes(pipes, initialValue, metas, level) {
|
|
11
31
|
let v = initialValue;
|
|
12
32
|
for (const pipe of pipes) {
|
|
@@ -35,6 +55,17 @@ function getMoostMate() {
|
|
|
35
55
|
}
|
|
36
56
|
|
|
37
57
|
const sharedMoostInfact = getNewMoostInfact();
|
|
58
|
+
let loggingOptions = {
|
|
59
|
+
newInstance: 'SINGLETON',
|
|
60
|
+
warn: true,
|
|
61
|
+
error: true,
|
|
62
|
+
};
|
|
63
|
+
function setInfactLoggingOptions(options) {
|
|
64
|
+
loggingOptions = {
|
|
65
|
+
...loggingOptions,
|
|
66
|
+
...options,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
38
69
|
function getMoostInfact() {
|
|
39
70
|
return sharedMoostInfact;
|
|
40
71
|
}
|
|
@@ -80,6 +111,82 @@ function getNewMoostInfact() {
|
|
|
80
111
|
}
|
|
81
112
|
},
|
|
82
113
|
storeProvideRegByInstance: true,
|
|
114
|
+
on: (event, targetClass, message, args) => {
|
|
115
|
+
switch (event) {
|
|
116
|
+
case 'new-instance': {
|
|
117
|
+
const isForEvent = getMoostMate().read(targetClass)?.injectable !== 'SINGLETON';
|
|
118
|
+
const isSingleton = !isForEvent;
|
|
119
|
+
if (loggingOptions.newInstance === false ||
|
|
120
|
+
(loggingOptions.newInstance === 'FOR_EVENT' && !isForEvent) ||
|
|
121
|
+
(loggingOptions.newInstance === 'SINGLETON' && isSingleton)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case 'warn': {
|
|
127
|
+
if (!loggingOptions.warn) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case 'error': {
|
|
133
|
+
if (!loggingOptions.error) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
let logger;
|
|
140
|
+
try {
|
|
141
|
+
logger = eventCore.useEventLogger('infact');
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
logger = getDefaultLogger('infact');
|
|
145
|
+
}
|
|
146
|
+
const instance = `${'[4m'}${targetClass.name}${'[24m'}`;
|
|
147
|
+
switch (event) {
|
|
148
|
+
case 'new-instance': {
|
|
149
|
+
const params = args
|
|
150
|
+
?.map(a => {
|
|
151
|
+
switch (typeof a) {
|
|
152
|
+
case 'number':
|
|
153
|
+
case 'boolean': {
|
|
154
|
+
return `${'[33m'}${a}${'[2m' + '[34m'}`;
|
|
155
|
+
}
|
|
156
|
+
case 'string': {
|
|
157
|
+
return `${'[92m'}"${a.slice(0, 1)}..."${'[2m' + '[34m'}`;
|
|
158
|
+
}
|
|
159
|
+
case 'object': {
|
|
160
|
+
if (Array.isArray(a)) {
|
|
161
|
+
return `[${a.length}]`;
|
|
162
|
+
}
|
|
163
|
+
if (mate$1.getConstructor(a)) {
|
|
164
|
+
return mate$1.getConstructor(a).name;
|
|
165
|
+
}
|
|
166
|
+
return '{}';
|
|
167
|
+
}
|
|
168
|
+
default: {
|
|
169
|
+
return '*';
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
.map(a => `${'[2m' + '[1m'}${a}${'[22m' + '[2m'}`)
|
|
174
|
+
.join(', ') || '';
|
|
175
|
+
logger.info(`new ${instance}${'[2m' + '[34m'}(${params})`);
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
case 'warn': {
|
|
179
|
+
const hier = `${'[2m' + '[34m'}⋱ ${args?.map(String).join(' → ') || ''}`;
|
|
180
|
+
logger.warn(`${instance} - ${message} ${hier}`);
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
case 'error': {
|
|
184
|
+
const hier = `${'[2m' + '[34m'}⋱ ${args?.map(String).join(' → ') || ''}`;
|
|
185
|
+
logger.error(`Failed to instantiate ${instance}. ${message} ${hier}`);
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
},
|
|
83
190
|
});
|
|
84
191
|
}
|
|
85
192
|
|
|
@@ -253,29 +360,16 @@ function getParentProps(constructor) {
|
|
|
253
360
|
return [];
|
|
254
361
|
}
|
|
255
362
|
|
|
256
|
-
function getDefaultLogger(topic) {
|
|
257
|
-
return new logger.ProstoLogger({
|
|
258
|
-
level: 4,
|
|
259
|
-
transports: [
|
|
260
|
-
logger.createConsoleTransort({
|
|
261
|
-
format: logger.coloredConsole,
|
|
262
|
-
}),
|
|
263
|
-
],
|
|
264
|
-
}, topic);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
363
|
async function getCallableFn(targetInstance, fn, pipes, logger) {
|
|
268
364
|
const mate = getMoostMate();
|
|
269
365
|
const meta = mate.read(fn);
|
|
270
366
|
if (meta?.injectable) {
|
|
271
367
|
const infact = getMoostInfact();
|
|
272
|
-
infact.silent(true);
|
|
273
368
|
const instance = (await infact.getForInstance(targetInstance, fn, {
|
|
274
369
|
customData: {
|
|
275
370
|
pipes: [...(pipes || []), ...(meta.pipes || [])].sort((a, b) => a.priority - b.priority),
|
|
276
371
|
},
|
|
277
372
|
}));
|
|
278
|
-
infact.silent(false);
|
|
279
373
|
return ((...args) => instance.handler(...args));
|
|
280
374
|
}
|
|
281
375
|
if (typeof fn === 'function') {
|
|
@@ -462,7 +556,7 @@ async function bindControllerMethods(options) {
|
|
|
462
556
|
getIterceptorHandler,
|
|
463
557
|
resolveArgs,
|
|
464
558
|
logHandler: (eventName) => {
|
|
465
|
-
options.
|
|
559
|
+
options.moostInstance.logMappedHandler(eventName, classConstructor, method);
|
|
466
560
|
},
|
|
467
561
|
register(h, path, args) {
|
|
468
562
|
const data = wm.get(h);
|
|
@@ -618,9 +712,6 @@ function Const(value, label) {
|
|
|
618
712
|
function ConstFactory(factory, label) {
|
|
619
713
|
return Resolve(async () => factory(), label);
|
|
620
714
|
}
|
|
621
|
-
function InjectEventLogger(topic) {
|
|
622
|
-
return Resolve(() => eventCore.useEventLogger(topic));
|
|
623
|
-
}
|
|
624
715
|
function fillLabel(target, key, index, name) {
|
|
625
716
|
if (name) {
|
|
626
717
|
const meta = getMoostMate().read(target, key);
|
|
@@ -670,10 +761,10 @@ class Moost extends hookable.Hookable {
|
|
|
670
761
|
this.provide = infact$1.createProvideRegistry([infact$1.Infact, getMoostInfact], [mate$1.Mate, getMoostMate]);
|
|
671
762
|
this.replace = {};
|
|
672
763
|
this.unregisteredControllers = [];
|
|
673
|
-
this.logger = options?.logger || getDefaultLogger(`${'[2m' + '[35m'}
|
|
674
|
-
|
|
764
|
+
this.logger = options?.logger || getDefaultLogger(`${'[2m' + '[35m'}moost`);
|
|
765
|
+
setDefaultLogger(this.logger);
|
|
675
766
|
const mate = getMoostMate();
|
|
676
|
-
Object.assign(mate, { logger: this.getLogger('
|
|
767
|
+
Object.assign(mate, { logger: this.getLogger('mate') });
|
|
677
768
|
}
|
|
678
769
|
_fireEventStart(source) {
|
|
679
770
|
this.callHook('event-start', source);
|
|
@@ -682,7 +773,7 @@ class Moost extends hookable.Hookable {
|
|
|
682
773
|
this.callHook('event-end', source);
|
|
683
774
|
}
|
|
684
775
|
getLogger(topic) {
|
|
685
|
-
if (this.logger instanceof logger.ProstoLogger) {
|
|
776
|
+
if (topic && this.logger instanceof logger.ProstoLogger) {
|
|
686
777
|
return this.logger.createTopic(topic);
|
|
687
778
|
}
|
|
688
779
|
return this.logger;
|
|
@@ -712,8 +803,6 @@ class Moost extends hookable.Hookable {
|
|
|
712
803
|
}
|
|
713
804
|
}
|
|
714
805
|
async bindControllers() {
|
|
715
|
-
const infact = getMoostInfact();
|
|
716
|
-
infact.setLogger(this.logger);
|
|
717
806
|
const meta = getMoostMate();
|
|
718
807
|
const thisMeta = meta.read(this);
|
|
719
808
|
const provide = { ...thisMeta?.provide, ...this.provide };
|
|
@@ -753,14 +842,9 @@ class Moost extends hookable.Hookable {
|
|
|
753
842
|
}
|
|
754
843
|
const getInstance = instance
|
|
755
844
|
? () => Promise.resolve(instance)
|
|
756
|
-
: async () => {
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
...infactOpts,
|
|
760
|
-
}));
|
|
761
|
-
infact.silent(false);
|
|
762
|
-
return instance;
|
|
763
|
-
};
|
|
845
|
+
: async () => (await infact.get(controller, {
|
|
846
|
+
...infactOpts,
|
|
847
|
+
}));
|
|
764
848
|
const classConstructor = mate$1.isConstructor(controller)
|
|
765
849
|
? controller
|
|
766
850
|
: mate$1.getConstructor(controller);
|
|
@@ -775,6 +859,7 @@ class Moost extends hookable.Hookable {
|
|
|
775
859
|
provide: classMeta?.provide,
|
|
776
860
|
replace: classMeta?.replace,
|
|
777
861
|
logger: this.logger,
|
|
862
|
+
moostInstance: this,
|
|
778
863
|
}));
|
|
779
864
|
if (classMeta && classMeta.importController) {
|
|
780
865
|
const prefix = typeof replaceOwnPrefix === 'string' ? replaceOwnPrefix : classMeta.controller?.prefix;
|
|
@@ -858,6 +943,11 @@ class Moost extends hookable.Hookable {
|
|
|
858
943
|
this.unregisteredControllers.push(...controllers);
|
|
859
944
|
return this;
|
|
860
945
|
}
|
|
946
|
+
logMappedHandler(eventName, classConstructor, method, stroke, prefix) {
|
|
947
|
+
const c = stroke ? __DYE_CROSSED__ : '';
|
|
948
|
+
const coff = stroke ? __DYE_CROSSED_OFF__ : '';
|
|
949
|
+
this.logger.info(`${prefix || ''}${c}${eventName} ${'[0m' + '[2m' + '[32m' + c}→ ${classConstructor.name}.${'[36m' + c}${method}${'[32m'}()${coff}`);
|
|
950
|
+
}
|
|
861
951
|
}
|
|
862
952
|
|
|
863
953
|
Object.defineProperty(exports, "getConstructor", {
|
|
@@ -900,6 +990,14 @@ Object.defineProperty(exports, "createReplaceRegistry", {
|
|
|
900
990
|
enumerable: true,
|
|
901
991
|
get: function () { return infact$1.createReplaceRegistry; }
|
|
902
992
|
});
|
|
993
|
+
Object.defineProperty(exports, "ProstoLogger", {
|
|
994
|
+
enumerable: true,
|
|
995
|
+
get: function () { return logger.ProstoLogger; }
|
|
996
|
+
});
|
|
997
|
+
Object.defineProperty(exports, "clearGlobalWooks", {
|
|
998
|
+
enumerable: true,
|
|
999
|
+
get: function () { return wooks.clearGlobalWooks; }
|
|
1000
|
+
});
|
|
903
1001
|
Object.defineProperty(exports, "getGlobalWooks", {
|
|
904
1002
|
enumerable: true,
|
|
905
1003
|
get: function () { return wooks.getGlobalWooks; }
|
|
@@ -913,7 +1011,6 @@ exports.Id = Id;
|
|
|
913
1011
|
exports.ImportController = ImportController;
|
|
914
1012
|
exports.Inherit = Inherit;
|
|
915
1013
|
exports.Inject = Inject;
|
|
916
|
-
exports.InjectEventLogger = InjectEventLogger;
|
|
917
1014
|
exports.Injectable = Injectable;
|
|
918
1015
|
exports.Intercept = Intercept;
|
|
919
1016
|
exports.InterceptorHandler = InterceptorHandler;
|
|
@@ -939,4 +1036,5 @@ exports.getNewMoostInfact = getNewMoostInfact;
|
|
|
939
1036
|
exports.registerEventScope = registerEventScope;
|
|
940
1037
|
exports.resolvePipe = resolvePipe;
|
|
941
1038
|
exports.setControllerContext = setControllerContext;
|
|
1039
|
+
exports.setInfactLoggingOptions = setInfactLoggingOptions;
|
|
942
1040
|
exports.useControllerContext = useControllerContext;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { useEventLogger } from '@wooksjs/event-core';
|
|
2
2
|
export { ContextInjector, EventLogger, THook, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventLogger } from '@wooksjs/event-core';
|
|
3
|
+
import { TConsoleBase } from '@prostojs/logger';
|
|
4
|
+
export { ProstoLogger, TConsoleBase } from '@prostojs/logger';
|
|
3
5
|
import { TProvideRegistry, Infact, TReplaceRegistry, TProvideFn } from '@prostojs/infact';
|
|
4
6
|
export { TProvideRegistry, createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
|
|
5
|
-
import { TConsoleBase } from '@prostojs/logger';
|
|
6
7
|
import { Hookable } from 'hookable';
|
|
7
8
|
import * as _prostojs_mate from '@prostojs/mate';
|
|
8
9
|
import { TMateParamMeta, Mate } from '@prostojs/mate';
|
|
9
10
|
export { getConstructor, isConstructor } from '@prostojs/mate';
|
|
10
|
-
export { getGlobalWooks } from 'wooks';
|
|
11
|
+
export { clearGlobalWooks, getGlobalWooks } from 'wooks';
|
|
11
12
|
|
|
12
13
|
type TAny = any;
|
|
13
14
|
type TAnyFn = (...a: TAny[]) => unknown;
|
|
@@ -136,6 +137,12 @@ declare function Intercept(handler: TCallableClassFunction<TInterceptorFn>, prio
|
|
|
136
137
|
|
|
137
138
|
type TDecoratorLevel = 'CLASS' | 'METHOD' | 'PROP' | 'PARAM';
|
|
138
139
|
|
|
140
|
+
interface TInfactLoggingOptions {
|
|
141
|
+
newInstance?: true | false | 'FOR_EVENT' | 'SINGLETON';
|
|
142
|
+
warn?: true | false;
|
|
143
|
+
error?: true | false;
|
|
144
|
+
}
|
|
145
|
+
declare function setInfactLoggingOptions(options: TInfactLoggingOptions): void;
|
|
139
146
|
declare function getMoostInfact(): Infact<TMoostMetadata<TEmpty>, TMoostMetadata<TEmpty>, TMoostParamsMetadata, TCustom>;
|
|
140
147
|
interface TCustom {
|
|
141
148
|
pipes?: TPipeData[];
|
|
@@ -192,6 +199,7 @@ interface TMoostMetadata<H extends TObject = TEmpty> extends TCommonMetaFields,
|
|
|
192
199
|
returnType?: TFunction;
|
|
193
200
|
provide?: TProvideRegistry;
|
|
194
201
|
replace?: TReplaceRegistry;
|
|
202
|
+
loggerTopic?: string;
|
|
195
203
|
params: Array<TMateParamMeta & TMoostParamsMetadata>;
|
|
196
204
|
}
|
|
197
205
|
interface TMoostParamsMetadata extends TCommonMetaFields, TCommonMoostMeta {
|
|
@@ -314,12 +322,6 @@ declare function Const<T>(value: T, label?: string): ParameterDecorator & Proper
|
|
|
314
322
|
* @paramType unknown
|
|
315
323
|
*/
|
|
316
324
|
declare function ConstFactory<T>(factory: () => T | Promise<T>, label?: string): ParameterDecorator & PropertyDecorator;
|
|
317
|
-
/**
|
|
318
|
-
* Resolves event logger from event context
|
|
319
|
-
* @param topic
|
|
320
|
-
* @returns Resolver to '@wooksjs/event-core' (EventLogger)
|
|
321
|
-
*/
|
|
322
|
-
declare function InjectEventLogger(topic?: string): ParameterDecorator & PropertyDecorator;
|
|
323
325
|
|
|
324
326
|
declare class InterceptorHandler {
|
|
325
327
|
protected handlers: Array<{
|
|
@@ -432,7 +434,7 @@ interface TMoostOptions {
|
|
|
432
434
|
* │ const app = new MyServer()
|
|
433
435
|
* │ const http = new MoostHttp()
|
|
434
436
|
* │ app.adapter(http).listen(3000, () => {
|
|
435
|
-
* │ app.getLogger('
|
|
437
|
+
* │ app.getLogger('MyApp').log('Up on port 3000')
|
|
436
438
|
* │ })
|
|
437
439
|
* │ app.init()
|
|
438
440
|
* ```
|
|
@@ -480,13 +482,13 @@ declare class Moost extends Hookable {
|
|
|
480
482
|
* Provides application logger
|
|
481
483
|
* ```js
|
|
482
484
|
* // get logger with topic = "App"
|
|
483
|
-
* const logger = app.getLogger('
|
|
485
|
+
* const logger = app.getLogger('App')
|
|
484
486
|
* logger.log('...')
|
|
485
487
|
* ```
|
|
486
488
|
* @param topic
|
|
487
489
|
* @returns
|
|
488
490
|
*/
|
|
489
|
-
getLogger(topic
|
|
491
|
+
getLogger(topic?: string): TConsoleBase;
|
|
490
492
|
adapter<T extends TMoostAdapter<TAny>>(a: T): T;
|
|
491
493
|
getControllersOverview(): TControllerOverview[];
|
|
492
494
|
/**
|
|
@@ -524,6 +526,7 @@ declare class Moost extends Hookable {
|
|
|
524
526
|
* @returns
|
|
525
527
|
*/
|
|
526
528
|
registerControllers(...controllers: Array<TObject | TFunction | [string, TObject | TFunction]>): this;
|
|
529
|
+
logMappedHandler(eventName: string, classConstructor: Function, method: string, stroke?: boolean, prefix?: string): void;
|
|
527
530
|
}
|
|
528
531
|
interface TMoostAdapterOptions<H, T> {
|
|
529
532
|
prefix: string;
|
|
@@ -637,4 +640,4 @@ type TInterceptorClass = TClassFunction<TInterceptorFn>;
|
|
|
637
640
|
*/
|
|
638
641
|
declare function definePipeFn<T extends TObject = TEmpty>(fn: TPipeFn<T>, priority?: TPipePriority): TPipeFn<T>;
|
|
639
642
|
|
|
640
|
-
export { Circular, Const, ConstFactory, Controller, Description, Id, ImportController, Inherit, Inject,
|
|
643
|
+
export { Circular, Const, ConstFactory, Controller, Description, Id, ImportController, Inherit, Inject, Injectable, Intercept, InterceptorHandler, Label, Moost, Optional, Param, Params, Pipe, Provide, Replace, Required, Resolve, type TCallableClassFunction, type TClassConstructor, type TClassFunction, type TContextInjectorHook, type TControllerOverview, type TInjectableScope, type TInterceptorAfter, type TInterceptorBefore, type TInterceptorClass, type TInterceptorData, type TInterceptorFn, type TInterceptorOnError, TInterceptorPriority, type TMoostAdapter, type TMoostAdapterOptions, type TMoostEventHandlerHookOptions, type TMoostEventHandlerOptions, type TMoostHandler, type TMoostMetadata, type TMoostOptions, type TMoostParamsMetadata, type TPipeData, type TPipeFn, type TPipeMetas, TPipePriority, Value, defineInterceptorFn, defineMoostEventHandler, definePipeFn, getInstanceOwnMethods, getInstanceOwnProps, getMoostInfact, getMoostMate, getNewMoostInfact, registerEventScope, resolvePipe, setControllerContext, setInfactLoggingOptions, useControllerContext };
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,33 @@
|
|
|
1
1
|
import { Mate, getConstructor, isConstructor } from '@prostojs/mate';
|
|
2
2
|
export { getConstructor, isConstructor } from '@prostojs/mate';
|
|
3
|
-
import { useEventId, useAsyncEventContext, getContextInjector,
|
|
3
|
+
import { useEventId, useEventLogger, useAsyncEventContext, getContextInjector, useRouteParams, createAsyncEventContext } from '@wooksjs/event-core';
|
|
4
4
|
export { ContextInjector, EventLogger, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventLogger } from '@wooksjs/event-core';
|
|
5
5
|
import { Infact, createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
|
|
6
6
|
export { createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
|
|
7
7
|
import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
|
|
8
|
+
export { ProstoLogger } from '@prostojs/logger';
|
|
8
9
|
import { Hookable } from 'hookable';
|
|
9
|
-
export { getGlobalWooks } from 'wooks';
|
|
10
|
+
export { clearGlobalWooks, getGlobalWooks } from 'wooks';
|
|
11
|
+
|
|
12
|
+
let defaultLogger;
|
|
13
|
+
function setDefaultLogger(logger) {
|
|
14
|
+
defaultLogger = logger;
|
|
15
|
+
}
|
|
16
|
+
function getDefaultLogger(topic) {
|
|
17
|
+
if (!defaultLogger) {
|
|
18
|
+
defaultLogger = new ProstoLogger({
|
|
19
|
+
level: 4,
|
|
20
|
+
transports: [
|
|
21
|
+
createConsoleTransort({
|
|
22
|
+
format: coloredConsole,
|
|
23
|
+
}),
|
|
24
|
+
],
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return topic && defaultLogger instanceof ProstoLogger
|
|
28
|
+
? defaultLogger.createTopic(topic)
|
|
29
|
+
: defaultLogger;
|
|
30
|
+
}
|
|
10
31
|
|
|
11
32
|
async function runPipes(pipes, initialValue, metas, level) {
|
|
12
33
|
let v = initialValue;
|
|
@@ -36,6 +57,17 @@ function getMoostMate() {
|
|
|
36
57
|
}
|
|
37
58
|
|
|
38
59
|
const sharedMoostInfact = getNewMoostInfact();
|
|
60
|
+
let loggingOptions = {
|
|
61
|
+
newInstance: 'SINGLETON',
|
|
62
|
+
warn: true,
|
|
63
|
+
error: true,
|
|
64
|
+
};
|
|
65
|
+
function setInfactLoggingOptions(options) {
|
|
66
|
+
loggingOptions = {
|
|
67
|
+
...loggingOptions,
|
|
68
|
+
...options,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
39
71
|
function getMoostInfact() {
|
|
40
72
|
return sharedMoostInfact;
|
|
41
73
|
}
|
|
@@ -81,6 +113,82 @@ function getNewMoostInfact() {
|
|
|
81
113
|
}
|
|
82
114
|
},
|
|
83
115
|
storeProvideRegByInstance: true,
|
|
116
|
+
on: (event, targetClass, message, args) => {
|
|
117
|
+
switch (event) {
|
|
118
|
+
case 'new-instance': {
|
|
119
|
+
const isForEvent = getMoostMate().read(targetClass)?.injectable !== 'SINGLETON';
|
|
120
|
+
const isSingleton = !isForEvent;
|
|
121
|
+
if (loggingOptions.newInstance === false ||
|
|
122
|
+
(loggingOptions.newInstance === 'FOR_EVENT' && !isForEvent) ||
|
|
123
|
+
(loggingOptions.newInstance === 'SINGLETON' && isSingleton)) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case 'warn': {
|
|
129
|
+
if (!loggingOptions.warn) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case 'error': {
|
|
135
|
+
if (!loggingOptions.error) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
let logger;
|
|
142
|
+
try {
|
|
143
|
+
logger = useEventLogger('infact');
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
logger = getDefaultLogger('infact');
|
|
147
|
+
}
|
|
148
|
+
const instance = `${'[4m'}${targetClass.name}${'[24m'}`;
|
|
149
|
+
switch (event) {
|
|
150
|
+
case 'new-instance': {
|
|
151
|
+
const params = args
|
|
152
|
+
?.map(a => {
|
|
153
|
+
switch (typeof a) {
|
|
154
|
+
case 'number':
|
|
155
|
+
case 'boolean': {
|
|
156
|
+
return `${'[33m'}${a}${'[2m' + '[34m'}`;
|
|
157
|
+
}
|
|
158
|
+
case 'string': {
|
|
159
|
+
return `${'[92m'}"${a.slice(0, 1)}..."${'[2m' + '[34m'}`;
|
|
160
|
+
}
|
|
161
|
+
case 'object': {
|
|
162
|
+
if (Array.isArray(a)) {
|
|
163
|
+
return `[${a.length}]`;
|
|
164
|
+
}
|
|
165
|
+
if (getConstructor(a)) {
|
|
166
|
+
return getConstructor(a).name;
|
|
167
|
+
}
|
|
168
|
+
return '{}';
|
|
169
|
+
}
|
|
170
|
+
default: {
|
|
171
|
+
return '*';
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
.map(a => `${'[2m' + '[1m'}${a}${'[22m' + '[2m'}`)
|
|
176
|
+
.join(', ') || '';
|
|
177
|
+
logger.info(`new ${instance}${'[2m' + '[34m'}(${params})`);
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
case 'warn': {
|
|
181
|
+
const hier = `${'[2m' + '[34m'}⋱ ${args?.map(String).join(' → ') || ''}`;
|
|
182
|
+
logger.warn(`${instance} - ${message} ${hier}`);
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case 'error': {
|
|
186
|
+
const hier = `${'[2m' + '[34m'}⋱ ${args?.map(String).join(' → ') || ''}`;
|
|
187
|
+
logger.error(`Failed to instantiate ${instance}. ${message} ${hier}`);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
},
|
|
84
192
|
});
|
|
85
193
|
}
|
|
86
194
|
|
|
@@ -254,29 +362,16 @@ function getParentProps(constructor) {
|
|
|
254
362
|
return [];
|
|
255
363
|
}
|
|
256
364
|
|
|
257
|
-
function getDefaultLogger(topic) {
|
|
258
|
-
return new ProstoLogger({
|
|
259
|
-
level: 4,
|
|
260
|
-
transports: [
|
|
261
|
-
createConsoleTransort({
|
|
262
|
-
format: coloredConsole,
|
|
263
|
-
}),
|
|
264
|
-
],
|
|
265
|
-
}, topic);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
365
|
async function getCallableFn(targetInstance, fn, pipes, logger) {
|
|
269
366
|
const mate = getMoostMate();
|
|
270
367
|
const meta = mate.read(fn);
|
|
271
368
|
if (meta?.injectable) {
|
|
272
369
|
const infact = getMoostInfact();
|
|
273
|
-
infact.silent(true);
|
|
274
370
|
const instance = (await infact.getForInstance(targetInstance, fn, {
|
|
275
371
|
customData: {
|
|
276
372
|
pipes: [...(pipes || []), ...(meta.pipes || [])].sort((a, b) => a.priority - b.priority),
|
|
277
373
|
},
|
|
278
374
|
}));
|
|
279
|
-
infact.silent(false);
|
|
280
375
|
return ((...args) => instance.handler(...args));
|
|
281
376
|
}
|
|
282
377
|
if (typeof fn === 'function') {
|
|
@@ -463,7 +558,7 @@ async function bindControllerMethods(options) {
|
|
|
463
558
|
getIterceptorHandler,
|
|
464
559
|
resolveArgs,
|
|
465
560
|
logHandler: (eventName) => {
|
|
466
|
-
options.
|
|
561
|
+
options.moostInstance.logMappedHandler(eventName, classConstructor, method);
|
|
467
562
|
},
|
|
468
563
|
register(h, path, args) {
|
|
469
564
|
const data = wm.get(h);
|
|
@@ -619,9 +714,6 @@ function Const(value, label) {
|
|
|
619
714
|
function ConstFactory(factory, label) {
|
|
620
715
|
return Resolve(async () => factory(), label);
|
|
621
716
|
}
|
|
622
|
-
function InjectEventLogger(topic) {
|
|
623
|
-
return Resolve(() => useEventLogger(topic));
|
|
624
|
-
}
|
|
625
717
|
function fillLabel(target, key, index, name) {
|
|
626
718
|
if (name) {
|
|
627
719
|
const meta = getMoostMate().read(target, key);
|
|
@@ -671,10 +763,10 @@ class Moost extends Hookable {
|
|
|
671
763
|
this.provide = createProvideRegistry([Infact, getMoostInfact], [Mate, getMoostMate]);
|
|
672
764
|
this.replace = {};
|
|
673
765
|
this.unregisteredControllers = [];
|
|
674
|
-
this.logger = options?.logger || getDefaultLogger(`${'[2m' + '[35m'}
|
|
675
|
-
|
|
766
|
+
this.logger = options?.logger || getDefaultLogger(`${'[2m' + '[35m'}moost`);
|
|
767
|
+
setDefaultLogger(this.logger);
|
|
676
768
|
const mate = getMoostMate();
|
|
677
|
-
Object.assign(mate, { logger: this.getLogger('
|
|
769
|
+
Object.assign(mate, { logger: this.getLogger('mate') });
|
|
678
770
|
}
|
|
679
771
|
_fireEventStart(source) {
|
|
680
772
|
this.callHook('event-start', source);
|
|
@@ -683,7 +775,7 @@ class Moost extends Hookable {
|
|
|
683
775
|
this.callHook('event-end', source);
|
|
684
776
|
}
|
|
685
777
|
getLogger(topic) {
|
|
686
|
-
if (this.logger instanceof ProstoLogger) {
|
|
778
|
+
if (topic && this.logger instanceof ProstoLogger) {
|
|
687
779
|
return this.logger.createTopic(topic);
|
|
688
780
|
}
|
|
689
781
|
return this.logger;
|
|
@@ -713,8 +805,6 @@ class Moost extends Hookable {
|
|
|
713
805
|
}
|
|
714
806
|
}
|
|
715
807
|
async bindControllers() {
|
|
716
|
-
const infact = getMoostInfact();
|
|
717
|
-
infact.setLogger(this.logger);
|
|
718
808
|
const meta = getMoostMate();
|
|
719
809
|
const thisMeta = meta.read(this);
|
|
720
810
|
const provide = { ...thisMeta?.provide, ...this.provide };
|
|
@@ -754,14 +844,9 @@ class Moost extends Hookable {
|
|
|
754
844
|
}
|
|
755
845
|
const getInstance = instance
|
|
756
846
|
? () => Promise.resolve(instance)
|
|
757
|
-
: async () => {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
...infactOpts,
|
|
761
|
-
}));
|
|
762
|
-
infact.silent(false);
|
|
763
|
-
return instance;
|
|
764
|
-
};
|
|
847
|
+
: async () => (await infact.get(controller, {
|
|
848
|
+
...infactOpts,
|
|
849
|
+
}));
|
|
765
850
|
const classConstructor = isConstructor(controller)
|
|
766
851
|
? controller
|
|
767
852
|
: getConstructor(controller);
|
|
@@ -776,6 +861,7 @@ class Moost extends Hookable {
|
|
|
776
861
|
provide: classMeta?.provide,
|
|
777
862
|
replace: classMeta?.replace,
|
|
778
863
|
logger: this.logger,
|
|
864
|
+
moostInstance: this,
|
|
779
865
|
}));
|
|
780
866
|
if (classMeta && classMeta.importController) {
|
|
781
867
|
const prefix = typeof replaceOwnPrefix === 'string' ? replaceOwnPrefix : classMeta.controller?.prefix;
|
|
@@ -859,6 +945,11 @@ class Moost extends Hookable {
|
|
|
859
945
|
this.unregisteredControllers.push(...controllers);
|
|
860
946
|
return this;
|
|
861
947
|
}
|
|
948
|
+
logMappedHandler(eventName, classConstructor, method, stroke, prefix) {
|
|
949
|
+
const c = stroke ? __DYE_CROSSED__ : '';
|
|
950
|
+
const coff = stroke ? __DYE_CROSSED_OFF__ : '';
|
|
951
|
+
this.logger.info(`${prefix || ''}${c}${eventName} ${'[0m' + '[2m' + '[32m' + c}→ ${classConstructor.name}.${'[36m' + c}${method}${'[32m'}()${coff}`);
|
|
952
|
+
}
|
|
862
953
|
}
|
|
863
954
|
|
|
864
|
-
export { Circular, Const, ConstFactory, Controller, Description, Id, ImportController, Inherit, Inject,
|
|
955
|
+
export { Circular, Const, ConstFactory, Controller, Description, Id, ImportController, Inherit, Inject, Injectable, Intercept, InterceptorHandler, Label, Moost, Optional, Param, Params, Pipe, Provide, Replace, Required, Resolve, TInterceptorPriority, TPipePriority, Value, defineInterceptorFn, defineMoostEventHandler, definePipeFn, getInstanceOwnMethods, getInstanceOwnProps, getMoostInfact, getMoostMate, getNewMoostInfact, registerEventScope, resolvePipe, setControllerContext, setInfactLoggingOptions, useControllerContext };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "moost",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "moost",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"prostojs"
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@prostojs/infact": "^0.
|
|
34
|
+
"@prostojs/infact": "^0.3.0",
|
|
35
35
|
"@prostojs/mate": "^0.3.3",
|
|
36
36
|
"@prostojs/logger": "^0.4.3",
|
|
37
|
-
"@wooksjs/event-core": "^0.5.
|
|
37
|
+
"@wooksjs/event-core": "^0.5.19",
|
|
38
38
|
"hookable": "^5.5.3",
|
|
39
|
-
"wooks": "^0.5.
|
|
39
|
+
"wooks": "^0.5.19"
|
|
40
40
|
},
|
|
41
41
|
"author": "Artem Maltsev",
|
|
42
42
|
"license": "MIT",
|