moost 0.3.11 → 0.3.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
@@ -86,10 +86,9 @@ function getNewMoostInfact() {
86
86
  }
87
87
 
88
88
  function getInstanceOwnMethods(instance) {
89
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
90
89
  const proto = Object.getPrototypeOf(instance);
91
90
  return [
92
- ...getParentProps(mate$1.getConstructor(instance)), // Inheritance support
91
+ ...getParentProps(mate$1.getConstructor(instance)),
93
92
  ...Object.getOwnPropertyNames(proto),
94
93
  ...Object.getOwnPropertyNames(instance),
95
94
  ].filter((m) => typeof instance[m] === 'function');
@@ -222,7 +221,6 @@ async function bindControllerMethods(options) {
222
221
  const { adapters } = opts;
223
222
  opts.globalPrefix = opts.globalPrefix || '';
224
223
  opts.provide = opts.provide || {};
225
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
226
224
  const fakeInstance = Object.create(classConstructor.prototype);
227
225
  const methods = getInstanceOwnMethods(fakeInstance);
228
226
  const mate = getMoostMate();
@@ -243,14 +241,12 @@ async function bindControllerMethods(options) {
243
241
  if (!methodMeta.handlers || !methodMeta.handlers.length)
244
242
  continue;
245
243
  const pipes = [...(opts.pipes || []), ...(methodMeta.pipes || [])].sort((a, b) => a.priority - b.priority);
246
- // preparing interceptors
247
244
  const interceptors = [
248
245
  ...(opts.interceptors || []),
249
246
  ...(meta.interceptors || []),
250
247
  ...(methodMeta.interceptors || []),
251
248
  ].sort((a, b) => a.priority - b.priority);
252
249
  const getIterceptorHandler = getIterceptorHandlerFactory(interceptors, getInstance, pipes, options.logger);
253
- // preparing pipes
254
250
  const argsPipes = [];
255
251
  for (const p of methodMeta.params || []) {
256
252
  argsPipes.push({
@@ -313,87 +309,34 @@ async function bindControllerMethods(options) {
313
309
  return controllerOverview;
314
310
  }
315
311
 
316
- /**
317
- * ## Label
318
- * ### @Decorator
319
- * _Common purpose decorator that may be used by various adapters for various purposes_
320
- *
321
- * Stores Label metadata
322
- */
323
312
  function Label(value) {
324
313
  return getMoostMate().decorate('label', value);
325
314
  }
326
- /**
327
- * ## Description
328
- * ### @Decorator
329
- * _Common purpose decorator that may be used by various adapters for various purposes_
330
- *
331
- * Stores Description metadata
332
- */
333
315
  function Description(value) {
334
316
  return getMoostMate().decorate('description', value);
335
317
  }
336
- /**
337
- * ## Value
338
- * ### @Decorator
339
- * _Common purpose decorator that may be used by various adapters for various purposes_
340
- *
341
- * Stores Value metadata
342
- */
343
318
  function Value(value) {
344
319
  return getMoostMate().decorate('value', value);
345
320
  }
346
- /**
347
- * ## Id
348
- * ### @Decorator
349
- * _Common purpose decorator that may be used by various adapters for various purposes_
350
- *
351
- * Stores Id metadata
352
- */
353
321
  function Id(value) {
354
322
  return getMoostMate().decorate('id', value);
355
323
  }
356
- /**
357
- * ## Optional
358
- * ### @Decorator
359
- * _Common purpose decorator that may be used by various adapters for various purposes_
360
- *
361
- * Stores Optional metadata
362
- */
363
324
  function Optional() {
364
325
  return getMoostMate().decorate('optional', true);
365
326
  }
366
- /**
367
- * ## Required
368
- * ### @Decorator
369
- * _Common purpose decorator that may be used by various adapters for various purposes_
370
- *
371
- * Stores Required metadata
372
- */
373
327
  function Required() {
374
328
  const mate = getMoostMate();
375
- return mate.apply(mate.decorate('required', true),
376
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
377
- mate.decorateClass((meta, level, key, index) => {
329
+ return mate.apply(mate.decorate('required', true), mate.decorateClass((meta, level, key, index) => {
378
330
  if (typeof index !== 'number' &&
379
331
  meta &&
380
332
  ['string', 'symbol'].includes(typeof key)) {
381
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
382
333
  meta.requiredProps = meta.requiredProps || [];
383
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
384
334
  meta.requiredProps.push(key);
385
335
  }
386
336
  return meta;
387
337
  }));
388
338
  }
389
339
 
390
- /**
391
- * Hook to the Response Status
392
- * @decorator
393
- * @param resolver - resolver function
394
- * @param label - field label
395
- * @paramType unknown
396
- */
397
340
  function Resolve(resolver, label) {
398
341
  return (target, key, index) => {
399
342
  const i = typeof index === 'number' ? index : undefined;
@@ -407,48 +350,18 @@ function Resolve(resolver, label) {
407
350
  })(target, key, i);
408
351
  };
409
352
  }
410
- /**
411
- * Get Param Value from url parh
412
- * @decorator
413
- * @param name - param name
414
- * @paramType string
415
- */
416
353
  function Param(name) {
417
354
  return getMoostMate().apply(getMoostMate().decorate('paramSource', 'ROUTE'), getMoostMate().decorate('paramName', name), Resolve(() => eventCore.useRouteParams().get(name), name));
418
355
  }
419
- /**
420
- * Get Parsed Params from url parh
421
- * @decorator
422
- * @paramType object
423
- */
424
356
  function Params() {
425
357
  return Resolve(() => eventCore.useRouteParams().params, 'params');
426
358
  }
427
- /**
428
- * Provide Const Value
429
- * @decorator
430
- * @param value - provided value
431
- * @param label - label of the field
432
- * @paramType unknown
433
- */
434
359
  function Const(value, label) {
435
360
  return Resolve(() => value, label);
436
361
  }
437
- /**
438
- * Provide Const Value from Factory fn
439
- * @decorator
440
- * @param factory - value Factory fn
441
- * @param label - label of the field
442
- * @paramType unknown
443
- */
444
362
  function ConstFactory(factory, label) {
445
363
  return Resolve(async () => await factory(), label);
446
364
  }
447
- /**
448
- * Resolves event logger from event context
449
- * @param topic
450
- * @returns Resolver to '@wooksjs/event-core' (EventLogger)
451
- */
452
365
  function InjectEventLogger(topic) {
453
366
  return Resolve(() => eventCore.useEventLogger(topic));
454
367
  }
@@ -470,15 +383,6 @@ function fillLabel(target, key, index, name) {
470
383
  }
471
384
  }
472
385
 
473
- /**
474
- * ## Injectable
475
- * ### @Decorator
476
- * Mark the Class as Injectable to enable it to be used in dependency injection
477
- * @param scope - Scope for injection ("FOR_EVENT" | "SINGLETON" | true)
478
- * FOR_EVENT - will create a new instance for each incoming request
479
- * SINGLETON | true - will create a new instance only once
480
- * @param label - field label
481
- */
482
386
  function Injectable(scope = true) {
483
387
  return getMoostMate().decorate('injectable', scope);
484
388
  }
@@ -488,12 +392,6 @@ const insureInjectable = getMoostMate().decorate((meta) => {
488
392
  return meta;
489
393
  });
490
394
 
491
- /**
492
- * ## Controller
493
- * ### @Decorator
494
- * Set Class as a Controller
495
- * @param prefix - define the prefix for all the paths of this controller
496
- */
497
395
  function Controller(prefix) {
498
396
  const mate = getMoostMate();
499
397
  return mate.apply(insureInjectable, mate.decorate('controller', { prefix: prefix || '' }));
@@ -524,14 +422,6 @@ exports.TInterceptorPriority = void 0;
524
422
  TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
525
423
  TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
526
424
  })(exports.TInterceptorPriority || (exports.TInterceptorPriority = {}));
527
- /**
528
- * ## Intercept
529
- * ### @Decorator
530
- * Set interceptor
531
- * @param handler interceptor fn (use defineInterceptorFn)
532
- * @param priority interceptor priority
533
- * @returns
534
- */
535
425
  function Intercept(handler, priority) {
536
426
  return getMoostMate().decorate('interceptors', {
537
427
  handler,
@@ -541,13 +431,6 @@ function Intercept(handler, priority) {
541
431
  }, true);
542
432
  }
543
433
 
544
- /**
545
- * ## Provide
546
- * ### @Decorator
547
- * Defines provide registry for class (and all the children)
548
- * @param type - string or class constructor
549
- * @param fn - factory function for provided value
550
- */
551
434
  function Provide(type, fn) {
552
435
  return getMoostMate().decorate((meta) => {
553
436
  meta.provide = meta.provide || {};
@@ -555,23 +438,10 @@ function Provide(type, fn) {
555
438
  return meta;
556
439
  });
557
440
  }
558
- /**
559
- * ## Inject
560
- * ### @Decorator
561
- * Defines a key from provide registry to inject value
562
- * (For optional values use with @Optional())
563
- * @param type - string or class constructor
564
- */
565
441
  function Inject(type) {
566
442
  return getMoostMate().decorate('inject', type);
567
443
  }
568
444
 
569
- /**
570
- * ## Inherit
571
- * ### @Decorator
572
- * Inherit metadata from super class
573
- * @returns
574
- */
575
445
  const Inherit = () => getMoostMate().decorate('inherit', true);
576
446
 
577
447
  exports.TPipePriority = void 0;
@@ -587,14 +457,6 @@ exports.TPipePriority = void 0;
587
457
  TPipePriority[TPipePriority["AFTER_VALIDATE"] = 8] = "AFTER_VALIDATE";
588
458
  })(exports.TPipePriority || (exports.TPipePriority = {}));
589
459
 
590
- /**
591
- * ## Pipe
592
- * ### @Decorator
593
- * Attach pipe
594
- * @param handler pipe handler
595
- * @param priority pipe priority
596
- * @returns
597
- */
598
460
  function Pipe(handler, priority) {
599
461
  if (typeof priority !== 'number') {
600
462
  priority =
@@ -605,50 +467,10 @@ function Pipe(handler, priority) {
605
467
  return getMoostMate().decorate('pipes', { handler, priority }, true);
606
468
  }
607
469
 
608
- /**
609
- * ### Define Interceptor Function
610
- *
611
- * ```ts
612
- * defineInterceptorFn((before, after, onError) => {
613
- * //init
614
- * before(() => {
615
- * // before handler
616
- * })
617
- * after((response, reply) => {
618
- * // after handler
619
- * })
620
- * onError((error, reply) => {
621
- * // when error occured
622
- * })
623
- * },
624
- * TInterceptorPriority.INTERCEPTOR,
625
- * )
626
- * ```
627
- *
628
- * @param fn interceptor function
629
- * @param priority priority of the interceptor where BEFORE_ALL = 0, BEFORE_GUARD = 1, GUARD = 2, AFTER_GUARD = 3, INTERCEPTOR = 4, CATCH_ERROR = 5, AFTER_ALL = 6
630
- * @returns
631
- */
632
470
  function defineInterceptorFn(fn, priority = exports.TInterceptorPriority.INTERCEPTOR) {
633
471
  fn.priority = priority;
634
472
  return fn;
635
473
  }
636
- /**
637
- * ### Define Pipe Function
638
- *
639
- * ```ts
640
- * // example of a transform pipe
641
- * const uppercaseTransformPipe = definePipeFn((value, metas, level) => {
642
- * return typeof value === 'string' ? value.toUpperCase() : value
643
- * },
644
- * TPipePriority.TRANSFORM,
645
- * )
646
- * ```
647
- *
648
- * @param fn interceptor function
649
- * @param priority priority of the pipe where BEFORE_RESOLVE = 0, RESOLVE = 1, AFTER_RESOLVE = 2, BEFORE_TRANSFORM = 3, TRANSFORM = 4, AFTER_TRANSFORM = 5, BEFORE_VALIDATE = 6, VALIDATE = 7, AFTER_VALIDATE = 8
650
- * @returns
651
- */
652
474
  function definePipeFn(fn, priority = exports.TPipePriority.TRANSFORM) {
653
475
  fn.priority = priority;
654
476
  return fn;
@@ -680,56 +502,6 @@ function getDefaultLogger(topic) {
680
502
  }, topic);
681
503
  }
682
504
 
683
- /**
684
- * ## Moost
685
- * Main moostjs class that serves as a shell for Moost Adapters
686
- *
687
- * ### Usage with HTTP Adapter
688
- * ```ts
689
- * │ // HTTP server example
690
- * │ import { MoostHttp, Get } from '@moostjs/event-http'
691
- * │ import { Moost, Param } from 'moost'
692
- * │
693
- * │ class MyServer extends Moost {
694
- * │ @Get('test/:name')
695
- * │ test(@Param('name') name: string) {
696
- * │ return { message: `Hello ${name}!` }
697
- * │ }
698
- * │ }
699
- * │
700
- * │ const app = new MyServer()
701
- * │ const http = new MoostHttp()
702
- * │ app.adapter(http).listen(3000, () => {
703
- * │ app.getLogger('MyApp').log('Up on port 3000')
704
- * │ })
705
- * │ app.init()
706
- * ```
707
- * ### Usage with CLI Adapter
708
- * ```ts
709
- * │ // CLI example
710
- * │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
711
- * │ import { Moost, Param } from 'moost'
712
- * │
713
- * │ class MyApp extends Moost {
714
- * │ @Cli('command/:arg')
715
- * │ command(
716
- * │ @Param('arg')
717
- * │ arg: string,
718
- * │ @CliOption('test', 't')
719
- * │ test: boolean,
720
- * │ ) {
721
- * │ return `command run with flag arg=${ arg }, test=${ test }`
722
- * │ }
723
- * │ }
724
- * │
725
- * │ const app = new MyApp()
726
- * │ app.applyGlobalInterceptors(cliHelpInterceptor())
727
- * │
728
- * │ const cli = new MoostCli()
729
- * │ app.adapter(cli)
730
- * │ app.init()
731
- * ```
732
- */
733
505
  class Moost {
734
506
  constructor(options) {
735
507
  this.options = options;
@@ -744,17 +516,6 @@ class Moost {
744
516
  const mate = getMoostMate();
745
517
  Object.assign(mate, { logger: this.getLogger('mate') });
746
518
  }
747
- /**
748
- * ### getLogger
749
- * Provides application logger
750
- * ```js
751
- * // get logger with topic = "App"
752
- * const logger = app.getLogger('App')
753
- * logger.log('...')
754
- * ```
755
- * @param topic
756
- * @returns
757
- */
758
519
  getLogger(topic) {
759
520
  if (this.logger instanceof logger.ProstoLogger) {
760
521
  return this.logger.createTopic(topic);
@@ -768,10 +529,6 @@ class Moost {
768
529
  getControllersOverview() {
769
530
  return this.controllersOverview;
770
531
  }
771
- /**
772
- * ### init
773
- * Ititializes adapter. Must be called after adapters are attached.
774
- */
775
532
  async init() {
776
533
  this.setProvideRegistry(infact$1.createProvideRegistry([Moost, () => this], [logger.ProstoLogger, () => this.logger], ['MOOST_LOGGER', () => this.logger]));
777
534
  for (const a of this.adapters) {
@@ -826,16 +583,13 @@ class Moost {
826
583
  instance = controller;
827
584
  infact.setProvideRegByInstance(instance, provide);
828
585
  }
829
- // getInstance - instance factory for resolving SINGLETON and FOR_EVENT instance
830
586
  const getInstance = instance
831
587
  ? () => Promise.resolve(instance)
832
588
  : async () => {
833
- // if (!instance) {
834
589
  infact.silent(true);
835
590
  const { restoreCtx } = eventCore.useEventContext();
836
591
  const instance = (await infact.get(controller, { ...infactOpts, syncContextFn: restoreCtx }));
837
592
  infact.silent(false);
838
- // }
839
593
  return instance;
840
594
  };
841
595
  const classConstructor = mate$1.isConstructor(controller)
@@ -861,9 +615,7 @@ class Moost {
861
615
  if (ic.typeResolver) {
862
616
  const isConstr = mate$1.isConstructor(ic.typeResolver);
863
617
  const isFunc = typeof ic.typeResolver === 'function';
864
- await this.bindController(
865
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
866
- isConstr
618
+ await this.bindController(isConstr
867
619
  ? ic.typeResolver
868
620
  : isFunc
869
621
  ? await ic.typeResolver()
@@ -894,12 +646,6 @@ class Moost {
894
646
  this.globalInterceptorHandler = undefined;
895
647
  return this;
896
648
  }
897
- /**
898
- * Provides InterceptorHandler with global interceptors and pipes.
899
- * Used to process interceptors when event handler was not found.
900
- *
901
- * @returns IterceptorHandler
902
- */
903
649
  getGlobalInterceptorHandler() {
904
650
  if (!this.globalInterceptorHandler) {
905
651
  const mate = getMoostMate();
@@ -932,20 +678,10 @@ class Moost {
932
678
  this.globalInterceptorHandler = undefined;
933
679
  return this;
934
680
  }
935
- /**
936
- * Register new entried to provide as dependency injections
937
- * @param provide - Provide Registry (use createProvideRegistry from '\@prostojs/infact')
938
- * @returns
939
- */
940
681
  setProvideRegistry(provide) {
941
682
  this.provide = { ...this.provide, ...provide };
942
683
  return this;
943
684
  }
944
- /**
945
- * Register controllers (similar to @ImportController decorator)
946
- * @param controllers - list of target controllers (instances)
947
- * @returns
948
- */
949
685
  registerControllers(...controllers) {
950
686
  this.unregisteredControllers.push(...controllers);
951
687
  return this;
@@ -963,9 +699,7 @@ function useControllerContext() {
963
699
  const { get } = store('controller');
964
700
  const getController = () => get('instance');
965
701
  const getMethod = () => get('method');
966
- // todo: add generic types to getControllerMeta
967
702
  const getControllerMeta = () => getMoostMate().read(getController());
968
- // todo: add generic types to getMethodMeta
969
703
  const getMethodMeta = (name) => getMoostMate().read(getController(), name || getMethod());
970
704
  function instantiate(c) {
971
705
  return getMoostInfact().getForInstance(getController(), c);
@@ -1029,12 +763,9 @@ function defineMoostEventHandler(options) {
1029
763
  }
1030
764
  let args = [];
1031
765
  if (options.resolveArgs) {
1032
- // params
1033
766
  restoreCtx();
1034
767
  try {
1035
- // logger.trace(`resolving method args for "${ opts.method as string }"`)
1036
768
  args = await options.resolveArgs();
1037
- // logger.trace(`args for method "${ opts.method as string }" resolved (count ${String(args.length)})`)
1038
769
  }
1039
770
  catch (e) {
1040
771
  options.logErrors && logger.error(e);
@@ -1048,7 +779,6 @@ function defineMoostEventHandler(options) {
1048
779
  if (typeof response !== 'undefined')
1049
780
  return endWithResponse();
1050
781
  }
1051
- // fire request handler
1052
782
  const callControllerMethod = () => {
1053
783
  restoreCtx();
1054
784
  if (options.callControllerMethod) {
@@ -1067,11 +797,9 @@ function defineMoostEventHandler(options) {
1067
797
  return endWithResponse(true);
1068
798
  }
1069
799
  async function endWithResponse(raise = false) {
1070
- // fire after interceptors
1071
800
  if (interceptorHandler) {
1072
801
  restoreCtx();
1073
802
  try {
1074
- // logger.trace('firing after interceptors')
1075
803
  response = await interceptorHandler.fireAfter(response);
1076
804
  }
1077
805
  catch (e) {