moost 0.4.10 → 0.4.12

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 CHANGED
@@ -142,11 +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
146
  }
146
147
  const interceptorHandler = await options.getIterceptorHandler();
147
148
  if (interceptorHandler?.count) {
148
149
  try {
149
- response = await ci.with('Inteceptors:init', () => interceptorHandler.init());
150
+ response = await ci.with('Interceptors:init', () => interceptorHandler.init());
150
151
  if (response !== undefined) {
151
152
  return await endWithResponse();
152
153
  }
@@ -169,7 +170,7 @@ function defineMoostEventHandler(options) {
169
170
  }
170
171
  }
171
172
  if (interceptorHandler?.countBefore) {
172
- response = await ci.with('Inteceptors:before', () => interceptorHandler.fireBefore(response));
173
+ response = await ci.with('Interceptors:before', () => interceptorHandler.fireBefore(response));
173
174
  if (response !== undefined) {
174
175
  return endWithResponse();
175
176
  }
@@ -198,7 +199,7 @@ function defineMoostEventHandler(options) {
198
199
  async function endWithResponse(raise = false) {
199
200
  if (interceptorHandler?.countAfter || interceptorHandler?.countOnError) {
200
201
  try {
201
- response = await ci.with('Inteceptors:after', () => interceptorHandler.fireAfter(response));
202
+ response = await ci.with('Interceptors:after', () => interceptorHandler.fireAfter(response));
202
203
  }
203
204
  catch (error) {
204
205
  options.logErrors && logger.error(error);
@@ -310,16 +311,15 @@ class InterceptorHandler {
310
311
  }
311
312
  async init() {
312
313
  const ci = eventCore.getContextInjector();
313
- for (const handler of this.handlers) {
314
- const response = await ci.with(`Inteceptor:${handler.name}`, {
314
+ for (const { handler, name } of this.handlers) {
315
+ const response = await ci.with(`Interceptor:${name}`, {
315
316
  'moost.interceptor.stage': 'init',
316
- 'moost.interceptor.priority': handler.priority || '',
317
317
  }, () => handler(fn => {
318
- this.before.push({ handler, fn });
318
+ this.before.push({ name, fn });
319
319
  }, fn => {
320
- this.after.unshift({ handler, fn });
320
+ this.after.unshift({ name, fn });
321
321
  }, fn => {
322
- this.onError.unshift({ handler, fn });
322
+ this.onError.unshift({ name, fn });
323
323
  }));
324
324
  if (response !== undefined) {
325
325
  return response;
@@ -329,10 +329,9 @@ class InterceptorHandler {
329
329
  async fireBefore(response) {
330
330
  const ci = eventCore.getContextInjector();
331
331
  this.response = response;
332
- for (const { handler, fn } of this.before) {
333
- await ci.with(`Inteceptor:${handler.name}`, {
332
+ for (const { name, fn } of this.before) {
333
+ await ci.with(`Interceptor:${name}`, {
334
334
  'moost.interceptor.stage': 'before',
335
- 'moost.interceptor.priority': handler.priority || '',
336
335
  }, () => fn(this.replyFn.bind(this)));
337
336
  if (this.responseOverwritten) {
338
337
  break;
@@ -344,18 +343,16 @@ class InterceptorHandler {
344
343
  const ci = eventCore.getContextInjector();
345
344
  this.response = response;
346
345
  if (response instanceof Error) {
347
- for (const { handler, fn } of this.onError) {
348
- await ci.with(`Inteceptor:${handler.name}`, {
346
+ for (const { name, fn } of this.onError) {
347
+ await ci.with(`Interceptor:${name}`, {
349
348
  'moost.interceptor.stage': 'after',
350
- 'moost.interceptor.priority': handler.priority || '',
351
349
  }, () => fn(response, this.replyFn.bind(this)));
352
350
  }
353
351
  }
354
352
  else {
355
- for (const { handler, fn } of this.after) {
356
- await ci.with(`Inteceptor:${handler.name}`, {
353
+ for (const { name, fn } of this.after) {
354
+ await ci.with(`Interceptor:${name}`, {
357
355
  'moost.interceptor.stage': 'onError',
358
- 'moost.interceptor.priority': handler.priority || '',
359
356
  }, () => fn(response, this.replyFn.bind(this)));
360
357
  }
361
358
  }
@@ -367,16 +364,19 @@ const mate = getMoostMate();
367
364
  function getIterceptorHandlerFactory(interceptors, getTargetInstance, pipes, logger) {
368
365
  return () => {
369
366
  const interceptorHandlers = [];
370
- for (const { handler } of interceptors) {
367
+ for (const { handler, name } of interceptors) {
371
368
  const interceptorMeta = mate.read(handler);
372
369
  if (interceptorMeta?.injectable) {
373
- interceptorHandlers.push(async (...args) => {
374
- const targetInstance = await getTargetInstance();
375
- return (await getCallableFn(targetInstance, handler, pipes, logger))(...args);
370
+ interceptorHandlers.push({
371
+ handler: async (...args) => {
372
+ const targetInstance = await getTargetInstance();
373
+ return (await getCallableFn(targetInstance, handler, pipes, logger))(...args);
374
+ },
375
+ name,
376
376
  });
377
377
  }
378
378
  else {
379
- interceptorHandlers.push(handler);
379
+ interceptorHandlers.push({ handler: handler, name });
380
380
  }
381
381
  }
382
382
  return Promise.resolve(new InterceptorHandler(interceptorHandlers));
@@ -546,10 +546,11 @@ exports.TInterceptorPriority = void 0;
546
546
  TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
547
547
  TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
548
548
  })(exports.TInterceptorPriority || (exports.TInterceptorPriority = {}));
549
- function Intercept(handler, priority) {
549
+ function Intercept(handler, priority, name) {
550
550
  return getMoostMate().decorate('interceptors', {
551
551
  handler,
552
552
  priority: priority || handler.priority || exports.TInterceptorPriority.INTERCEPTOR,
553
+ name: name || handler._name || handler.name,
553
554
  }, true);
554
555
  }
555
556
 
@@ -827,12 +828,17 @@ class Moost extends hookable.Hookable {
827
828
  priority: typeof item.priority === 'number'
828
829
  ? item.priority
829
830
  : exports.TInterceptorPriority.INTERCEPTOR,
831
+ name: item._name || item.name || '<anonymous>',
830
832
  });
831
833
  }
832
834
  else {
833
835
  this.interceptors.push({
834
836
  handler: item.handler,
835
837
  priority: item.priority,
838
+ name: item.name ||
839
+ item.handler._name ||
840
+ item.handler.name ||
841
+ '<anonymous>',
836
842
  });
837
843
  }
838
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: TInterceptorFn[];
323
- constructor(handlers: TInterceptorFn[]);
324
- protected before: Array<{
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
- handler: TInterceptorFn;
337
+ name: string;
330
338
  fn: TInterceptorAfter;
331
339
  }>;
332
340
  protected onError: Array<{
333
- handler: TInterceptorFn;
341
+ name: string;
334
342
  fn: TInterceptorOnError;
335
343
  }>;
336
344
  response?: unknown;
@@ -393,7 +401,7 @@ interface THandlerOverview {
393
401
  args: string[];
394
402
  }>;
395
403
  }
396
- type TContextInjectorHook = 'Event:start' | 'Inteceptors:init' | 'Arguments:resolve' | 'Inteceptors:before' | 'Handler' | 'Inteceptors:after';
404
+ type TContextInjectorHook = 'Event:start' | 'Interceptors:init' | 'Arguments:resolve' | 'Interceptors:before' | 'Handler' | 'Interceptors:after';
397
405
 
398
406
  interface TMoostOptions {
399
407
  /**
package/dist/index.mjs CHANGED
@@ -143,11 +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
147
  }
147
148
  const interceptorHandler = await options.getIterceptorHandler();
148
149
  if (interceptorHandler?.count) {
149
150
  try {
150
- response = await ci.with('Inteceptors:init', () => interceptorHandler.init());
151
+ response = await ci.with('Interceptors:init', () => interceptorHandler.init());
151
152
  if (response !== undefined) {
152
153
  return await endWithResponse();
153
154
  }
@@ -170,7 +171,7 @@ function defineMoostEventHandler(options) {
170
171
  }
171
172
  }
172
173
  if (interceptorHandler?.countBefore) {
173
- response = await ci.with('Inteceptors:before', () => interceptorHandler.fireBefore(response));
174
+ response = await ci.with('Interceptors:before', () => interceptorHandler.fireBefore(response));
174
175
  if (response !== undefined) {
175
176
  return endWithResponse();
176
177
  }
@@ -199,7 +200,7 @@ function defineMoostEventHandler(options) {
199
200
  async function endWithResponse(raise = false) {
200
201
  if (interceptorHandler?.countAfter || interceptorHandler?.countOnError) {
201
202
  try {
202
- response = await ci.with('Inteceptors:after', () => interceptorHandler.fireAfter(response));
203
+ response = await ci.with('Interceptors:after', () => interceptorHandler.fireAfter(response));
203
204
  }
204
205
  catch (error) {
205
206
  options.logErrors && logger.error(error);
@@ -311,16 +312,15 @@ class InterceptorHandler {
311
312
  }
312
313
  async init() {
313
314
  const ci = getContextInjector();
314
- for (const handler of this.handlers) {
315
- const response = await ci.with(`Inteceptor:${handler.name}`, {
315
+ for (const { handler, name } of this.handlers) {
316
+ const response = await ci.with(`Interceptor:${name}`, {
316
317
  'moost.interceptor.stage': 'init',
317
- 'moost.interceptor.priority': handler.priority || '',
318
318
  }, () => handler(fn => {
319
- this.before.push({ handler, fn });
319
+ this.before.push({ name, fn });
320
320
  }, fn => {
321
- this.after.unshift({ handler, fn });
321
+ this.after.unshift({ name, fn });
322
322
  }, fn => {
323
- this.onError.unshift({ handler, fn });
323
+ this.onError.unshift({ name, fn });
324
324
  }));
325
325
  if (response !== undefined) {
326
326
  return response;
@@ -330,10 +330,9 @@ class InterceptorHandler {
330
330
  async fireBefore(response) {
331
331
  const ci = getContextInjector();
332
332
  this.response = response;
333
- for (const { handler, fn } of this.before) {
334
- await ci.with(`Inteceptor:${handler.name}`, {
333
+ for (const { name, fn } of this.before) {
334
+ await ci.with(`Interceptor:${name}`, {
335
335
  'moost.interceptor.stage': 'before',
336
- 'moost.interceptor.priority': handler.priority || '',
337
336
  }, () => fn(this.replyFn.bind(this)));
338
337
  if (this.responseOverwritten) {
339
338
  break;
@@ -345,18 +344,16 @@ class InterceptorHandler {
345
344
  const ci = getContextInjector();
346
345
  this.response = response;
347
346
  if (response instanceof Error) {
348
- for (const { handler, fn } of this.onError) {
349
- await ci.with(`Inteceptor:${handler.name}`, {
347
+ for (const { name, fn } of this.onError) {
348
+ await ci.with(`Interceptor:${name}`, {
350
349
  'moost.interceptor.stage': 'after',
351
- 'moost.interceptor.priority': handler.priority || '',
352
350
  }, () => fn(response, this.replyFn.bind(this)));
353
351
  }
354
352
  }
355
353
  else {
356
- for (const { handler, fn } of this.after) {
357
- await ci.with(`Inteceptor:${handler.name}`, {
354
+ for (const { name, fn } of this.after) {
355
+ await ci.with(`Interceptor:${name}`, {
358
356
  'moost.interceptor.stage': 'onError',
359
- 'moost.interceptor.priority': handler.priority || '',
360
357
  }, () => fn(response, this.replyFn.bind(this)));
361
358
  }
362
359
  }
@@ -368,16 +365,19 @@ const mate = getMoostMate();
368
365
  function getIterceptorHandlerFactory(interceptors, getTargetInstance, pipes, logger) {
369
366
  return () => {
370
367
  const interceptorHandlers = [];
371
- for (const { handler } of interceptors) {
368
+ for (const { handler, name } of interceptors) {
372
369
  const interceptorMeta = mate.read(handler);
373
370
  if (interceptorMeta?.injectable) {
374
- interceptorHandlers.push(async (...args) => {
375
- const targetInstance = await getTargetInstance();
376
- return (await getCallableFn(targetInstance, handler, pipes, logger))(...args);
371
+ interceptorHandlers.push({
372
+ handler: async (...args) => {
373
+ const targetInstance = await getTargetInstance();
374
+ return (await getCallableFn(targetInstance, handler, pipes, logger))(...args);
375
+ },
376
+ name,
377
377
  });
378
378
  }
379
379
  else {
380
- interceptorHandlers.push(handler);
380
+ interceptorHandlers.push({ handler: handler, name });
381
381
  }
382
382
  }
383
383
  return Promise.resolve(new InterceptorHandler(interceptorHandlers));
@@ -547,10 +547,11 @@ var TInterceptorPriority;
547
547
  TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
548
548
  TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
549
549
  })(TInterceptorPriority || (TInterceptorPriority = {}));
550
- function Intercept(handler, priority) {
550
+ function Intercept(handler, priority, name) {
551
551
  return getMoostMate().decorate('interceptors', {
552
552
  handler,
553
553
  priority: priority || handler.priority || TInterceptorPriority.INTERCEPTOR,
554
+ name: name || handler._name || handler.name,
554
555
  }, true);
555
556
  }
556
557
 
@@ -828,12 +829,17 @@ class Moost extends Hookable {
828
829
  priority: typeof item.priority === 'number'
829
830
  ? item.priority
830
831
  : TInterceptorPriority.INTERCEPTOR,
832
+ name: item._name || item.name || '<anonymous>',
831
833
  });
832
834
  }
833
835
  else {
834
836
  this.interceptors.push({
835
837
  handler: item.handler,
836
838
  priority: item.priority,
839
+ name: item.name ||
840
+ item.handler._name ||
841
+ item.handler.name ||
842
+ '<anonymous>',
837
843
  });
838
844
  }
839
845
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moost",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
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.9",
37
+ "@wooksjs/event-core": "^0.5.10",
38
38
  "hookable": "^5.5.3",
39
- "wooks": "^0.5.9"
39
+ "wooks": "^0.5.10"
40
40
  },
41
41
  "author": "Artem Maltsev",
42
42
  "license": "MIT",