moost 0.4.11 → 0.4.13
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 +31 -26
- package/dist/index.d.ts +16 -7
- package/dist/index.mjs +31 -26
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -142,12 +142,12 @@ function defineMoostEventHandler(options) {
|
|
|
142
142
|
const instance = await options.getControllerInstance();
|
|
143
143
|
if (instance) {
|
|
144
144
|
setControllerContext(instance, options.controllerMethod || '', options.targetPath);
|
|
145
|
-
ci.hook('Controller:registered');
|
|
145
|
+
ci.hook(options.handlerType, 'Controller:registered');
|
|
146
146
|
}
|
|
147
147
|
const interceptorHandler = await options.getIterceptorHandler();
|
|
148
148
|
if (interceptorHandler?.count) {
|
|
149
149
|
try {
|
|
150
|
-
response = await ci.with('
|
|
150
|
+
response = await ci.with('Interceptors:init', () => interceptorHandler.init());
|
|
151
151
|
if (response !== undefined) {
|
|
152
152
|
return await endWithResponse();
|
|
153
153
|
}
|
|
@@ -170,7 +170,7 @@ function defineMoostEventHandler(options) {
|
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
if (interceptorHandler?.countBefore) {
|
|
173
|
-
response = await ci.with('
|
|
173
|
+
response = await ci.with('Interceptors:before', () => interceptorHandler.fireBefore(response));
|
|
174
174
|
if (response !== undefined) {
|
|
175
175
|
return endWithResponse();
|
|
176
176
|
}
|
|
@@ -186,7 +186,7 @@ function defineMoostEventHandler(options) {
|
|
|
186
186
|
}
|
|
187
187
|
};
|
|
188
188
|
try {
|
|
189
|
-
response = await ci.with(
|
|
189
|
+
response = await ci.with(`Handler:${options.targetPath}`, {
|
|
190
190
|
'moost.handler': options.controllerMethod || '',
|
|
191
191
|
'moost.controller': mate$1.getConstructor(instance).name,
|
|
192
192
|
}, () => callControllerMethod());
|
|
@@ -199,7 +199,7 @@ function defineMoostEventHandler(options) {
|
|
|
199
199
|
async function endWithResponse(raise = false) {
|
|
200
200
|
if (interceptorHandler?.countAfter || interceptorHandler?.countOnError) {
|
|
201
201
|
try {
|
|
202
|
-
response = await ci.with('
|
|
202
|
+
response = await ci.with('Interceptors:after', () => interceptorHandler.fireAfter(response));
|
|
203
203
|
}
|
|
204
204
|
catch (error) {
|
|
205
205
|
options.logErrors && logger.error(error);
|
|
@@ -311,16 +311,15 @@ class InterceptorHandler {
|
|
|
311
311
|
}
|
|
312
312
|
async init() {
|
|
313
313
|
const ci = eventCore.getContextInjector();
|
|
314
|
-
for (const handler of this.handlers) {
|
|
315
|
-
const response = await ci.with(`
|
|
314
|
+
for (const { handler, name } of this.handlers) {
|
|
315
|
+
const response = await ci.with(`Interceptor:${name}`, {
|
|
316
316
|
'moost.interceptor.stage': 'init',
|
|
317
|
-
'moost.interceptor.priority': handler.priority || '',
|
|
318
317
|
}, () => handler(fn => {
|
|
319
|
-
this.before.push({
|
|
318
|
+
this.before.push({ name, fn });
|
|
320
319
|
}, fn => {
|
|
321
|
-
this.after.unshift({
|
|
320
|
+
this.after.unshift({ name, fn });
|
|
322
321
|
}, fn => {
|
|
323
|
-
this.onError.unshift({
|
|
322
|
+
this.onError.unshift({ name, fn });
|
|
324
323
|
}));
|
|
325
324
|
if (response !== undefined) {
|
|
326
325
|
return response;
|
|
@@ -330,10 +329,9 @@ class InterceptorHandler {
|
|
|
330
329
|
async fireBefore(response) {
|
|
331
330
|
const ci = eventCore.getContextInjector();
|
|
332
331
|
this.response = response;
|
|
333
|
-
for (const {
|
|
334
|
-
await ci.with(`
|
|
332
|
+
for (const { name, fn } of this.before) {
|
|
333
|
+
await ci.with(`Interceptor:${name}`, {
|
|
335
334
|
'moost.interceptor.stage': 'before',
|
|
336
|
-
'moost.interceptor.priority': handler.priority || '',
|
|
337
335
|
}, () => fn(this.replyFn.bind(this)));
|
|
338
336
|
if (this.responseOverwritten) {
|
|
339
337
|
break;
|
|
@@ -345,18 +343,16 @@ class InterceptorHandler {
|
|
|
345
343
|
const ci = eventCore.getContextInjector();
|
|
346
344
|
this.response = response;
|
|
347
345
|
if (response instanceof Error) {
|
|
348
|
-
for (const {
|
|
349
|
-
await ci.with(`
|
|
346
|
+
for (const { name, fn } of this.onError) {
|
|
347
|
+
await ci.with(`Interceptor:${name}`, {
|
|
350
348
|
'moost.interceptor.stage': 'after',
|
|
351
|
-
'moost.interceptor.priority': handler.priority || '',
|
|
352
349
|
}, () => fn(response, this.replyFn.bind(this)));
|
|
353
350
|
}
|
|
354
351
|
}
|
|
355
352
|
else {
|
|
356
|
-
for (const {
|
|
357
|
-
await ci.with(`
|
|
353
|
+
for (const { name, fn } of this.after) {
|
|
354
|
+
await ci.with(`Interceptor:${name}`, {
|
|
358
355
|
'moost.interceptor.stage': 'onError',
|
|
359
|
-
'moost.interceptor.priority': handler.priority || '',
|
|
360
356
|
}, () => fn(response, this.replyFn.bind(this)));
|
|
361
357
|
}
|
|
362
358
|
}
|
|
@@ -368,16 +364,19 @@ const mate = getMoostMate();
|
|
|
368
364
|
function getIterceptorHandlerFactory(interceptors, getTargetInstance, pipes, logger) {
|
|
369
365
|
return () => {
|
|
370
366
|
const interceptorHandlers = [];
|
|
371
|
-
for (const { handler } of interceptors) {
|
|
367
|
+
for (const { handler, name } of interceptors) {
|
|
372
368
|
const interceptorMeta = mate.read(handler);
|
|
373
369
|
if (interceptorMeta?.injectable) {
|
|
374
|
-
interceptorHandlers.push(
|
|
375
|
-
|
|
376
|
-
|
|
370
|
+
interceptorHandlers.push({
|
|
371
|
+
handler: async (...args) => {
|
|
372
|
+
const targetInstance = await getTargetInstance();
|
|
373
|
+
return (await getCallableFn(targetInstance, handler, pipes, logger))(...args);
|
|
374
|
+
},
|
|
375
|
+
name,
|
|
377
376
|
});
|
|
378
377
|
}
|
|
379
378
|
else {
|
|
380
|
-
interceptorHandlers.push(handler);
|
|
379
|
+
interceptorHandlers.push({ handler: handler, name });
|
|
381
380
|
}
|
|
382
381
|
}
|
|
383
382
|
return Promise.resolve(new InterceptorHandler(interceptorHandlers));
|
|
@@ -547,10 +546,11 @@ exports.TInterceptorPriority = void 0;
|
|
|
547
546
|
TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
|
|
548
547
|
TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
|
|
549
548
|
})(exports.TInterceptorPriority || (exports.TInterceptorPriority = {}));
|
|
550
|
-
function Intercept(handler, priority) {
|
|
549
|
+
function Intercept(handler, priority, name) {
|
|
551
550
|
return getMoostMate().decorate('interceptors', {
|
|
552
551
|
handler,
|
|
553
552
|
priority: priority || handler.priority || exports.TInterceptorPriority.INTERCEPTOR,
|
|
553
|
+
name: name || handler._name || handler.name,
|
|
554
554
|
}, true);
|
|
555
555
|
}
|
|
556
556
|
|
|
@@ -828,12 +828,17 @@ class Moost extends hookable.Hookable {
|
|
|
828
828
|
priority: typeof item.priority === 'number'
|
|
829
829
|
? item.priority
|
|
830
830
|
: exports.TInterceptorPriority.INTERCEPTOR,
|
|
831
|
+
name: item._name || item.name || '<anonymous>',
|
|
831
832
|
});
|
|
832
833
|
}
|
|
833
834
|
else {
|
|
834
835
|
this.interceptors.push({
|
|
835
836
|
handler: item.handler,
|
|
836
837
|
priority: item.priority,
|
|
838
|
+
name: item.name ||
|
|
839
|
+
item.handler._name ||
|
|
840
|
+
item.handler.name ||
|
|
841
|
+
'<anonymous>',
|
|
837
842
|
});
|
|
838
843
|
}
|
|
839
844
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -112,6 +112,7 @@ type TInterceptorOnError = (error: Error, reply: (response: TAny) => void) => vo
|
|
|
112
112
|
interface TInterceptorFn {
|
|
113
113
|
(before: (fn: TInterceptorBefore) => void, after: (fn: TInterceptorAfter) => void, onError: (fn: TInterceptorOnError) => void): unknown | Promise<unknown>;
|
|
114
114
|
priority?: TInterceptorPriority;
|
|
115
|
+
_name?: string;
|
|
115
116
|
}
|
|
116
117
|
declare enum TInterceptorPriority {
|
|
117
118
|
BEFORE_ALL = 0,
|
|
@@ -130,7 +131,7 @@ declare enum TInterceptorPriority {
|
|
|
130
131
|
* @param priority interceptor priority
|
|
131
132
|
* @returns
|
|
132
133
|
*/
|
|
133
|
-
declare function Intercept(handler: TCallableClassFunction<TInterceptorFn>, priority?: TInterceptorPriority): ClassDecorator & MethodDecorator;
|
|
134
|
+
declare function Intercept(handler: TCallableClassFunction<TInterceptorFn>, priority?: TInterceptorPriority, name?: string): ClassDecorator & MethodDecorator;
|
|
134
135
|
|
|
135
136
|
type TDecoratorLevel = 'CLASS' | 'METHOD' | 'PROP' | 'PARAM';
|
|
136
137
|
|
|
@@ -207,6 +208,7 @@ type TMoostHandler<T> = {
|
|
|
207
208
|
interface TInterceptorData {
|
|
208
209
|
handler: TCallableClassFunction<TInterceptorFn>;
|
|
209
210
|
priority: TInterceptorPriority;
|
|
211
|
+
name: string;
|
|
210
212
|
}
|
|
211
213
|
declare function getMoostMate<Class extends TObject = TEmpty, Prop extends TObject = TEmpty, Param extends TObject = TEmpty>(): Mate<TMoostMetadata<TEmpty> & Class & {
|
|
212
214
|
params: Array<Param & TMateParamMeta>;
|
|
@@ -319,18 +321,24 @@ declare function ConstFactory<T>(factory: () => T | Promise<T>, label?: string):
|
|
|
319
321
|
declare function InjectEventLogger(topic?: string): ParameterDecorator & PropertyDecorator;
|
|
320
322
|
|
|
321
323
|
declare class InterceptorHandler {
|
|
322
|
-
protected handlers:
|
|
323
|
-
|
|
324
|
-
|
|
324
|
+
protected handlers: Array<{
|
|
325
|
+
handler: TInterceptorFn;
|
|
326
|
+
name: string;
|
|
327
|
+
}>;
|
|
328
|
+
constructor(handlers: Array<{
|
|
325
329
|
handler: TInterceptorFn;
|
|
330
|
+
name: string;
|
|
331
|
+
}>);
|
|
332
|
+
protected before: Array<{
|
|
333
|
+
name: string;
|
|
326
334
|
fn: TInterceptorBefore;
|
|
327
335
|
}>;
|
|
328
336
|
protected after: Array<{
|
|
329
|
-
|
|
337
|
+
name: string;
|
|
330
338
|
fn: TInterceptorAfter;
|
|
331
339
|
}>;
|
|
332
340
|
protected onError: Array<{
|
|
333
|
-
|
|
341
|
+
name: string;
|
|
334
342
|
fn: TInterceptorOnError;
|
|
335
343
|
}>;
|
|
336
344
|
response?: unknown;
|
|
@@ -369,6 +377,7 @@ interface TMoostEventHandlerOptions<T> {
|
|
|
369
377
|
end?: (opts: TMoostEventHandlerHookOptions<T>) => unknown;
|
|
370
378
|
};
|
|
371
379
|
targetPath: string;
|
|
380
|
+
handlerType: string;
|
|
372
381
|
}
|
|
373
382
|
declare function registerEventScope(scopeId: string): () => void;
|
|
374
383
|
declare function defineMoostEventHandler<T>(options: TMoostEventHandlerOptions<T>): () => Promise<unknown>;
|
|
@@ -393,7 +402,7 @@ interface THandlerOverview {
|
|
|
393
402
|
args: string[];
|
|
394
403
|
}>;
|
|
395
404
|
}
|
|
396
|
-
type TContextInjectorHook = 'Event:start' | '
|
|
405
|
+
type TContextInjectorHook = 'Event:start' | 'Interceptors:init' | 'Arguments:resolve' | 'Interceptors:before' | 'Handler' | 'Interceptors:after';
|
|
397
406
|
|
|
398
407
|
interface TMoostOptions {
|
|
399
408
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -143,12 +143,12 @@ function defineMoostEventHandler(options) {
|
|
|
143
143
|
const instance = await options.getControllerInstance();
|
|
144
144
|
if (instance) {
|
|
145
145
|
setControllerContext(instance, options.controllerMethod || '', options.targetPath);
|
|
146
|
-
ci.hook('Controller:registered');
|
|
146
|
+
ci.hook(options.handlerType, 'Controller:registered');
|
|
147
147
|
}
|
|
148
148
|
const interceptorHandler = await options.getIterceptorHandler();
|
|
149
149
|
if (interceptorHandler?.count) {
|
|
150
150
|
try {
|
|
151
|
-
response = await ci.with('
|
|
151
|
+
response = await ci.with('Interceptors:init', () => interceptorHandler.init());
|
|
152
152
|
if (response !== undefined) {
|
|
153
153
|
return await endWithResponse();
|
|
154
154
|
}
|
|
@@ -171,7 +171,7 @@ function defineMoostEventHandler(options) {
|
|
|
171
171
|
}
|
|
172
172
|
}
|
|
173
173
|
if (interceptorHandler?.countBefore) {
|
|
174
|
-
response = await ci.with('
|
|
174
|
+
response = await ci.with('Interceptors:before', () => interceptorHandler.fireBefore(response));
|
|
175
175
|
if (response !== undefined) {
|
|
176
176
|
return endWithResponse();
|
|
177
177
|
}
|
|
@@ -187,7 +187,7 @@ function defineMoostEventHandler(options) {
|
|
|
187
187
|
}
|
|
188
188
|
};
|
|
189
189
|
try {
|
|
190
|
-
response = await ci.with(
|
|
190
|
+
response = await ci.with(`Handler:${options.targetPath}`, {
|
|
191
191
|
'moost.handler': options.controllerMethod || '',
|
|
192
192
|
'moost.controller': getConstructor(instance).name,
|
|
193
193
|
}, () => callControllerMethod());
|
|
@@ -200,7 +200,7 @@ function defineMoostEventHandler(options) {
|
|
|
200
200
|
async function endWithResponse(raise = false) {
|
|
201
201
|
if (interceptorHandler?.countAfter || interceptorHandler?.countOnError) {
|
|
202
202
|
try {
|
|
203
|
-
response = await ci.with('
|
|
203
|
+
response = await ci.with('Interceptors:after', () => interceptorHandler.fireAfter(response));
|
|
204
204
|
}
|
|
205
205
|
catch (error) {
|
|
206
206
|
options.logErrors && logger.error(error);
|
|
@@ -312,16 +312,15 @@ class InterceptorHandler {
|
|
|
312
312
|
}
|
|
313
313
|
async init() {
|
|
314
314
|
const ci = getContextInjector();
|
|
315
|
-
for (const handler of this.handlers) {
|
|
316
|
-
const response = await ci.with(`
|
|
315
|
+
for (const { handler, name } of this.handlers) {
|
|
316
|
+
const response = await ci.with(`Interceptor:${name}`, {
|
|
317
317
|
'moost.interceptor.stage': 'init',
|
|
318
|
-
'moost.interceptor.priority': handler.priority || '',
|
|
319
318
|
}, () => handler(fn => {
|
|
320
|
-
this.before.push({
|
|
319
|
+
this.before.push({ name, fn });
|
|
321
320
|
}, fn => {
|
|
322
|
-
this.after.unshift({
|
|
321
|
+
this.after.unshift({ name, fn });
|
|
323
322
|
}, fn => {
|
|
324
|
-
this.onError.unshift({
|
|
323
|
+
this.onError.unshift({ name, fn });
|
|
325
324
|
}));
|
|
326
325
|
if (response !== undefined) {
|
|
327
326
|
return response;
|
|
@@ -331,10 +330,9 @@ class InterceptorHandler {
|
|
|
331
330
|
async fireBefore(response) {
|
|
332
331
|
const ci = getContextInjector();
|
|
333
332
|
this.response = response;
|
|
334
|
-
for (const {
|
|
335
|
-
await ci.with(`
|
|
333
|
+
for (const { name, fn } of this.before) {
|
|
334
|
+
await ci.with(`Interceptor:${name}`, {
|
|
336
335
|
'moost.interceptor.stage': 'before',
|
|
337
|
-
'moost.interceptor.priority': handler.priority || '',
|
|
338
336
|
}, () => fn(this.replyFn.bind(this)));
|
|
339
337
|
if (this.responseOverwritten) {
|
|
340
338
|
break;
|
|
@@ -346,18 +344,16 @@ class InterceptorHandler {
|
|
|
346
344
|
const ci = getContextInjector();
|
|
347
345
|
this.response = response;
|
|
348
346
|
if (response instanceof Error) {
|
|
349
|
-
for (const {
|
|
350
|
-
await ci.with(`
|
|
347
|
+
for (const { name, fn } of this.onError) {
|
|
348
|
+
await ci.with(`Interceptor:${name}`, {
|
|
351
349
|
'moost.interceptor.stage': 'after',
|
|
352
|
-
'moost.interceptor.priority': handler.priority || '',
|
|
353
350
|
}, () => fn(response, this.replyFn.bind(this)));
|
|
354
351
|
}
|
|
355
352
|
}
|
|
356
353
|
else {
|
|
357
|
-
for (const {
|
|
358
|
-
await ci.with(`
|
|
354
|
+
for (const { name, fn } of this.after) {
|
|
355
|
+
await ci.with(`Interceptor:${name}`, {
|
|
359
356
|
'moost.interceptor.stage': 'onError',
|
|
360
|
-
'moost.interceptor.priority': handler.priority || '',
|
|
361
357
|
}, () => fn(response, this.replyFn.bind(this)));
|
|
362
358
|
}
|
|
363
359
|
}
|
|
@@ -369,16 +365,19 @@ const mate = getMoostMate();
|
|
|
369
365
|
function getIterceptorHandlerFactory(interceptors, getTargetInstance, pipes, logger) {
|
|
370
366
|
return () => {
|
|
371
367
|
const interceptorHandlers = [];
|
|
372
|
-
for (const { handler } of interceptors) {
|
|
368
|
+
for (const { handler, name } of interceptors) {
|
|
373
369
|
const interceptorMeta = mate.read(handler);
|
|
374
370
|
if (interceptorMeta?.injectable) {
|
|
375
|
-
interceptorHandlers.push(
|
|
376
|
-
|
|
377
|
-
|
|
371
|
+
interceptorHandlers.push({
|
|
372
|
+
handler: async (...args) => {
|
|
373
|
+
const targetInstance = await getTargetInstance();
|
|
374
|
+
return (await getCallableFn(targetInstance, handler, pipes, logger))(...args);
|
|
375
|
+
},
|
|
376
|
+
name,
|
|
378
377
|
});
|
|
379
378
|
}
|
|
380
379
|
else {
|
|
381
|
-
interceptorHandlers.push(handler);
|
|
380
|
+
interceptorHandlers.push({ handler: handler, name });
|
|
382
381
|
}
|
|
383
382
|
}
|
|
384
383
|
return Promise.resolve(new InterceptorHandler(interceptorHandlers));
|
|
@@ -548,10 +547,11 @@ var TInterceptorPriority;
|
|
|
548
547
|
TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
|
|
549
548
|
TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
|
|
550
549
|
})(TInterceptorPriority || (TInterceptorPriority = {}));
|
|
551
|
-
function Intercept(handler, priority) {
|
|
550
|
+
function Intercept(handler, priority, name) {
|
|
552
551
|
return getMoostMate().decorate('interceptors', {
|
|
553
552
|
handler,
|
|
554
553
|
priority: priority || handler.priority || TInterceptorPriority.INTERCEPTOR,
|
|
554
|
+
name: name || handler._name || handler.name,
|
|
555
555
|
}, true);
|
|
556
556
|
}
|
|
557
557
|
|
|
@@ -829,12 +829,17 @@ class Moost extends Hookable {
|
|
|
829
829
|
priority: typeof item.priority === 'number'
|
|
830
830
|
? item.priority
|
|
831
831
|
: TInterceptorPriority.INTERCEPTOR,
|
|
832
|
+
name: item._name || item.name || '<anonymous>',
|
|
832
833
|
});
|
|
833
834
|
}
|
|
834
835
|
else {
|
|
835
836
|
this.interceptors.push({
|
|
836
837
|
handler: item.handler,
|
|
837
838
|
priority: item.priority,
|
|
839
|
+
name: item.name ||
|
|
840
|
+
item.handler._name ||
|
|
841
|
+
item.handler.name ||
|
|
842
|
+
'<anonymous>',
|
|
838
843
|
});
|
|
839
844
|
}
|
|
840
845
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "moost",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.13",
|
|
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.11",
|
|
38
38
|
"hookable": "^5.5.3",
|
|
39
|
-
"wooks": "^0.5.
|
|
39
|
+
"wooks": "^0.5.11"
|
|
40
40
|
},
|
|
41
41
|
"author": "Artem Maltsev",
|
|
42
42
|
"license": "MIT",
|