moost 0.2.26 → 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
@@ -1,10 +1,14 @@
1
1
  import { Infact } from '@prostojs/infact';
2
2
  import { Mate } from '@prostojs/mate';
3
3
  import { TConsoleBase } from '@prostojs/logger';
4
+ import { TMateClassMeta } from '@prostojs/mate';
5
+ import { TMateParamMeta } from '@prostojs/mate';
6
+ import { TMatePropMeta } from '@prostojs/mate';
4
7
  import { TProvideFn } from '@prostojs/infact';
5
8
  import { TProvideRegistry } from '@prostojs/infact';
6
9
  import { TValidoDtoOptions } from '@prostojs/valido';
7
10
  import { TValidoFn } from '@prostojs/valido';
11
+ import { useEventLogger } from '@wooksjs/event-core';
8
12
  import { validoIsBoolean } from '@prostojs/valido';
9
13
  import { validoIsNumber } from '@prostojs/valido';
10
14
  import { validoIsString } from '@prostojs/valido';
@@ -30,12 +34,68 @@ export declare function Const<T>(value: T, label?: string): ParameterDecorator &
30
34
  export declare function ConstFactory<T>(factory: () => T | Promise<T>, label?: string): ParameterDecorator & PropertyDecorator;
31
35
 
32
36
  /**
37
+ * ## Controller
38
+ * ### @Decorator
33
39
  * Set Class as a Controller
34
- * @decorator
35
40
  * @param prefix - define the prefix for all the paths of this controller
36
41
  */
37
42
  export declare function Controller(prefix?: string): ClassDecorator;
38
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
+
39
99
  export declare function Dto(dtoOptions?: TValidoDtoOptions): ClassDecorator;
40
100
 
41
101
  export declare const genericTypesCastPipe: (strict?: boolean) => TPipeFn;
@@ -46,28 +106,45 @@ export declare function getMoostMate<Class extends TObject = TEmpty, Prop extend
46
106
 
47
107
  export declare function getNewMoostInfact(): Infact<TMoostMetadata, TMoostMetadata, TMoostParamsMetadata, TCustom>;
48
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
+ */
49
116
  export declare function Id(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
50
117
 
51
118
  /**
52
- * Set Class as a Controller
53
- * @decorator
119
+ * ## ImportController
120
+ * ### @Decorator
121
+ * Attach sub-controller
54
122
  * @param controller - target controller (instance) to import
55
123
  * @param provide - provide registry for the target controller
56
124
  */
57
125
  export declare function ImportController(controller: TFunction | TObject, provide?: TProvideRegistry): ClassDecorator;
58
126
 
59
127
  /**
60
- * Set Class as a Controller
61
- * @decorator
128
+ * ## ImportController
129
+ * ### @Decorator
130
+ * Attach sub-controller
62
131
  * @param prefix - redefine the prefix for all the paths of this controller
63
132
  * @param controller - point to a controller (instance) to import
64
133
  * @param provide - provide registry for the target controller
65
134
  */
66
135
  export declare function ImportController(prefix: string, controller: TFunction | TObject, provide?: TProvideRegistry): ClassDecorator;
67
136
 
137
+ /**
138
+ * ## Inherit
139
+ * ### @Decorator
140
+ * Inherit metadata from super class
141
+ * @returns
142
+ */
68
143
  export declare const Inherit: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
69
144
 
70
145
  /**
146
+ * ## Inject
147
+ * ### @Decorator
71
148
  * Defines a key from provide registry to inject value
72
149
  * (For optional values use with @Nullable())
73
150
  * @param type - string or class constructor
@@ -75,8 +152,9 @@ export declare const Inherit: () => MethodDecorator & ClassDecorator & Parameter
75
152
  export declare function Inject(type: string | TClassConstructor): ParameterDecorator;
76
153
 
77
154
  /**
155
+ * ## Injectable
156
+ * ### @Decorator
78
157
  * Mark the Class as Injectable to enable it to be used in dependency injection
79
- * @decorator
80
158
  * @param scope - Scope for injection ("FOR_EVENT" | "SINGLETON" | true)
81
159
  * FOR_EVENT - will create a new instance for each incoming request
82
160
  * SINGLETON | true - will create a new instance only once
@@ -91,6 +169,14 @@ export declare function Injectable(scope?: true | TInjectableScope): ClassDecora
91
169
  */
92
170
  export declare function InjectEventLogger(topic?: string): ParameterDecorator & PropertyDecorator;
93
171
 
172
+ /**
173
+ * ## Intercept
174
+ * ### @Decorator
175
+ * Set interceptor
176
+ * @param handler interceptor fn (use defineInterceptorFn)
177
+ * @param priority interceptor priority
178
+ * @returns
179
+ */
94
180
  export declare function Intercept(handler: TCallableClassFunction<TInterceptorFn>, priority?: TInterceptorPriority): ClassDecorator & MethodDecorator;
95
181
 
96
182
  export declare class InterceptorHandler {
@@ -102,7 +188,7 @@ export declare class InterceptorHandler {
102
188
  response?: unknown;
103
189
  responseOverwritten: boolean;
104
190
  replyFn(reply: unknown): void;
105
- init(): Promise<void>;
191
+ init(): Promise<unknown>;
106
192
  fireBefore(response: unknown): Promise<unknown>;
107
193
  fireAfter(response: unknown): Promise<unknown>;
108
194
  }
@@ -117,8 +203,65 @@ export declare function IsString(...args: Parameters<typeof validoIsString>): Pr
117
203
 
118
204
  export declare function IsTypeOf(type: 'string' | 'object' | 'number' | 'boolean', errorText?: string): PropertyDecorator & ParameterDecorator;
119
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
+ */
120
213
  export declare function Label(value: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
121
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
+ */
122
265
  export declare class Moost {
123
266
  protected options?: TMoostOptions | undefined;
124
267
  protected logger: TConsoleBase;
@@ -128,12 +271,35 @@ export declare class Moost {
128
271
  protected provide: TProvideRegistry;
129
272
  protected unregisteredControllers: (TObject | TFunction)[];
130
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
+ */
131
285
  getLogger(topic: string): TConsoleBase;
132
- 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
+ */
133
291
  init(): Promise<void>;
134
292
  protected bindControllers(): Promise<void>;
135
293
  protected bindController(controller: TFunction | TObject, provide: TProvideRegistry, globalPrefix: string, replaceOwnPrefix?: string): Promise<void>;
136
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>;
137
303
  applyGlobalInterceptors(...items: (TInterceptorData['handler'] | TInterceptorData)[]): this;
138
304
  /**
139
305
  * Register new entried to provide as dependency injections
@@ -150,11 +316,20 @@ export declare class Moost {
150
316
  }
151
317
 
152
318
  /**
319
+ * ## Nullable
320
+ * ### @Decorator
153
321
  * Makes injectable value optional
154
322
  * @param value default true
155
323
  */
156
324
  export declare function Nullable(v?: boolean): ParameterDecorator;
157
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
+ */
158
333
  export declare function Optional(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
159
334
 
160
335
  /**
@@ -163,7 +338,7 @@ export declare function Optional(): MethodDecorator & ClassDecorator & Parameter
163
338
  * @param name - param name
164
339
  * @paramType string
165
340
  */
166
- export declare function Param(name: string): ParameterDecorator & PropertyDecorator;
341
+ export declare function Param(name: string): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
167
342
 
168
343
  /**
169
344
  * Get Parsed Params from url parh
@@ -173,12 +348,23 @@ export declare function Param(name: string): ParameterDecorator & PropertyDecora
173
348
  export declare function Params(): ParameterDecorator & PropertyDecorator;
174
349
 
175
350
  /**
351
+ * ## Provide
352
+ * ### @Decorator
176
353
  * Defines provide registry for class (and all the children)
177
354
  * @param type - string or class constructor
178
355
  * @param fn - factory function for provided value
179
356
  */
180
357
  export declare function Provide(type: string | TClassConstructor, fn: TProvideFn): ClassDecorator;
181
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
+ */
182
368
  declare function Required_2(): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
183
369
  export { Required_2 as Required }
184
370
 
@@ -191,7 +377,9 @@ export { Required_2 as Required }
191
377
  */
192
378
  export declare function Resolve<T extends TObject = TEmpty>(resolver: (metas: TPipeMetas<T>, level: TDecoratorLevel) => unknown, label?: string): ParameterDecorator & PropertyDecorator;
193
379
 
194
- export declare const resolvePipe: TPipeFn;
380
+ export declare const resolvePipe: TPipeFn<TEmpty>;
381
+
382
+ export declare function setControllerContext<T>(controller: T, method: keyof T): void;
195
383
 
196
384
  declare type TAny = any;
197
385
 
@@ -214,6 +402,8 @@ declare interface TClassFunctionConstructor<T extends TAnyFn = TAnyFn> {
214
402
  declare interface TCommonMetaFields {
215
403
  id?: string;
216
404
  label?: string;
405
+ value?: unknown;
406
+ description?: string;
217
407
  optional?: boolean;
218
408
  required?: boolean;
219
409
  }
@@ -245,13 +435,15 @@ export declare type TInterceptorAfter = (response: TAny, reply: (response: TAny)
245
435
 
246
436
  export declare type TInterceptorBefore = (reply: (response: TAny) => void) => void | Promise<void>;
247
437
 
438
+ export declare type TInterceptorClass = TClassFunction<TInterceptorFn>;
439
+
248
440
  export declare interface TInterceptorData {
249
441
  handler: TCallableClassFunction<TInterceptorFn>;
250
442
  priority: TInterceptorPriority;
251
443
  }
252
444
 
253
445
  export declare type TInterceptorFn = {
254
- (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>;
255
447
  priority?: TInterceptorPriority;
256
448
  };
257
449
 
@@ -267,25 +459,51 @@ export declare enum TInterceptorPriority {
267
459
  AFTER_ALL = 6
268
460
  }
269
461
 
270
- export declare interface TMoostAdapter<H extends object> {
462
+ export declare interface TMoostAdapter<H> {
271
463
  bindHandler<T extends TObject = TObject>(options: TMoostAdapterOptions<H, T>): void | Promise<void>;
272
- onInit?: () => void | Promise<void>;
464
+ onInit?: (moost: Moost) => void | Promise<void>;
273
465
  getProvideRegistry?: () => TProvideRegistry;
274
466
  }
275
467
 
276
- export declare interface TMoostAdapterOptions<H extends object, T extends object> {
468
+ export declare interface TMoostAdapterOptions<H, T> {
277
469
  prefix: string;
278
470
  fakeInstance: T;
279
471
  getInstance: () => Promise<T>;
280
472
  method: keyof T;
281
473
  handlers: TMoostHandler<H>[];
282
474
  getIterceptorHandler: () => Promise<InterceptorHandler>;
283
- registerEventScope: (scopeId: string) => () => void;
284
475
  resolveArgs: () => Promise<unknown[]>;
285
476
  logHandler: (eventName: string) => void;
286
477
  }
287
478
 
288
- 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 & {
289
507
  type: string;
290
508
  };
291
509
 
@@ -308,6 +526,9 @@ export declare interface TMoostMetadata extends TCommonMetaFields, TCommonMoostM
308
526
  }
309
527
 
310
528
  export declare interface TMoostOptions {
529
+ /**
530
+ * Prefix that is used for each event path
531
+ */
311
532
  globalPrefix?: string;
312
533
  logger?: TConsoleBase;
313
534
  }
@@ -316,6 +537,7 @@ export declare interface TMoostParamsMetadata extends TCommonMetaFields, TCommon
316
537
  circular?: () => TAny;
317
538
  inject?: string | symbol | TClassConstructor;
318
539
  nullable?: boolean;
540
+ isRouteParam?: string;
319
541
  }
320
542
 
321
543
  declare type TObject = object;
@@ -363,8 +585,24 @@ declare interface TValidatePipeOptions {
363
585
  errorCb?: (message: string, details: string | TObject) => unknown;
364
586
  }
365
587
 
588
+ export declare function useControllerContext<T extends object>(): {
589
+ getController: () => T;
590
+ getMethod: () => keyof T;
591
+ getControllerMeta: () => Partial<TMoostMetadata & TEmpty & TMateClassMeta & TMatePropMeta<TMoostMetadata & TMoostParamsMetadata & TEmpty> & TMoostParamsMetadata & TMateParamMeta> | undefined;
592
+ getMethodMeta: () => Partial<TMoostMetadata & TEmpty & TMateClassMeta & TMatePropMeta<TMoostMetadata & TMoostParamsMetadata & TEmpty> & TMoostParamsMetadata & TMateParamMeta> | undefined;
593
+ };
594
+
366
595
  export declare function Validate<T = unknown>(validator: TValidoFn<T>): PropertyDecorator & ParameterDecorator;
367
596
 
368
597
  export declare const validatePipe: (opts?: TValidatePipeOptions) => TPipeFn;
369
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
+
370
608
  export { }