moost 0.2.27 → 0.2.28

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
@@ -8,6 +8,7 @@ import { TProvideFn } from '@prostojs/infact';
8
8
  import { TProvideRegistry } from '@prostojs/infact';
9
9
  import { TValidoDtoOptions } from '@prostojs/valido';
10
10
  import { TValidoFn } from '@prostojs/valido';
11
+ import { useEventLogger } from '@wooksjs/event-core';
11
12
  import { validoIsBoolean } from '@prostojs/valido';
12
13
  import { validoIsNumber } from '@prostojs/valido';
13
14
  import { validoIsString } from '@prostojs/valido';
@@ -33,12 +34,68 @@ export declare function Const<T>(value: T, label?: string): ParameterDecorator &
33
34
  export declare function ConstFactory<T>(factory: () => T | Promise<T>, label?: string): ParameterDecorator & PropertyDecorator;
34
35
 
35
36
  /**
37
+ * ## Controller
38
+ * ### @Decorator
36
39
  * Set Class as a Controller
37
- * @decorator
38
40
  * @param prefix - define the prefix for all the paths of this controller
39
41
  */
40
42
  export declare function Controller(prefix?: string): ClassDecorator;
41
43
 
44
+ /**
45
+ * ### Define Interceptor Function
46
+ *
47
+ * ```ts
48
+ * defineInterceptorFn((before, after, onError) => {
49
+ * //init
50
+ * before(() => {
51
+ * // before handler
52
+ * })
53
+ * after((response, reply) => {
54
+ * // after handler
55
+ * })
56
+ * onError((error, reply) => {
57
+ * // when error occured
58
+ * })
59
+ * },
60
+ * TInterceptorPriority.INTERCEPTOR,
61
+ * )
62
+ * ```
63
+ *
64
+ * @param fn interceptor function
65
+ * @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
66
+ * @returns
67
+ */
68
+ export declare function defineInterceptorFn(fn: TInterceptorFn, priority?: TInterceptorPriority): TInterceptorFn;
69
+
70
+ export declare function defineMoostEventHandler<T>(options: TMoostEventHandlerOptions<T>): () => Promise<unknown>;
71
+
72
+ /**
73
+ * ### Define Pipe Function
74
+ *
75
+ * ```ts
76
+ * // example of a transform pipe
77
+ * const uppercaseTransformPipe = definePipeFn((value, metas, level) => {
78
+ * return typeof value === 'string' ? value.toUpperCase() : value
79
+ * },
80
+ * TPipePriority.TRANSFORM,
81
+ * )
82
+ * ```
83
+ *
84
+ * @param fn interceptor function
85
+ * @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
86
+ * @returns
87
+ */
88
+ export declare function definePipeFn(fn: TPipeFn, priority?: TPipePriority): TPipeFn<TEmpty>;
89
+
90
+ /**
91
+ * ## Description
92
+ * ### @Decorator
93
+ * _Common purpose decorator that may be used by various adapters for various purposes_
94
+ *
95
+ * Stores Description metadata
96
+ */
97
+ export declare function Description(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
98
+
42
99
  export declare function Dto(dtoOptions?: TValidoDtoOptions): ClassDecorator;
43
100
 
44
101
  export declare const genericTypesCastPipe: (strict?: boolean) => TPipeFn;
@@ -49,28 +106,45 @@ export declare function getMoostMate<Class extends TObject = TEmpty, Prop extend
49
106
 
50
107
  export declare function getNewMoostInfact(): Infact<TMoostMetadata, TMoostMetadata, TMoostParamsMetadata, TCustom>;
51
108
 
109
+ /**
110
+ * ## Id
111
+ * ### @Decorator
112
+ * _Common purpose decorator that may be used by various adapters for various purposes_
113
+ *
114
+ * Stores Id metadata
115
+ */
52
116
  export declare function Id(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
53
117
 
54
118
  /**
55
- * Set Class as a Controller
56
- * @decorator
119
+ * ## ImportController
120
+ * ### @Decorator
121
+ * Attach sub-controller
57
122
  * @param controller - target controller (instance) to import
58
123
  * @param provide - provide registry for the target controller
59
124
  */
60
125
  export declare function ImportController(controller: TFunction | TObject, provide?: TProvideRegistry): ClassDecorator;
61
126
 
62
127
  /**
63
- * Set Class as a Controller
64
- * @decorator
128
+ * ## ImportController
129
+ * ### @Decorator
130
+ * Attach sub-controller
65
131
  * @param prefix - redefine the prefix for all the paths of this controller
66
132
  * @param controller - point to a controller (instance) to import
67
133
  * @param provide - provide registry for the target controller
68
134
  */
69
135
  export declare function ImportController(prefix: string, controller: TFunction | TObject, provide?: TProvideRegistry): ClassDecorator;
70
136
 
137
+ /**
138
+ * ## Inherit
139
+ * ### @Decorator
140
+ * Inherit metadata from super class
141
+ * @returns
142
+ */
71
143
  export declare const Inherit: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
72
144
 
73
145
  /**
146
+ * ## Inject
147
+ * ### @Decorator
74
148
  * Defines a key from provide registry to inject value
75
149
  * (For optional values use with @Nullable())
76
150
  * @param type - string or class constructor
@@ -78,8 +152,9 @@ export declare const Inherit: () => MethodDecorator & ClassDecorator & Parameter
78
152
  export declare function Inject(type: string | TClassConstructor): ParameterDecorator;
79
153
 
80
154
  /**
155
+ * ## Injectable
156
+ * ### @Decorator
81
157
  * Mark the Class as Injectable to enable it to be used in dependency injection
82
- * @decorator
83
158
  * @param scope - Scope for injection ("FOR_EVENT" | "SINGLETON" | true)
84
159
  * FOR_EVENT - will create a new instance for each incoming request
85
160
  * SINGLETON | true - will create a new instance only once
@@ -94,6 +169,14 @@ export declare function Injectable(scope?: true | TInjectableScope): ClassDecora
94
169
  */
95
170
  export declare function InjectEventLogger(topic?: string): ParameterDecorator & PropertyDecorator;
96
171
 
172
+ /**
173
+ * ## Intercept
174
+ * ### @Decorator
175
+ * Set interceptor
176
+ * @param handler interceptor fn (use defineInterceptorFn)
177
+ * @param priority interceptor priority
178
+ * @returns
179
+ */
97
180
  export declare function Intercept(handler: TCallableClassFunction<TInterceptorFn>, priority?: TInterceptorPriority): ClassDecorator & MethodDecorator;
98
181
 
99
182
  export declare class InterceptorHandler {
@@ -105,7 +188,7 @@ export declare class InterceptorHandler {
105
188
  response?: unknown;
106
189
  responseOverwritten: boolean;
107
190
  replyFn(reply: unknown): void;
108
- init(): Promise<void>;
191
+ init(): Promise<unknown>;
109
192
  fireBefore(response: unknown): Promise<unknown>;
110
193
  fireAfter(response: unknown): Promise<unknown>;
111
194
  }
@@ -120,8 +203,65 @@ export declare function IsString(...args: Parameters<typeof validoIsString>): Pr
120
203
 
121
204
  export declare function IsTypeOf(type: 'string' | 'object' | 'number' | 'boolean', errorText?: string): PropertyDecorator & ParameterDecorator;
122
205
 
206
+ /**
207
+ * ## Label
208
+ * ### @Decorator
209
+ * _Common purpose decorator that may be used by various adapters for various purposes_
210
+ *
211
+ * Stores Label metadata
212
+ */
123
213
  export declare function Label(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
124
214
 
215
+ /**
216
+ * ## Moost
217
+ * Main moostjs class that serves as a shell for Moost Adapters
218
+ *
219
+ * ### Usage with HTTP Adapter
220
+ * ```ts
221
+ * │ // HTTP server example
222
+ * │ import { MoostHttp, Get } from '@moostjs/event-http'
223
+ * │ import { Moost, Param } from 'moost'
224
+ * │
225
+ * │ class MyServer extends Moost {
226
+ * │ @Get('test/:name')
227
+ * │ test(@Param('name') name: string) {
228
+ * │ return { message: `Hello ${name}!` }
229
+ * │ }
230
+ * │ }
231
+ * │
232
+ * │ const app = new MyServer()
233
+ * │ const http = new MoostHttp()
234
+ * │ app.adapter(http).listen(3000, () => {
235
+ * │ app.getLogger('MyApp').log('Up on port 3000')
236
+ * │ })
237
+ * │ app.init()
238
+ * ```
239
+ * ### Usage with CLI Adapter
240
+ * ```ts
241
+ * │ // CLI example
242
+ * │ import { MoostCli, Cli, CliOption, cliHelpInterceptor } from '@moostjs/event-cli'
243
+ * │ import { Moost, Param } from 'moost'
244
+ * │
245
+ * │ class MyApp extends Moost {
246
+ * │ @Cli('command/:arg')
247
+ * │ command(
248
+ * │ @Param('arg')
249
+ * │ arg: string,
250
+ * │ @CliOption('test', 't')
251
+ * │ test: boolean,
252
+ * │ ) {
253
+ * │ return `command run with flag arg=${ arg }, test=${ test }`
254
+ * │ }
255
+ * │ }
256
+ * │
257
+ * │ const app = new MyApp()
258
+ * │ app.applyGlobalInterceptors(cliHelpInterceptor())
259
+ * │
260
+ * │ const cli = new MoostCli()
261
+ * │ app.adapter(cli)
262
+ * │ app.init()
263
+ * ```
264
+ */
125
265
  export declare class Moost {
126
266
  protected options?: TMoostOptions | undefined;
127
267
  protected logger: TConsoleBase;
@@ -131,12 +271,35 @@ export declare class Moost {
131
271
  protected provide: TProvideRegistry;
132
272
  protected unregisteredControllers: (TObject | TFunction)[];
133
273
  constructor(options?: TMoostOptions | undefined);
274
+ /**
275
+ * ### getLogger
276
+ * Provides application logger
277
+ * ```js
278
+ * // get logger with topic = "App"
279
+ * const logger = app.getLogger('App')
280
+ * logger.log('...')
281
+ * ```
282
+ * @param topic
283
+ * @returns
284
+ */
134
285
  getLogger(topic: string): TConsoleBase;
135
- adapter<T extends object, A extends TMoostAdapter<T>>(a: A): A;
286
+ adapter<T>(a: TMoostAdapter<T>): TMoostAdapter<T>;
287
+ /**
288
+ * ### init
289
+ * Ititializes adapter. Must be called after adapters are attached.
290
+ */
136
291
  init(): Promise<void>;
137
292
  protected bindControllers(): Promise<void>;
138
293
  protected bindController(controller: TFunction | TObject, provide: TProvideRegistry, globalPrefix: string, replaceOwnPrefix?: string): Promise<void>;
139
294
  applyGlobalPipes(...items: (TPipeFn | TPipeData)[]): this;
295
+ protected globalInterceptorHandler?: Promise<InterceptorHandler>;
296
+ /**
297
+ * Provides InterceptorHandler with global interceptors and pipes.
298
+ * Used to process interceptors when event handler was not found.
299
+ *
300
+ * @returns array of interceptors
301
+ */
302
+ getGlobalInterceptorHandler(): Promise<InterceptorHandler>;
140
303
  applyGlobalInterceptors(...items: (TInterceptorData['handler'] | TInterceptorData)[]): this;
141
304
  /**
142
305
  * Register new entried to provide as dependency injections
@@ -153,11 +316,20 @@ export declare class Moost {
153
316
  }
154
317
 
155
318
  /**
319
+ * ## Nullable
320
+ * ### @Decorator
156
321
  * Makes injectable value optional
157
322
  * @param value default true
158
323
  */
159
324
  export declare function Nullable(v?: boolean): ParameterDecorator;
160
325
 
326
+ /**
327
+ * ## Optional
328
+ * ### @Decorator
329
+ * _Common purpose decorator that may be used by various adapters for various purposes_
330
+ *
331
+ * Stores Optional metadata
332
+ */
161
333
  export declare function Optional(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
162
334
 
163
335
  /**
@@ -166,7 +338,7 @@ export declare function Optional(): MethodDecorator & ClassDecorator & Parameter
166
338
  * @param name - param name
167
339
  * @paramType string
168
340
  */
169
- export declare function Param(name: string): ParameterDecorator & PropertyDecorator;
341
+ export declare function Param(name: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
170
342
 
171
343
  /**
172
344
  * Get Parsed Params from url parh
@@ -176,12 +348,23 @@ export declare function Param(name: string): ParameterDecorator & PropertyDecora
176
348
  export declare function Params(): ParameterDecorator & PropertyDecorator;
177
349
 
178
350
  /**
351
+ * ## Provide
352
+ * ### @Decorator
179
353
  * Defines provide registry for class (and all the children)
180
354
  * @param type - string or class constructor
181
355
  * @param fn - factory function for provided value
182
356
  */
183
357
  export declare function Provide(type: string | TClassConstructor, fn: TProvideFn): ClassDecorator;
184
358
 
359
+ export declare function registerEventScope(scopeId: string): () => void;
360
+
361
+ /**
362
+ * ## Required
363
+ * ### @Decorator
364
+ * _Common purpose decorator that may be used by various adapters for various purposes_
365
+ *
366
+ * Stores Required metadata
367
+ */
185
368
  declare function Required_2(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
186
369
  export { Required_2 as Required }
187
370
 
@@ -194,7 +377,7 @@ export { Required_2 as Required }
194
377
  */
195
378
  export declare function Resolve<T extends TObject = TEmpty>(resolver: (metas: TPipeMetas<T>, level: TDecoratorLevel) => unknown, label?: string): ParameterDecorator & PropertyDecorator;
196
379
 
197
- export declare const resolvePipe: TPipeFn;
380
+ export declare const resolvePipe: TPipeFn<TEmpty>;
198
381
 
199
382
  export declare function setControllerContext<T>(controller: T, method: keyof T): void;
200
383
 
@@ -219,6 +402,8 @@ declare interface TClassFunctionConstructor<T extends TAnyFn = TAnyFn> {
219
402
  declare interface TCommonMetaFields {
220
403
  id?: string;
221
404
  label?: string;
405
+ value?: unknown;
406
+ description?: string;
222
407
  optional?: boolean;
223
408
  required?: boolean;
224
409
  }
@@ -250,13 +435,15 @@ export declare type TInterceptorAfter = (response: TAny, reply: (response: TAny)
250
435
 
251
436
  export declare type TInterceptorBefore = (reply: (response: TAny) => void) => void | Promise<void>;
252
437
 
438
+ export declare type TInterceptorClass = TClassFunction<TInterceptorFn>;
439
+
253
440
  export declare interface TInterceptorData {
254
441
  handler: TCallableClassFunction<TInterceptorFn>;
255
442
  priority: TInterceptorPriority;
256
443
  }
257
444
 
258
445
  export declare type TInterceptorFn = {
259
- (before: (fn: TInterceptorBefore) => void, after: (fn: TInterceptorAfter) => void, onError: (fn: TInterceptorOnError) => void): void | Promise<void>;
446
+ (before: (fn: TInterceptorBefore) => void, after: (fn: TInterceptorAfter) => void, onError: (fn: TInterceptorOnError) => void): unknown | Promise<unknown>;
260
447
  priority?: TInterceptorPriority;
261
448
  };
262
449
 
@@ -272,25 +459,51 @@ export declare enum TInterceptorPriority {
272
459
  AFTER_ALL = 6
273
460
  }
274
461
 
275
- export declare interface TMoostAdapter<H extends object> {
462
+ export declare interface TMoostAdapter<H> {
276
463
  bindHandler<T extends TObject = TObject>(options: TMoostAdapterOptions<H, T>): void | Promise<void>;
277
- onInit?: () => void | Promise<void>;
464
+ onInit?: (moost: Moost) => void | Promise<void>;
278
465
  getProvideRegistry?: () => TProvideRegistry;
279
466
  }
280
467
 
281
- export declare interface TMoostAdapterOptions<H extends object, T extends object> {
468
+ export declare interface TMoostAdapterOptions<H, T> {
282
469
  prefix: string;
283
470
  fakeInstance: T;
284
471
  getInstance: () => Promise<T>;
285
472
  method: keyof T;
286
473
  handlers: TMoostHandler<H>[];
287
474
  getIterceptorHandler: () => Promise<InterceptorHandler>;
288
- registerEventScope: (scopeId: string) => () => void;
289
475
  resolveArgs: () => Promise<unknown[]>;
290
476
  logHandler: (eventName: string) => void;
291
477
  }
292
478
 
293
- export declare type TMoostHandler<T extends object> = T & {
479
+ export declare interface TMoostEventHandlerHookOptions<T> {
480
+ restoreCtx: () => void;
481
+ scopeId: string;
482
+ logger: ReturnType<typeof useEventLogger>;
483
+ unscope: () => void;
484
+ instance?: T;
485
+ method?: keyof T;
486
+ getResponse: () => unknown;
487
+ reply: (r: unknown) => void;
488
+ }
489
+
490
+ export declare interface TMoostEventHandlerOptions<T> {
491
+ contextType?: string | string[];
492
+ loggerTitle: string;
493
+ getIterceptorHandler: () => Promise<InterceptorHandler> | InterceptorHandler | undefined;
494
+ getControllerInstance: () => Promise<T> | T | undefined;
495
+ controllerMethod?: keyof T;
496
+ callControllerMethod?: (args: unknown[]) => unknown;
497
+ resolveArgs?: () => Promise<unknown[]> | unknown[];
498
+ logErrors?: boolean;
499
+ manualUnscope?: boolean;
500
+ hooks?: {
501
+ init?: (opts: TMoostEventHandlerHookOptions<T>) => unknown;
502
+ end?: (opts: TMoostEventHandlerHookOptions<T>) => unknown;
503
+ };
504
+ }
505
+
506
+ export declare type TMoostHandler<T> = T & {
294
507
  type: string;
295
508
  };
296
509
 
@@ -313,6 +526,9 @@ export declare interface TMoostMetadata extends TCommonMetaFields, TCommonMoostM
313
526
  }
314
527
 
315
528
  export declare interface TMoostOptions {
529
+ /**
530
+ * Prefix that is used for each event path
531
+ */
316
532
  globalPrefix?: string;
317
533
  logger?: TConsoleBase;
318
534
  }
@@ -321,6 +537,7 @@ export declare interface TMoostParamsMetadata extends TCommonMetaFields, TCommon
321
537
  circular?: () => TAny;
322
538
  inject?: string | symbol | TClassConstructor;
323
539
  nullable?: boolean;
540
+ isRouteParam?: string;
324
541
  }
325
542
 
326
543
  declare type TObject = object;
@@ -379,4 +596,13 @@ export declare function Validate<T = unknown>(validator: TValidoFn<T>): Property
379
596
 
380
597
  export declare const validatePipe: (opts?: TValidatePipeOptions) => TPipeFn;
381
598
 
599
+ /**
600
+ * ## Value
601
+ * ### @Decorator
602
+ * _Common purpose decorator that may be used by various adapters for various purposes_
603
+ *
604
+ * Stores Value metadata
605
+ */
606
+ export declare function Value(value: unknown): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
607
+
382
608
  export { }