moost 0.5.21 → 0.5.23

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.d.ts CHANGED
@@ -1,13 +1,13 @@
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';
5
3
  import { TProvideRegistry, Infact, TReplaceRegistry, TProvideFn } from '@prostojs/infact';
6
4
  export { TProvideRegistry, createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
5
+ import { TConsoleBase } from '@prostojs/logger';
6
+ export { ProstoLogger, TConsoleBase } from '@prostojs/logger';
7
7
  import { Hookable } from 'hookable';
8
8
  import * as _prostojs_mate from '@prostojs/mate';
9
9
  import { TMateParamMeta, Mate } from '@prostojs/mate';
10
- export { getConstructor, isConstructor } from '@prostojs/mate';
10
+ export { Mate, TMateParamMeta, getConstructor, isConstructor } from '@prostojs/mate';
11
11
  export { clearGlobalWooks, getGlobalWooks } from 'wooks';
12
12
 
13
13
  type TAny = any;
@@ -20,86 +20,17 @@ interface TEmpty {
20
20
 
21
21
  declare function Circular<T = unknown>(resolver: () => TClassConstructor<T>): ParameterDecorator;
22
22
 
23
- /**
24
- * ## Label
25
- * ### @Decorator
26
- * _Common purpose decorator that may be used by various adapters for various purposes_
27
- *
28
- * Stores Label metadata
29
- */
30
23
  declare function Label(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
31
- /**
32
- * ## Description
33
- * ### @Decorator
34
- * _Common purpose decorator that may be used by various adapters for various purposes_
35
- *
36
- * Stores Description metadata
37
- */
38
24
  declare function Description(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
39
- /**
40
- * ## Value
41
- * ### @Decorator
42
- * _Common purpose decorator that may be used by various adapters for various purposes_
43
- *
44
- * Stores Value metadata
45
- */
46
25
  declare function Value(value: unknown): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
47
- /**
48
- * ## Id
49
- * ### @Decorator
50
- * _Common purpose decorator that may be used by various adapters for various purposes_
51
- *
52
- * Stores Id metadata
53
- */
54
26
  declare function Id(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
55
- /**
56
- * ## Optional
57
- * ### @Decorator
58
- * _Common purpose decorator that may be used by various adapters for various purposes_
59
- *
60
- * Stores Optional metadata
61
- */
62
27
  declare function Optional(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
63
- /**
64
- * ## Required
65
- * ### @Decorator
66
- * _Common purpose decorator that may be used by various adapters for various purposes_
67
- *
68
- * Stores Required metadata
69
- */
70
28
  declare function Required(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
71
29
 
72
- /**
73
- * ## Controller
74
- * ### @Decorator
75
- * Set Class as a Controller
76
- * @param prefix - define the prefix for all the paths of this controller
77
- */
78
30
  declare function Controller(prefix?: string): ClassDecorator;
79
- /**
80
- * ## ImportController
81
- * ### @Decorator
82
- * Attach sub-controller
83
- * @param controller - target controller (instance) to import
84
- * @param provide - provide registry for the target controller
85
- */
86
31
  declare function ImportController(controller: TFunction | TObject, provide?: TProvideRegistry): ClassDecorator;
87
- /**
88
- * ## ImportController
89
- * ### @Decorator
90
- * Attach sub-controller
91
- * @param prefix - redefine the prefix for all the paths of this controller
92
- * @param controller - point to a controller (instance) to import
93
- * @param provide - provide registry for the target controller
94
- */
95
32
  declare function ImportController(prefix: string, controller: TFunction | TObject, provide?: TProvideRegistry): ClassDecorator;
96
33
 
97
- /**
98
- * ## Inherit
99
- * ### @Decorator
100
- * Inherit metadata from super class
101
- * @returns
102
- */
103
34
  declare const Inherit: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
104
35
 
105
36
  interface TClassFunction<T extends TAnyFn = TAnyFn> {
@@ -125,14 +56,6 @@ declare enum TInterceptorPriority {
125
56
  CATCH_ERROR = 5,
126
57
  AFTER_ALL = 6
127
58
  }
128
- /**
129
- * ## Intercept
130
- * ### @Decorator
131
- * Set interceptor
132
- * @param handler interceptor fn (use defineInterceptorFn)
133
- * @param priority interceptor priority
134
- * @returns
135
- */
136
59
  declare function Intercept(handler: TCallableClassFunction<TInterceptorFn>, priority?: TInterceptorPriority, name?: string): ClassDecorator & MethodDecorator;
137
60
 
138
61
  type TDecoratorLevel = 'CLASS' | 'METHOD' | 'PROP' | 'PARAM';
@@ -147,23 +70,8 @@ declare function getMoostInfact(): Infact<TMoostMetadata<TEmpty>, TMoostMetadata
147
70
  interface TCustom {
148
71
  pipes?: TPipeData[];
149
72
  }
150
- /**
151
- * Define global scope name to be used with `@InjectFromScope` and `@InjectScopeVars` decorators
152
- *
153
- * You can read scoped vars with `getInfactScopeVars`
154
- * @param name scope name
155
- * @param scopeVars key-value object as scoped vars
156
- */
157
73
  declare function defineInfactScope<T extends object>(name: string | symbol, scopeVars: T): void;
158
- /**
159
- * Read scoped vars defined with `defineInfactScope`
160
- * @param name scope name
161
- * @returns key-value object as scoped vars
162
- */
163
74
  declare function getInfactScopeVars<T extends object>(name: string | symbol): T | undefined;
164
- /**
165
- * Get Infact instance (used for Dependency Injections)
166
- */
167
75
  declare function getNewMoostInfact(): Infact<TMoostMetadata<TEmpty>, TMoostMetadata<TEmpty>, TMoostParamsMetadata, TCustom>;
168
76
 
169
77
  interface TPipeMetas<T extends TObject = TEmpty> {
@@ -258,119 +166,24 @@ interface TCommonMoostMeta {
258
166
  type?: TFunction;
259
167
  }
260
168
 
261
- /**
262
- * ## Injectable
263
- * ### @Decorator
264
- * Mark the Class as Injectable to enable it to be used in dependency injection
265
- * @param scope - Scope for injection ("FOR_EVENT" | "SINGLETON" | true)
266
- * FOR_EVENT - will create a new instance for each incoming request
267
- * SINGLETON | true - will create a new instance only once
268
- * @param label - field label
269
- */
270
169
  declare function Injectable(scope?: true | TInjectableScope): ClassDecorator;
271
170
 
272
- /**
273
- * Resolves event logger from event context
274
- * @param topic
275
- * @returns Resolver to '@wooksjs/event-core' (EventLogger)
276
- */
277
171
  declare function InjectEventLogger(topic?: string): ParameterDecorator & PropertyDecorator;
278
- /**
279
- * Resolves app-level logger
280
- * @param topic - logger topic (can be overrided by @LoggerTopic)
281
- * @returns
282
- */
283
172
  declare function InjectMoostLogger(topic?: string): ParameterDecorator & PropertyDecorator;
284
- /**
285
- * Sets logger topic (used in @InjectMoostLogger)
286
- * @param topic - logger topic (banner)
287
- * @returns
288
- */
289
173
  declare function LoggerTopic(topic: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
290
174
 
291
- /**
292
- * ## Pipe
293
- * ### @Decorator
294
- * Attach pipe
295
- * @param handler pipe handler
296
- * @param priority pipe priority
297
- * @returns
298
- */
299
175
  declare function Pipe(handler: TPipeFn, priority?: TPipePriority): ClassDecorator & MethodDecorator & ParameterDecorator;
300
176
 
301
- /**
302
- * ## Provide
303
- * ### @Decorator
304
- * Defines provide registry for class (and all the children)
305
- * @param type - string or class constructor
306
- * @param fn - factory function for provided value
307
- */
308
177
  declare function Provide(type: string | TClassConstructor, fn: TProvideFn): ClassDecorator & ParameterDecorator & PropertyDecorator;
309
- /**
310
- * ## Replace
311
- * ### @Decorator
312
- * Defines class to replace in DI
313
- * @param type - class to replace
314
- * @param newType - new class
315
- */
316
178
  declare function Replace(type: TClassConstructor, newType: TClassConstructor): ClassDecorator;
317
- /**
318
- * ## Inject
319
- * ### @Decorator
320
- * Defines a key from provide registry to inject value
321
- * (For optional values use with @Optional())
322
- * @param type - string or class constructor
323
- */
324
179
  declare function Inject(type: string | TClassConstructor): ParameterDecorator & PropertyDecorator;
325
- /**
326
- * Injects instance from scope
327
- *
328
- * (scope must be defined by `defineInfactScope` fn)
329
- * @param name scope name
330
- */
331
180
  declare function InjectFromScope(name: string | symbol): ParameterDecorator & PropertyDecorator;
332
- /**
333
- * Inject vars from scope for instances
334
- * instantiated with `@InjectFromScope` decorator
335
- */
336
181
  declare function InjectScopeVars(name?: string): ParameterDecorator & PropertyDecorator;
337
182
 
338
- /**
339
- * Hook to the Response Status
340
- * @decorator
341
- * @param resolver - resolver function
342
- * @param label - field label
343
- * @paramType unknown
344
- */
345
183
  declare function Resolve<T extends TObject = TEmpty>(resolver: (metas: TPipeMetas<T>, level: TDecoratorLevel) => unknown, label?: string): ParameterDecorator & PropertyDecorator;
346
- /**
347
- * Get Param Value from url parh
348
- * @decorator
349
- * @param name - param name
350
- * @paramType string
351
- */
352
184
  declare function Param(name: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
353
- /**
354
- * Get Parsed Params from url parh
355
- * @decorator
356
- * @paramType object
357
- */
358
185
  declare function Params(): ParameterDecorator & PropertyDecorator;
359
- /**
360
- * Provide Const Value
361
- * @decorator
362
- * @param value - provided value
363
- * @param label - label of the field
364
- * @paramType unknown
365
- */
366
186
  declare function Const<T>(value: T, label?: string): ParameterDecorator & PropertyDecorator;
367
- /**
368
- * Provide Const Value from Factory fn
369
- * @decorator
370
- * @param factory - value Factory fn
371
- * @param label - label of the field
372
- * @paramType unknown
373
- */
374
187
  declare function ConstFactory<T>(factory: () => T | Promise<T>, label?: string): ParameterDecorator & PropertyDecorator;
375
188
 
376
189
  declare class InterceptorHandler {
@@ -458,62 +271,9 @@ interface THandlerOverview {
458
271
  type TContextInjectorHook = 'Event:start' | 'Interceptors:init' | 'Arguments:resolve' | 'Interceptors:before' | 'Handler' | 'Interceptors:after';
459
272
 
460
273
  interface TMoostOptions {
461
- /**
462
- * Prefix that is used for each event path
463
- */
464
274
  globalPrefix?: string;
465
275
  logger?: TConsoleBase;
466
276
  }
467
- /**
468
- * ## Moost
469
- * Main moostjs class that serves as a shell for Moost Adapters
470
- *
471
- * ### Usage with HTTP Adapter
472
- * ```ts
473
- * │ // HTTP server example
474
- * │ import { MoostHttp, Get } from '@moostjs/event-http'
475
- * │ import { Moost, Param } from 'moost'
476
- * │
477
- * │ class MyServer extends Moost {
478
- * │ @Get('test/:name')
479
- * │ test(@Param('name') name: string) {
480
- * │ return { message: `Hello ${name}!` }
481
- * │ }
482
- * │ }
483
- * │
484
- * │ const app = new MyServer()
485
- * │ const http = new MoostHttp()
486
- * │ app.adapter(http).listen(3000, () => {
487
- * │ app.getLogger('MyApp').log('Up on port 3000')
488
- * │ })
489
- * │ app.init()
490
- * ```
491
- * ### Usage with CLI Adapter
492
- * ```ts
493
- * │ // CLI example
494
- * │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
495
- * │ import { Moost, Param } from 'moost'
496
- * │
497
- * │ class MyApp extends Moost {
498
- * │ @Cli('command/:arg')
499
- * │ command(
500
- * │ @Param('arg')
501
- * │ arg: string,
502
- * │ @CliOption('test', 't')
503
- * │ test: boolean,
504
- * │ ) {
505
- * │ return `command run with flag arg=${ arg }, test=${ test }`
506
- * │ }
507
- * │ }
508
- * │
509
- * │ const app = new MyApp()
510
- * │ app.applyGlobalInterceptors(cliHelpInterceptor())
511
- * │
512
- * │ const cli = new MoostCli()
513
- * │ app.adapter(cli)
514
- * │ app.init()
515
- * ```
516
- */
517
277
  declare class Moost extends Hookable {
518
278
  protected options?: TMoostOptions | undefined;
519
279
  protected logger: TConsoleBase;
@@ -527,54 +287,18 @@ declare class Moost extends Hookable {
527
287
  constructor(options?: TMoostOptions | undefined);
528
288
  _fireEventStart(source: TMoostAdapter<unknown>): void;
529
289
  _fireEventEnd(source: TMoostAdapter<unknown>): void;
530
- /**
531
- * ### getLogger
532
- * Provides application logger
533
- * ```js
534
- * // get logger with topic = "App"
535
- * const logger = app.getLogger('App')
536
- * logger.log('...')
537
- * ```
538
- * @param topic
539
- * @returns
540
- */
541
290
  getLogger(topic?: string): TConsoleBase;
542
291
  adapter<T extends TMoostAdapter<TAny>>(a: T): T;
543
292
  getControllersOverview(): TControllerOverview[];
544
- /**
545
- * ### init
546
- * Ititializes adapter. Must be called after adapters are attached.
547
- */
548
293
  init(): Promise<void>;
549
294
  protected bindControllers(): Promise<void>;
550
295
  protected bindController(controller: TFunction | TObject, provide: TProvideRegistry, replace: TReplaceRegistry, globalPrefix: string, replaceOwnPrefix?: string): Promise<void>;
551
296
  applyGlobalPipes(...items: Array<TPipeFn | TPipeData>): this;
552
297
  protected globalInterceptorHandler?: () => Promise<InterceptorHandler>;
553
- /**
554
- * Provides InterceptorHandler with global interceptors and pipes.
555
- * Used to process interceptors when event handler was not found.
556
- *
557
- * @returns IterceptorHandler
558
- */
559
298
  getGlobalInterceptorHandler(): Promise<InterceptorHandler>;
560
299
  applyGlobalInterceptors(...items: Array<TInterceptorData['handler'] | TInterceptorData>): this;
561
- /**
562
- * Register new entries to provide as dependency injections
563
- * @param provide - Provide Registry (use createProvideRegistry from '\@prostojs/infact')
564
- * @returns
565
- */
566
300
  setProvideRegistry(provide: TProvideRegistry): this;
567
- /**
568
- * Register replace classes to provide as dependency injections
569
- * @param replace - Replace Registry (use createReplaceRegistry from '\@prostojs/infact')
570
- * @returns
571
- */
572
301
  setReplaceRegistry(replace: TReplaceRegistry): this;
573
- /**
574
- * Register controllers (similar to @ImportController decorator)
575
- * @param controllers - list of target controllers (instances)
576
- * @returns
577
- */
578
302
  registerControllers(...controllers: Array<TObject | TFunction | [string, TObject | TFunction]>): this;
579
303
  logMappedHandler(eventName: string, classConstructor: Function, method: string, stroke?: boolean, prefix?: string): void;
580
304
  }
@@ -602,17 +326,17 @@ declare function useControllerContext<T extends object>(): {
602
326
  getRoute: () => string | undefined;
603
327
  getController: () => T;
604
328
  getMethod: () => string | undefined;
605
- getControllerMeta: <TT extends object>() => (TMoostMetadata<TEmpty> & TT & {
606
- params: (TT & _prostojs_mate.TMateParamMeta)[];
607
- }) | undefined;
608
- getMethodMeta: <TT_1 extends object>(name?: string) => (TMoostMetadata<TEmpty> & TT_1 & {
329
+ getControllerMeta: <TT_1 extends object>() => (TMoostMetadata<TEmpty> & TT_1 & {
609
330
  params: (TT_1 & _prostojs_mate.TMateParamMeta)[];
331
+ }) | undefined;
332
+ getMethodMeta: <TT_2 extends object>(name?: string) => (TMoostMetadata<TEmpty> & TT_2 & {
333
+ params: (TT_2 & _prostojs_mate.TMateParamMeta)[];
610
334
  } & {
611
- params: (TT_1 & _prostojs_mate.TMateParamMeta)[];
612
- } & _prostojs_mate.TMateClassMeta<(TMoostMetadata<TEmpty> & TT_1 & {
613
- params: (TT_1 & _prostojs_mate.TMateParamMeta)[];
614
- })["params"][0]> & _prostojs_mate.TMatePropMeta<(TMoostMetadata<TEmpty> & TT_1 & {
615
- params: (TT_1 & _prostojs_mate.TMateParamMeta)[];
335
+ params: (TT_2 & _prostojs_mate.TMateParamMeta)[];
336
+ } & _prostojs_mate.TMateClassMeta<(TMoostMetadata<TEmpty> & TT_2 & {
337
+ params: (TT_2 & _prostojs_mate.TMateParamMeta)[];
338
+ })["params"][0]> & _prostojs_mate.TMatePropMeta<(TMoostMetadata<TEmpty> & TT_2 & {
339
+ params: (TT_2 & _prostojs_mate.TMateParamMeta)[];
616
340
  })["params"][0]>) | undefined;
617
341
  getPropertiesList: () => (string | symbol)[];
618
342
  getScope: () => true | TInjectableScope;
@@ -624,70 +348,8 @@ declare function useControllerContext<T extends object>(): {
624
348
  } & _prostojs_mate.TMateClassMeta<_prostojs_mate.TMateParamMeta & TMoostParamsMetadata & object> & _prostojs_mate.TMatePropMeta<_prostojs_mate.TMateParamMeta & TMoostParamsMetadata & object>) | undefined;
625
349
  };
626
350
 
627
- /**
628
- * ### Define Interceptor Function
629
- *
630
- * ```ts
631
- * defineInterceptorFn((before, after, onError) => {
632
- * //init
633
- * before(() => {
634
- * // before handler
635
- * })
636
- * after((response, reply) => {
637
- * // after handler
638
- * })
639
- * onError((error, reply) => {
640
- * // when error occured
641
- * })
642
- * },
643
- * TInterceptorPriority.INTERCEPTOR,
644
- * )
645
- * ```
646
- *
647
- * @param fn interceptor function
648
- * @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
649
- * @returns
650
- */
651
351
  declare function defineInterceptorFn(fn: TInterceptorFn, priority?: TInterceptorPriority): TInterceptorFn;
652
- /**
653
- * Class based interceptor interface
654
- *
655
- * Use it to create class-based interceptors and don't forget to make it **@Injectable()**
656
- *
657
- * @example
658
- * "@Injectable()
659
- * export class MyInterceptor implements TInterceptorClass {
660
- * static priority = TInterceptorPriority.INTERCEPTOR
661
- * handler: TInterceptorClass['handler'] = (before, after, onError) => {
662
- * before((reply) => {
663
- * console.log('before')
664
- * })
665
- * after((response, reply) => {
666
- * console.log('after')
667
- * })
668
- * onError((error, reply) => {
669
- * console.log('error')
670
- * })
671
- * }
672
- * }
673
- */
674
352
  type TInterceptorClass = TClassFunction<TInterceptorFn>;
675
- /**
676
- * ### Define Pipe Function
677
- *
678
- * ```ts
679
- * // example of a transform pipe
680
- * const uppercaseTransformPipe = definePipeFn((value, metas, level) => {
681
- * return typeof value === 'string' ? value.toUpperCase() : value
682
- * },
683
- * TPipePriority.TRANSFORM,
684
- * )
685
- * ```
686
- *
687
- * @param fn interceptor function
688
- * @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
689
- * @returns
690
- */
691
353
  declare function definePipeFn<T extends TObject = TEmpty>(fn: TPipeFn<T>, priority?: TPipePriority): TPipeFn<T>;
692
354
 
693
355
  export { Circular, Const, ConstFactory, Controller, Description, Id, ImportController, Inherit, Inject, InjectEventLogger, InjectFromScope, InjectMoostLogger, InjectScopeVars, Injectable, Intercept, InterceptorHandler, Label, LoggerTopic, 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, defineInfactScope, defineInterceptorFn, defineMoostEventHandler, definePipeFn, getInfactScopeVars, getInstanceOwnMethods, getInstanceOwnProps, getMoostInfact, getMoostMate, getNewMoostInfact, registerEventScope, resolvePipe, setControllerContext, setInfactLoggingOptions, useControllerContext };