moost 0.4.6 → 0.4.8
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 +72 -30
- package/dist/index.d.ts +38 -18
- package/dist/index.mjs +57 -23
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var mate$1 = require('@prostojs/mate');
|
|
3
4
|
var eventCore = require('@wooksjs/event-core');
|
|
4
5
|
var infact$1 = require('@prostojs/infact');
|
|
5
|
-
var mate$1 = require('@prostojs/mate');
|
|
6
6
|
var logger = require('@prostojs/logger');
|
|
7
7
|
var hookable = require('hookable');
|
|
8
8
|
|
|
@@ -82,17 +82,19 @@ function getNewMoostInfact() {
|
|
|
82
82
|
});
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
function setControllerContext(controller, method) {
|
|
85
|
+
function setControllerContext(controller, method, route) {
|
|
86
86
|
const { store } = eventCore.useAsyncEventContext();
|
|
87
87
|
const { set } = store('controller');
|
|
88
88
|
set('instance', controller);
|
|
89
89
|
set('method', method);
|
|
90
|
+
set('route', route);
|
|
90
91
|
}
|
|
91
92
|
function useControllerContext() {
|
|
92
93
|
const { store } = eventCore.useAsyncEventContext();
|
|
93
94
|
const { get } = store('controller');
|
|
94
95
|
const getController = () => get('instance');
|
|
95
96
|
const getMethod = () => get('method');
|
|
97
|
+
const getRoute = () => get('route');
|
|
96
98
|
const getControllerMeta = () => getMoostMate().read(getController());
|
|
97
99
|
const getMethodMeta = (name) => getMoostMate().read(getController(), name || getMethod());
|
|
98
100
|
function instantiate(c) {
|
|
@@ -100,6 +102,7 @@ function useControllerContext() {
|
|
|
100
102
|
}
|
|
101
103
|
return {
|
|
102
104
|
instantiate,
|
|
105
|
+
getRoute,
|
|
103
106
|
getController,
|
|
104
107
|
getMethod,
|
|
105
108
|
getControllerMeta,
|
|
@@ -119,6 +122,7 @@ function registerEventScope(scopeId) {
|
|
|
119
122
|
};
|
|
120
123
|
}
|
|
121
124
|
function defineMoostEventHandler(options) {
|
|
125
|
+
const ci = eventCore.getContextInjector();
|
|
122
126
|
return async () => {
|
|
123
127
|
const scopeId = eventCore.useEventId().getId();
|
|
124
128
|
const logger = eventCore.useEventLogger(options.loggerTitle);
|
|
@@ -137,12 +141,12 @@ function defineMoostEventHandler(options) {
|
|
|
137
141
|
}
|
|
138
142
|
const instance = await options.getControllerInstance();
|
|
139
143
|
if (instance) {
|
|
140
|
-
setControllerContext(instance, options.controllerMethod || '');
|
|
144
|
+
setControllerContext(instance, options.controllerMethod || '', options.targetPath);
|
|
141
145
|
}
|
|
142
146
|
const interceptorHandler = await options.getIterceptorHandler();
|
|
143
147
|
if (interceptorHandler) {
|
|
144
148
|
try {
|
|
145
|
-
response = await interceptorHandler.init();
|
|
149
|
+
response = await ci.with('Inteceptors:init', () => interceptorHandler.init());
|
|
146
150
|
if (response !== undefined) {
|
|
147
151
|
return await endWithResponse();
|
|
148
152
|
}
|
|
@@ -156,7 +160,7 @@ function defineMoostEventHandler(options) {
|
|
|
156
160
|
let args = [];
|
|
157
161
|
if (options.resolveArgs) {
|
|
158
162
|
try {
|
|
159
|
-
args = await options.resolveArgs();
|
|
163
|
+
args = await ci.with('Arguments:resolve', () => options.resolveArgs());
|
|
160
164
|
}
|
|
161
165
|
catch (error) {
|
|
162
166
|
options.logErrors && logger.error(error);
|
|
@@ -165,7 +169,7 @@ function defineMoostEventHandler(options) {
|
|
|
165
169
|
}
|
|
166
170
|
}
|
|
167
171
|
if (interceptorHandler) {
|
|
168
|
-
response = await interceptorHandler.fireBefore(response);
|
|
172
|
+
response = await ci.with('Inteceptors:before', () => interceptorHandler.fireBefore(response));
|
|
169
173
|
if (response !== undefined) {
|
|
170
174
|
return endWithResponse();
|
|
171
175
|
}
|
|
@@ -181,7 +185,10 @@ function defineMoostEventHandler(options) {
|
|
|
181
185
|
}
|
|
182
186
|
};
|
|
183
187
|
try {
|
|
184
|
-
response =
|
|
188
|
+
response = await ci.with('Handler', {
|
|
189
|
+
'moost.handler': options.controllerMethod || '',
|
|
190
|
+
'moost.controller': mate$1.getConstructor(instance).name,
|
|
191
|
+
}, () => callControllerMethod());
|
|
185
192
|
}
|
|
186
193
|
catch (error) {
|
|
187
194
|
options.logErrors && logger.error(error);
|
|
@@ -191,7 +198,7 @@ function defineMoostEventHandler(options) {
|
|
|
191
198
|
async function endWithResponse(raise = false) {
|
|
192
199
|
if (interceptorHandler) {
|
|
193
200
|
try {
|
|
194
|
-
response = await interceptorHandler.fireAfter(response);
|
|
201
|
+
response = await ci.with('Inteceptors:after', () => interceptorHandler.fireAfter(response));
|
|
195
202
|
}
|
|
196
203
|
catch (error) {
|
|
197
204
|
options.logErrors && logger.error(error);
|
|
@@ -285,28 +292,48 @@ class InterceptorHandler {
|
|
|
285
292
|
this.onError = [];
|
|
286
293
|
this.responseOverwritten = false;
|
|
287
294
|
}
|
|
295
|
+
get count() {
|
|
296
|
+
return this.handlers.length;
|
|
297
|
+
}
|
|
298
|
+
get countBefore() {
|
|
299
|
+
return this.before.length;
|
|
300
|
+
}
|
|
301
|
+
get countAfter() {
|
|
302
|
+
return this.after.length;
|
|
303
|
+
}
|
|
304
|
+
get countOnError() {
|
|
305
|
+
return this.onError.length;
|
|
306
|
+
}
|
|
288
307
|
replyFn(reply) {
|
|
289
308
|
this.response = reply;
|
|
290
309
|
this.responseOverwritten = true;
|
|
291
310
|
}
|
|
292
311
|
async init() {
|
|
312
|
+
const ci = eventCore.getContextInjector();
|
|
293
313
|
for (const handler of this.handlers) {
|
|
294
|
-
const response = await handler
|
|
295
|
-
|
|
314
|
+
const response = await ci.with(`Inteceptor:${handler.name}`, {
|
|
315
|
+
'moost.interceptor.stage': 'init',
|
|
316
|
+
'moost.interceptor.priority': handler.priority || '',
|
|
317
|
+
}, () => handler(fn => {
|
|
318
|
+
this.before.push({ handler, fn });
|
|
296
319
|
}, fn => {
|
|
297
|
-
this.after.unshift(fn);
|
|
320
|
+
this.after.unshift({ handler, fn });
|
|
298
321
|
}, fn => {
|
|
299
|
-
this.onError.unshift(fn);
|
|
300
|
-
});
|
|
322
|
+
this.onError.unshift({ handler, fn });
|
|
323
|
+
}));
|
|
301
324
|
if (response !== undefined) {
|
|
302
325
|
return response;
|
|
303
326
|
}
|
|
304
327
|
}
|
|
305
328
|
}
|
|
306
329
|
async fireBefore(response) {
|
|
330
|
+
const ci = eventCore.getContextInjector();
|
|
307
331
|
this.response = response;
|
|
308
|
-
for (const handler of this.before) {
|
|
309
|
-
await handler
|
|
332
|
+
for (const { handler, fn } of this.before) {
|
|
333
|
+
await ci.with(`Inteceptor:${handler.name}`, {
|
|
334
|
+
'moost.interceptor.stage': 'before',
|
|
335
|
+
'moost.interceptor.priority': handler.priority || '',
|
|
336
|
+
}, () => fn(this.replyFn.bind(this)));
|
|
310
337
|
if (this.responseOverwritten) {
|
|
311
338
|
break;
|
|
312
339
|
}
|
|
@@ -314,15 +341,22 @@ class InterceptorHandler {
|
|
|
314
341
|
return this.response;
|
|
315
342
|
}
|
|
316
343
|
async fireAfter(response) {
|
|
344
|
+
const ci = eventCore.getContextInjector();
|
|
317
345
|
this.response = response;
|
|
318
346
|
if (response instanceof Error) {
|
|
319
|
-
for (const handler of this.onError) {
|
|
320
|
-
await handler
|
|
347
|
+
for (const { handler, fn } of this.onError) {
|
|
348
|
+
await ci.with(`Inteceptor:${handler.name}`, {
|
|
349
|
+
'moost.interceptor.stage': 'after',
|
|
350
|
+
'moost.interceptor.priority': handler.priority || '',
|
|
351
|
+
}, () => fn(response, this.replyFn.bind(this)));
|
|
321
352
|
}
|
|
322
353
|
}
|
|
323
354
|
else {
|
|
324
|
-
for (const handler of this.after) {
|
|
325
|
-
await handler
|
|
355
|
+
for (const { handler, fn } of this.after) {
|
|
356
|
+
await ci.with(`Inteceptor:${handler.name}`, {
|
|
357
|
+
'moost.interceptor.stage': 'onError',
|
|
358
|
+
'moost.interceptor.priority': handler.priority || '',
|
|
359
|
+
}, () => fn(response, this.replyFn.bind(this)));
|
|
326
360
|
}
|
|
327
361
|
}
|
|
328
362
|
return this.response;
|
|
@@ -707,7 +741,7 @@ class Moost extends hookable.Hookable {
|
|
|
707
741
|
event: { type: 'init' },
|
|
708
742
|
options: {},
|
|
709
743
|
})(async () => {
|
|
710
|
-
setControllerContext(this, 'bindController');
|
|
744
|
+
setControllerContext(this, 'bindController', '');
|
|
711
745
|
instance = (await infact.get(controller, infactOpts));
|
|
712
746
|
});
|
|
713
747
|
}
|
|
@@ -819,13 +853,29 @@ class Moost extends hookable.Hookable {
|
|
|
819
853
|
}
|
|
820
854
|
}
|
|
821
855
|
|
|
856
|
+
Object.defineProperty(exports, "getConstructor", {
|
|
857
|
+
enumerable: true,
|
|
858
|
+
get: function () { return mate$1.getConstructor; }
|
|
859
|
+
});
|
|
860
|
+
Object.defineProperty(exports, "isConstructor", {
|
|
861
|
+
enumerable: true,
|
|
862
|
+
get: function () { return mate$1.isConstructor; }
|
|
863
|
+
});
|
|
864
|
+
Object.defineProperty(exports, "ContextInjector", {
|
|
865
|
+
enumerable: true,
|
|
866
|
+
get: function () { return eventCore.ContextInjector; }
|
|
867
|
+
});
|
|
822
868
|
Object.defineProperty(exports, "EventLogger", {
|
|
823
869
|
enumerable: true,
|
|
824
870
|
get: function () { return eventCore.EventLogger; }
|
|
825
871
|
});
|
|
826
|
-
Object.defineProperty(exports, "
|
|
872
|
+
Object.defineProperty(exports, "getContextInjector", {
|
|
827
873
|
enumerable: true,
|
|
828
|
-
get: function () { return eventCore.
|
|
874
|
+
get: function () { return eventCore.getContextInjector; }
|
|
875
|
+
});
|
|
876
|
+
Object.defineProperty(exports, "replaceContextInjector", {
|
|
877
|
+
enumerable: true,
|
|
878
|
+
get: function () { return eventCore.replaceContextInjector; }
|
|
829
879
|
});
|
|
830
880
|
Object.defineProperty(exports, "useAsyncEventContext", {
|
|
831
881
|
enumerable: true,
|
|
@@ -843,14 +893,6 @@ Object.defineProperty(exports, "createReplaceRegistry", {
|
|
|
843
893
|
enumerable: true,
|
|
844
894
|
get: function () { return infact$1.createReplaceRegistry; }
|
|
845
895
|
});
|
|
846
|
-
Object.defineProperty(exports, "getConstructor", {
|
|
847
|
-
enumerable: true,
|
|
848
|
-
get: function () { return mate$1.getConstructor; }
|
|
849
|
-
});
|
|
850
|
-
Object.defineProperty(exports, "isConstructor", {
|
|
851
|
-
enumerable: true,
|
|
852
|
-
get: function () { return mate$1.isConstructor; }
|
|
853
|
-
});
|
|
854
896
|
exports.Circular = Circular;
|
|
855
897
|
exports.Const = Const;
|
|
856
898
|
exports.ConstFactory = ConstFactory;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEventLogger } from '@wooksjs/event-core';
|
|
2
|
-
export { EventLogger, THook,
|
|
2
|
+
export { ContextInjector, EventLogger, THook, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventLogger } from '@wooksjs/event-core';
|
|
3
3
|
import { TProvideRegistry, Infact, TReplaceRegistry, TProvideFn } from '@prostojs/infact';
|
|
4
4
|
export { TProvideRegistry, createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
|
|
5
5
|
import { TConsoleBase } from '@prostojs/logger';
|
|
@@ -321,11 +321,24 @@ declare function InjectEventLogger(topic?: string): ParameterDecorator & Propert
|
|
|
321
321
|
declare class InterceptorHandler {
|
|
322
322
|
protected handlers: TInterceptorFn[];
|
|
323
323
|
constructor(handlers: TInterceptorFn[]);
|
|
324
|
-
protected before:
|
|
325
|
-
|
|
326
|
-
|
|
324
|
+
protected before: Array<{
|
|
325
|
+
handler: TInterceptorFn;
|
|
326
|
+
fn: TInterceptorBefore;
|
|
327
|
+
}>;
|
|
328
|
+
protected after: Array<{
|
|
329
|
+
handler: TInterceptorFn;
|
|
330
|
+
fn: TInterceptorAfter;
|
|
331
|
+
}>;
|
|
332
|
+
protected onError: Array<{
|
|
333
|
+
handler: TInterceptorFn;
|
|
334
|
+
fn: TInterceptorOnError;
|
|
335
|
+
}>;
|
|
327
336
|
response?: unknown;
|
|
328
337
|
responseOverwritten: boolean;
|
|
338
|
+
get count(): number;
|
|
339
|
+
get countBefore(): number;
|
|
340
|
+
get countAfter(): number;
|
|
341
|
+
get countOnError(): number;
|
|
329
342
|
replyFn(reply: unknown): void;
|
|
330
343
|
init(): Promise<{} | null | undefined>;
|
|
331
344
|
fireBefore(response: unknown): Promise<unknown>;
|
|
@@ -355,6 +368,7 @@ interface TMoostEventHandlerOptions<T> {
|
|
|
355
368
|
init?: (opts: TMoostEventHandlerHookOptions<T>) => unknown;
|
|
356
369
|
end?: (opts: TMoostEventHandlerHookOptions<T>) => unknown;
|
|
357
370
|
};
|
|
371
|
+
targetPath: string;
|
|
358
372
|
}
|
|
359
373
|
declare function registerEventScope(scopeId: string): () => void;
|
|
360
374
|
declare function defineMoostEventHandler<T>(options: TMoostEventHandlerOptions<T>): () => Promise<unknown>;
|
|
@@ -379,6 +393,7 @@ interface THandlerOverview {
|
|
|
379
393
|
args: string[];
|
|
380
394
|
}>;
|
|
381
395
|
}
|
|
396
|
+
type TContextInjectorHook = 'Event:start' | 'Inteceptors:init' | 'Arguments:resolve' | 'Inteceptors:before' | 'Handler' | 'Inteceptors:after';
|
|
382
397
|
|
|
383
398
|
interface TMoostOptions {
|
|
384
399
|
/**
|
|
@@ -518,27 +533,32 @@ interface TMoostAdapter<H> {
|
|
|
518
533
|
getProvideRegistry?: () => TProvideRegistry;
|
|
519
534
|
}
|
|
520
535
|
|
|
521
|
-
declare function setControllerContext<T>(controller: T, method: keyof T): void;
|
|
536
|
+
declare function setControllerContext<T>(controller: T, method: keyof T, route: string): void;
|
|
522
537
|
declare function useControllerContext<T extends object>(): {
|
|
523
|
-
instantiate: <
|
|
538
|
+
instantiate: <TT>(c: TClassConstructor<TT>) => Promise<TT>;
|
|
539
|
+
getRoute: () => string | undefined;
|
|
524
540
|
getController: () => T;
|
|
525
541
|
getMethod: () => string | undefined;
|
|
526
|
-
getControllerMeta: () => (TMoostMetadata<TEmpty> &
|
|
527
|
-
params: (
|
|
542
|
+
getControllerMeta: <TT extends object>() => (TMoostMetadata<TEmpty> & TT & {
|
|
543
|
+
params: (TT & _prostojs_mate.TMateParamMeta)[];
|
|
528
544
|
}) | undefined;
|
|
529
|
-
getMethodMeta: (name?: string) => (TMoostMetadata<TEmpty> &
|
|
530
|
-
params: (
|
|
545
|
+
getMethodMeta: <TT_1 extends object>(name?: string) => (TMoostMetadata<TEmpty> & TT_1 & {
|
|
546
|
+
params: (TT_1 & _prostojs_mate.TMateParamMeta)[];
|
|
531
547
|
} & {
|
|
532
|
-
params: (
|
|
533
|
-
} & _prostojs_mate.TMateClassMeta<
|
|
548
|
+
params: (TT_1 & _prostojs_mate.TMateParamMeta)[];
|
|
549
|
+
} & _prostojs_mate.TMateClassMeta<(TMoostMetadata<TEmpty> & TT_1 & {
|
|
550
|
+
params: (TT_1 & _prostojs_mate.TMateParamMeta)[];
|
|
551
|
+
})["params"][0]> & _prostojs_mate.TMatePropMeta<(TMoostMetadata<TEmpty> & TT_1 & {
|
|
552
|
+
params: (TT_1 & _prostojs_mate.TMateParamMeta)[];
|
|
553
|
+
})["params"][0]>) | undefined;
|
|
534
554
|
getPropertiesList: () => (string | symbol)[];
|
|
535
555
|
getScope: () => true | TInjectableScope;
|
|
536
|
-
getParamsMeta: () => (_prostojs_mate.TMateParamMeta & TMoostParamsMetadata)[] & (
|
|
537
|
-
getPropMeta: (name: string) => (TMoostMetadata<TEmpty> &
|
|
538
|
-
params: (
|
|
556
|
+
getParamsMeta: () => (_prostojs_mate.TMateParamMeta & TMoostParamsMetadata)[] & (object & _prostojs_mate.TMateParamMeta)[] & (_prostojs_mate.TMateParamMeta & TMoostParamsMetadata & object)[];
|
|
557
|
+
getPropMeta: (name: string) => (TMoostMetadata<TEmpty> & object & {
|
|
558
|
+
params: (object & _prostojs_mate.TMateParamMeta)[];
|
|
539
559
|
} & {
|
|
540
|
-
params: (
|
|
541
|
-
} & _prostojs_mate.TMateClassMeta<_prostojs_mate.TMateParamMeta & TMoostParamsMetadata &
|
|
560
|
+
params: (object & _prostojs_mate.TMateParamMeta)[];
|
|
561
|
+
} & _prostojs_mate.TMateClassMeta<_prostojs_mate.TMateParamMeta & TMoostParamsMetadata & object> & _prostojs_mate.TMatePropMeta<_prostojs_mate.TMateParamMeta & TMoostParamsMetadata & object>) | undefined;
|
|
542
562
|
};
|
|
543
563
|
|
|
544
564
|
/**
|
|
@@ -585,4 +605,4 @@ type TInterceptorClass = TClassFunction<TInterceptorFn>;
|
|
|
585
605
|
*/
|
|
586
606
|
declare function definePipeFn<T extends TObject = TEmpty>(fn: TPipeFn<T>, priority?: TPipePriority): TPipeFn<T>;
|
|
587
607
|
|
|
588
|
-
export { Circular, Const, ConstFactory, Controller, Description, Id, ImportController, Inherit, Inject, InjectEventLogger, Injectable, Intercept, InterceptorHandler, Label, Moost, Optional, Param, Params, Pipe, Provide, Replace, Required, Resolve, type TCallableClassFunction, type TClassConstructor, type TClassFunction, 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, useControllerContext };
|
|
608
|
+
export { Circular, Const, ConstFactory, Controller, Description, Id, ImportController, Inherit, Inject, InjectEventLogger, 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, useControllerContext };
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { useEventId, useAsyncEventContext, useEventLogger, useRouteParams, createAsyncEventContext } from '@wooksjs/event-core';
|
|
2
|
-
export { EventLogger, eventContextHooks, useAsyncEventContext, useEventLogger } from '@wooksjs/event-core';
|
|
3
|
-
import { Infact, createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
|
|
4
|
-
export { createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
|
|
5
1
|
import { Mate, getConstructor, isConstructor } from '@prostojs/mate';
|
|
6
2
|
export { getConstructor, isConstructor } from '@prostojs/mate';
|
|
3
|
+
import { useEventId, useAsyncEventContext, getContextInjector, useEventLogger, useRouteParams, createAsyncEventContext } from '@wooksjs/event-core';
|
|
4
|
+
export { ContextInjector, EventLogger, getContextInjector, replaceContextInjector, useAsyncEventContext, useEventLogger } from '@wooksjs/event-core';
|
|
5
|
+
import { Infact, createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
|
|
6
|
+
export { createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
|
|
7
7
|
import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
|
|
8
8
|
import { Hookable } from 'hookable';
|
|
9
9
|
|
|
@@ -83,17 +83,19 @@ function getNewMoostInfact() {
|
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
function setControllerContext(controller, method) {
|
|
86
|
+
function setControllerContext(controller, method, route) {
|
|
87
87
|
const { store } = useAsyncEventContext();
|
|
88
88
|
const { set } = store('controller');
|
|
89
89
|
set('instance', controller);
|
|
90
90
|
set('method', method);
|
|
91
|
+
set('route', route);
|
|
91
92
|
}
|
|
92
93
|
function useControllerContext() {
|
|
93
94
|
const { store } = useAsyncEventContext();
|
|
94
95
|
const { get } = store('controller');
|
|
95
96
|
const getController = () => get('instance');
|
|
96
97
|
const getMethod = () => get('method');
|
|
98
|
+
const getRoute = () => get('route');
|
|
97
99
|
const getControllerMeta = () => getMoostMate().read(getController());
|
|
98
100
|
const getMethodMeta = (name) => getMoostMate().read(getController(), name || getMethod());
|
|
99
101
|
function instantiate(c) {
|
|
@@ -101,6 +103,7 @@ function useControllerContext() {
|
|
|
101
103
|
}
|
|
102
104
|
return {
|
|
103
105
|
instantiate,
|
|
106
|
+
getRoute,
|
|
104
107
|
getController,
|
|
105
108
|
getMethod,
|
|
106
109
|
getControllerMeta,
|
|
@@ -120,6 +123,7 @@ function registerEventScope(scopeId) {
|
|
|
120
123
|
};
|
|
121
124
|
}
|
|
122
125
|
function defineMoostEventHandler(options) {
|
|
126
|
+
const ci = getContextInjector();
|
|
123
127
|
return async () => {
|
|
124
128
|
const scopeId = useEventId().getId();
|
|
125
129
|
const logger = useEventLogger(options.loggerTitle);
|
|
@@ -138,12 +142,12 @@ function defineMoostEventHandler(options) {
|
|
|
138
142
|
}
|
|
139
143
|
const instance = await options.getControllerInstance();
|
|
140
144
|
if (instance) {
|
|
141
|
-
setControllerContext(instance, options.controllerMethod || '');
|
|
145
|
+
setControllerContext(instance, options.controllerMethod || '', options.targetPath);
|
|
142
146
|
}
|
|
143
147
|
const interceptorHandler = await options.getIterceptorHandler();
|
|
144
148
|
if (interceptorHandler) {
|
|
145
149
|
try {
|
|
146
|
-
response = await interceptorHandler.init();
|
|
150
|
+
response = await ci.with('Inteceptors:init', () => interceptorHandler.init());
|
|
147
151
|
if (response !== undefined) {
|
|
148
152
|
return await endWithResponse();
|
|
149
153
|
}
|
|
@@ -157,7 +161,7 @@ function defineMoostEventHandler(options) {
|
|
|
157
161
|
let args = [];
|
|
158
162
|
if (options.resolveArgs) {
|
|
159
163
|
try {
|
|
160
|
-
args = await options.resolveArgs();
|
|
164
|
+
args = await ci.with('Arguments:resolve', () => options.resolveArgs());
|
|
161
165
|
}
|
|
162
166
|
catch (error) {
|
|
163
167
|
options.logErrors && logger.error(error);
|
|
@@ -166,7 +170,7 @@ function defineMoostEventHandler(options) {
|
|
|
166
170
|
}
|
|
167
171
|
}
|
|
168
172
|
if (interceptorHandler) {
|
|
169
|
-
response = await interceptorHandler.fireBefore(response);
|
|
173
|
+
response = await ci.with('Inteceptors:before', () => interceptorHandler.fireBefore(response));
|
|
170
174
|
if (response !== undefined) {
|
|
171
175
|
return endWithResponse();
|
|
172
176
|
}
|
|
@@ -182,7 +186,10 @@ function defineMoostEventHandler(options) {
|
|
|
182
186
|
}
|
|
183
187
|
};
|
|
184
188
|
try {
|
|
185
|
-
response =
|
|
189
|
+
response = await ci.with('Handler', {
|
|
190
|
+
'moost.handler': options.controllerMethod || '',
|
|
191
|
+
'moost.controller': getConstructor(instance).name,
|
|
192
|
+
}, () => callControllerMethod());
|
|
186
193
|
}
|
|
187
194
|
catch (error) {
|
|
188
195
|
options.logErrors && logger.error(error);
|
|
@@ -192,7 +199,7 @@ function defineMoostEventHandler(options) {
|
|
|
192
199
|
async function endWithResponse(raise = false) {
|
|
193
200
|
if (interceptorHandler) {
|
|
194
201
|
try {
|
|
195
|
-
response = await interceptorHandler.fireAfter(response);
|
|
202
|
+
response = await ci.with('Inteceptors:after', () => interceptorHandler.fireAfter(response));
|
|
196
203
|
}
|
|
197
204
|
catch (error) {
|
|
198
205
|
options.logErrors && logger.error(error);
|
|
@@ -286,28 +293,48 @@ class InterceptorHandler {
|
|
|
286
293
|
this.onError = [];
|
|
287
294
|
this.responseOverwritten = false;
|
|
288
295
|
}
|
|
296
|
+
get count() {
|
|
297
|
+
return this.handlers.length;
|
|
298
|
+
}
|
|
299
|
+
get countBefore() {
|
|
300
|
+
return this.before.length;
|
|
301
|
+
}
|
|
302
|
+
get countAfter() {
|
|
303
|
+
return this.after.length;
|
|
304
|
+
}
|
|
305
|
+
get countOnError() {
|
|
306
|
+
return this.onError.length;
|
|
307
|
+
}
|
|
289
308
|
replyFn(reply) {
|
|
290
309
|
this.response = reply;
|
|
291
310
|
this.responseOverwritten = true;
|
|
292
311
|
}
|
|
293
312
|
async init() {
|
|
313
|
+
const ci = getContextInjector();
|
|
294
314
|
for (const handler of this.handlers) {
|
|
295
|
-
const response = await handler
|
|
296
|
-
|
|
315
|
+
const response = await ci.with(`Inteceptor:${handler.name}`, {
|
|
316
|
+
'moost.interceptor.stage': 'init',
|
|
317
|
+
'moost.interceptor.priority': handler.priority || '',
|
|
318
|
+
}, () => handler(fn => {
|
|
319
|
+
this.before.push({ handler, fn });
|
|
297
320
|
}, fn => {
|
|
298
|
-
this.after.unshift(fn);
|
|
321
|
+
this.after.unshift({ handler, fn });
|
|
299
322
|
}, fn => {
|
|
300
|
-
this.onError.unshift(fn);
|
|
301
|
-
});
|
|
323
|
+
this.onError.unshift({ handler, fn });
|
|
324
|
+
}));
|
|
302
325
|
if (response !== undefined) {
|
|
303
326
|
return response;
|
|
304
327
|
}
|
|
305
328
|
}
|
|
306
329
|
}
|
|
307
330
|
async fireBefore(response) {
|
|
331
|
+
const ci = getContextInjector();
|
|
308
332
|
this.response = response;
|
|
309
|
-
for (const handler of this.before) {
|
|
310
|
-
await handler
|
|
333
|
+
for (const { handler, fn } of this.before) {
|
|
334
|
+
await ci.with(`Inteceptor:${handler.name}`, {
|
|
335
|
+
'moost.interceptor.stage': 'before',
|
|
336
|
+
'moost.interceptor.priority': handler.priority || '',
|
|
337
|
+
}, () => fn(this.replyFn.bind(this)));
|
|
311
338
|
if (this.responseOverwritten) {
|
|
312
339
|
break;
|
|
313
340
|
}
|
|
@@ -315,15 +342,22 @@ class InterceptorHandler {
|
|
|
315
342
|
return this.response;
|
|
316
343
|
}
|
|
317
344
|
async fireAfter(response) {
|
|
345
|
+
const ci = getContextInjector();
|
|
318
346
|
this.response = response;
|
|
319
347
|
if (response instanceof Error) {
|
|
320
|
-
for (const handler of this.onError) {
|
|
321
|
-
await handler
|
|
348
|
+
for (const { handler, fn } of this.onError) {
|
|
349
|
+
await ci.with(`Inteceptor:${handler.name}`, {
|
|
350
|
+
'moost.interceptor.stage': 'after',
|
|
351
|
+
'moost.interceptor.priority': handler.priority || '',
|
|
352
|
+
}, () => fn(response, this.replyFn.bind(this)));
|
|
322
353
|
}
|
|
323
354
|
}
|
|
324
355
|
else {
|
|
325
|
-
for (const handler of this.after) {
|
|
326
|
-
await handler
|
|
356
|
+
for (const { handler, fn } of this.after) {
|
|
357
|
+
await ci.with(`Inteceptor:${handler.name}`, {
|
|
358
|
+
'moost.interceptor.stage': 'onError',
|
|
359
|
+
'moost.interceptor.priority': handler.priority || '',
|
|
360
|
+
}, () => fn(response, this.replyFn.bind(this)));
|
|
327
361
|
}
|
|
328
362
|
}
|
|
329
363
|
return this.response;
|
|
@@ -708,7 +742,7 @@ class Moost extends Hookable {
|
|
|
708
742
|
event: { type: 'init' },
|
|
709
743
|
options: {},
|
|
710
744
|
})(async () => {
|
|
711
|
-
setControllerContext(this, 'bindController');
|
|
745
|
+
setControllerContext(this, 'bindController', '');
|
|
712
746
|
instance = (await infact.get(controller, infactOpts));
|
|
713
747
|
});
|
|
714
748
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "moost",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.8",
|
|
4
4
|
"description": "moost",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -34,9 +34,9 @@
|
|
|
34
34
|
"@prostojs/infact": "^0.2.3",
|
|
35
35
|
"@prostojs/mate": "^0.3.2",
|
|
36
36
|
"@prostojs/logger": "^0.4.0",
|
|
37
|
-
"@wooksjs/event-core": "^0.5.
|
|
37
|
+
"@wooksjs/event-core": "^0.5.9",
|
|
38
38
|
"hookable": "^5.5.3",
|
|
39
|
-
"wooks": "^0.5.
|
|
39
|
+
"wooks": "^0.5.9"
|
|
40
40
|
},
|
|
41
41
|
"author": "Artem Maltsev",
|
|
42
42
|
"license": "MIT",
|