moost 0.5.33 → 0.6.1
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/README.md +21 -0
- package/dist/index.cjs +743 -271
- package/dist/index.d.ts +298 -116
- package/dist/index.mjs +680 -254
- package/package.json +39 -33
- package/scripts/setup-skills.js +76 -0
- package/skills/moost/SKILL.md +34 -0
- package/skills/moost/core.md +174 -0
- package/skills/moost/custom-adapters.md +744 -0
- package/skills/moost/decorators.md +185 -0
- package/skills/moost/di.md +161 -0
- package/skills/moost/interceptors.md +199 -0
- package/skills/moost/pipes.md +213 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,53 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import * as _wooksjs_event_core from '@wooksjs/event-core';
|
|
2
|
+
import { EventContext, Logger } from '@wooksjs/event-core';
|
|
3
|
+
export { Cached, ContextInjector, EventContext, EventContextOptions, Key, Logger, cached, createEventContext, current, eventTypeKey, getContextInjector, key, replaceContextInjector, resetContextInjector, run, useLogger } from '@wooksjs/event-core';
|
|
3
4
|
import { TProvideRegistry, Infact, TReplaceRegistry, TProvideFn } from '@prostojs/infact';
|
|
4
5
|
export { TProvideRegistry, createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
|
|
6
|
+
import * as _prostojs_mate from '@prostojs/mate';
|
|
7
|
+
import { TMateParamMeta, Mate } from '@prostojs/mate';
|
|
8
|
+
export { Mate, TMateParamMeta, getConstructor, isConstructor } from '@prostojs/mate';
|
|
5
9
|
import * as _prostojs_logger from '@prostojs/logger';
|
|
6
10
|
import { TConsoleBase, TProstoLoggerOptions, ProstoLogger } from '@prostojs/logger';
|
|
7
11
|
export { ProstoLogger, TConsoleBase } from '@prostojs/logger';
|
|
8
12
|
import { Hookable } from 'hookable';
|
|
9
|
-
import * as _prostojs_mate from '@prostojs/mate';
|
|
10
|
-
import { TMateParamMeta, Mate } from '@prostojs/mate';
|
|
11
|
-
export { Mate, TMateParamMeta, getConstructor, isConstructor } from '@prostojs/mate';
|
|
12
13
|
export { clearGlobalWooks, getGlobalWooks } from 'wooks';
|
|
13
14
|
|
|
15
|
+
/** Escape-hatch type alias for `any`. */
|
|
14
16
|
type TAny = any;
|
|
17
|
+
/** Generic function signature accepting any arguments. */
|
|
15
18
|
type TAnyFn = (...a: TAny[]) => unknown;
|
|
19
|
+
/** Alias for `object`. */
|
|
16
20
|
type TObject = object;
|
|
21
|
+
/** Alias for `Function`. */
|
|
17
22
|
type TFunction = Function;
|
|
23
|
+
/** Generic class constructor type. */
|
|
18
24
|
type TClassConstructor<T = unknown> = new (...args: TAny[]) => T;
|
|
25
|
+
/** Empty object interface used as a default generic constraint. */
|
|
19
26
|
interface TEmpty {
|
|
20
27
|
}
|
|
28
|
+
/** Union of primitive type name strings (mirrors `typeof` results). */
|
|
29
|
+
type TPrimitives = 'undefined' | 'boolean' | 'number' | 'bigint' | 'string' | 'symbol';
|
|
30
|
+
/** Minimal logger interface required by Moost internals. */
|
|
31
|
+
interface TLogger {
|
|
32
|
+
error: (...args: unknown[]) => void;
|
|
33
|
+
warn: (...args: unknown[]) => void;
|
|
34
|
+
log: (...args: unknown[]) => void;
|
|
35
|
+
info: (...args: unknown[]) => void;
|
|
36
|
+
}
|
|
21
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Marks a constructor parameter dependency as circular.
|
|
40
|
+
* The resolver function is called lazily to break the circular reference.
|
|
41
|
+
*
|
|
42
|
+
* @param resolver - Factory that returns the class constructor (e.g. `() => MyService`).
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* class ServiceA {
|
|
47
|
+
* constructor(@Circular(() => ServiceB) private b: ServiceB) {}
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
22
51
|
declare function Circular<T = unknown>(resolver: () => TClassConstructor<T>): ParameterDecorator;
|
|
23
52
|
|
|
24
53
|
/**
|
|
@@ -110,17 +139,33 @@ declare function ImportController(prefix: string, controller: TFunction | TObjec
|
|
|
110
139
|
*/
|
|
111
140
|
declare const Inherit: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
112
141
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
142
|
+
type TInterceptorBeforeFn = (reply: (response: TAny) => void) => void | Promise<void>;
|
|
143
|
+
type TInterceptorAfterFn = (response: TAny, reply: (response: TAny) => void) => void | Promise<void>;
|
|
144
|
+
type TInterceptorErrorFn = (error: Error, reply: (response: TAny) => void) => void | Promise<void>;
|
|
145
|
+
/**
|
|
146
|
+
* Object-based interceptor definition.
|
|
147
|
+
*
|
|
148
|
+
* Declares lifecycle hooks directly instead of registering callbacks via init function.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* const myInterceptor: TInterceptorDef = {
|
|
153
|
+
* before(reply) {
|
|
154
|
+
* if (!isAuthenticated()) reply(new HttpError(401))
|
|
155
|
+
* },
|
|
156
|
+
* after(response, reply) {
|
|
157
|
+
* reply(transform(response))
|
|
158
|
+
* },
|
|
159
|
+
* error(error, reply) {
|
|
160
|
+
* reply(formatError(error))
|
|
161
|
+
* },
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
interface TInterceptorDef {
|
|
166
|
+
before?: TInterceptorBeforeFn;
|
|
167
|
+
after?: TInterceptorAfterFn;
|
|
168
|
+
error?: TInterceptorErrorFn;
|
|
124
169
|
priority?: TInterceptorPriority;
|
|
125
170
|
_name?: string;
|
|
126
171
|
}
|
|
@@ -136,12 +181,12 @@ declare enum TInterceptorPriority {
|
|
|
136
181
|
/**
|
|
137
182
|
* ## Intercept
|
|
138
183
|
* ### @Decorator
|
|
139
|
-
*
|
|
140
|
-
* @param handler
|
|
141
|
-
* @param priority interceptor priority
|
|
142
|
-
* @
|
|
184
|
+
* Attach an interceptor to a class or method.
|
|
185
|
+
* @param handler — @Interceptor class constructor or TInterceptorDef object
|
|
186
|
+
* @param priority — interceptor priority (overrides handler's own priority)
|
|
187
|
+
* @param name — interceptor name for tracing
|
|
143
188
|
*/
|
|
144
|
-
declare function Intercept(handler:
|
|
189
|
+
declare function Intercept(handler: TClassConstructor | TInterceptorDef, priority?: TInterceptorPriority, name?: string): ClassDecorator & MethodDecorator;
|
|
145
190
|
|
|
146
191
|
type TDecoratorLevel = 'CLASS' | 'METHOD' | 'PROP' | 'PARAM';
|
|
147
192
|
|
|
@@ -150,7 +195,9 @@ interface TInfactLoggingOptions {
|
|
|
150
195
|
warn?: true | false;
|
|
151
196
|
error?: true | false;
|
|
152
197
|
}
|
|
198
|
+
/** Configures which Infact DI events are logged (instance creation, warnings, errors). */
|
|
153
199
|
declare function setInfactLoggingOptions(options: TInfactLoggingOptions): void;
|
|
200
|
+
/** Returns the shared Infact DI container used by Moost for dependency injection. */
|
|
154
201
|
declare function getMoostInfact(): Infact<TMoostMetadata<TEmpty>, TMoostMetadata<TEmpty>, TMoostParamsMetadata, TCustom>;
|
|
155
202
|
interface TCustom {
|
|
156
203
|
pipes?: TPipeData[];
|
|
@@ -174,6 +221,7 @@ declare function getInfactScopeVars<T extends object>(name: string | symbol): T
|
|
|
174
221
|
*/
|
|
175
222
|
declare function getNewMoostInfact(): Infact<TMoostMetadata<TEmpty>, TMoostMetadata<TEmpty>, TMoostParamsMetadata, TCustom>;
|
|
176
223
|
|
|
224
|
+
/** Metadata context passed to pipe functions during argument/property resolution. */
|
|
177
225
|
interface TPipeMetas<T extends TObject = TEmpty> {
|
|
178
226
|
classMeta?: TMoostMetadata & T;
|
|
179
227
|
methodMeta?: TMoostMetadata & T;
|
|
@@ -187,10 +235,12 @@ interface TPipeMetas<T extends TObject = TEmpty> {
|
|
|
187
235
|
key: string | symbol;
|
|
188
236
|
instantiate: <T extends TObject>(t: TClassConstructor<T>) => Promise<T>;
|
|
189
237
|
}
|
|
238
|
+
/** A pipe function that transforms, resolves, or validates a value during the pipe pipeline. */
|
|
190
239
|
interface TPipeFn<T extends TObject = TEmpty> {
|
|
191
240
|
(value: unknown, metas: TPipeMetas<T>, level: TDecoratorLevel): unknown | Promise<unknown>;
|
|
192
241
|
priority?: TPipePriority;
|
|
193
242
|
}
|
|
243
|
+
/** Execution priority for pipes. Lower values run first. */
|
|
194
244
|
declare enum TPipePriority {
|
|
195
245
|
BEFORE_RESOLVE = 0,
|
|
196
246
|
RESOLVE = 1,
|
|
@@ -202,6 +252,7 @@ declare enum TPipePriority {
|
|
|
202
252
|
VALIDATE = 7,
|
|
203
253
|
AFTER_VALIDATE = 8
|
|
204
254
|
}
|
|
255
|
+
/** A pipe entry pairing a handler function with its execution priority. */
|
|
205
256
|
interface TPipeData {
|
|
206
257
|
handler: TPipeFn;
|
|
207
258
|
priority: TPipePriority;
|
|
@@ -209,26 +260,32 @@ interface TPipeData {
|
|
|
209
260
|
|
|
210
261
|
declare const resolvePipe: TPipeFn<TEmpty>;
|
|
211
262
|
|
|
263
|
+
/** Core metadata shape attached to classes, methods, and properties by Moost decorators. */
|
|
212
264
|
interface TMoostMetadata<H extends TObject = TEmpty> extends TCommonMetaFields, TCommonMoostMeta {
|
|
213
|
-
requiredProps?:
|
|
265
|
+
requiredProps?: (string | symbol)[];
|
|
214
266
|
controller?: {
|
|
215
267
|
prefix?: string;
|
|
216
268
|
};
|
|
217
|
-
importController?:
|
|
269
|
+
importController?: {
|
|
218
270
|
prefix?: string;
|
|
219
271
|
typeResolver?: TClassConstructor | (() => TClassConstructor | TObject | Promise<TClassConstructor | TObject>);
|
|
220
272
|
provide?: TProvideRegistry;
|
|
221
|
-
}
|
|
222
|
-
properties?:
|
|
273
|
+
}[];
|
|
274
|
+
properties?: (string | symbol)[];
|
|
223
275
|
injectable?: true | TInjectableScope;
|
|
276
|
+
interceptor?: {
|
|
277
|
+
priority: TInterceptorPriority;
|
|
278
|
+
};
|
|
279
|
+
interceptorHook?: 'before' | 'after' | 'error';
|
|
224
280
|
interceptors?: TInterceptorData[];
|
|
225
|
-
handlers?:
|
|
281
|
+
handlers?: TMoostHandler<H>[];
|
|
226
282
|
returnType?: TFunction;
|
|
227
283
|
provide?: TProvideRegistry;
|
|
228
284
|
replace?: TReplaceRegistry;
|
|
229
285
|
loggerTopic?: string;
|
|
230
|
-
params:
|
|
286
|
+
params: (TMateParamMeta & TMoostParamsMetadata)[];
|
|
231
287
|
}
|
|
288
|
+
/** Metadata attached to constructor/method parameters by Moost decorators. */
|
|
232
289
|
interface TMoostParamsMetadata extends TCommonMetaFields, TCommonMoostMeta {
|
|
233
290
|
circular?: () => TAny;
|
|
234
291
|
inject?: string | symbol | TClassConstructor;
|
|
@@ -237,20 +294,24 @@ interface TMoostParamsMetadata extends TCommonMetaFields, TCommonMoostMeta {
|
|
|
237
294
|
paramName?: string;
|
|
238
295
|
fromScope?: string | symbol;
|
|
239
296
|
}
|
|
297
|
+
/** DI scope for injectable classes: per-event or application-wide singleton. */
|
|
240
298
|
type TInjectableScope = 'FOR_EVENT' | 'SINGLETON';
|
|
299
|
+
/** Describes a registered handler (HTTP route, CLI command, workflow step, etc.). */
|
|
241
300
|
type TMoostHandler<T> = {
|
|
242
301
|
type: string;
|
|
243
302
|
path?: string;
|
|
244
303
|
} & T;
|
|
304
|
+
/** Stored interceptor entry attached to a class or method via `@Intercept`. */
|
|
245
305
|
interface TInterceptorData {
|
|
246
|
-
handler:
|
|
306
|
+
handler: TClassConstructor | TInterceptorDef;
|
|
247
307
|
priority: TInterceptorPriority;
|
|
248
308
|
name: string;
|
|
249
309
|
}
|
|
310
|
+
/** Returns the shared `Mate` instance operating in the `'moost'` metadata workspace. */
|
|
250
311
|
declare function getMoostMate<Class extends TObject = TEmpty, Prop extends TObject = TEmpty, Param extends TObject = TEmpty>(): Mate<TMoostMetadata & Class & {
|
|
251
|
-
params:
|
|
312
|
+
params: (Param & TMateParamMeta)[];
|
|
252
313
|
}, TMoostMetadata & Prop & {
|
|
253
|
-
params:
|
|
314
|
+
params: (Param & TMateParamMeta)[];
|
|
254
315
|
}>;
|
|
255
316
|
interface TCommonMetaFields {
|
|
256
317
|
id?: string;
|
|
@@ -278,10 +339,75 @@ interface TCommonMoostMeta {
|
|
|
278
339
|
*/
|
|
279
340
|
declare function Injectable(scope?: true | TInjectableScope): ClassDecorator;
|
|
280
341
|
|
|
342
|
+
/** Callback used by interceptors to short-circuit and replace the handler response. */
|
|
343
|
+
type TOvertakeFn = (response: TAny) => void;
|
|
344
|
+
/** Stores the overtake (reply) function in the current event context. */
|
|
345
|
+
declare function setOvertake(fn: TOvertakeFn, ctx?: EventContext): void;
|
|
346
|
+
/** Retrieves the overtake (reply) function from the current event context. */
|
|
347
|
+
declare function useOvertake(ctx?: EventContext): TOvertakeFn;
|
|
348
|
+
/** Stores the interceptor result value in the current event context. */
|
|
349
|
+
declare function setInterceptResult(value: unknown, ctx?: EventContext): void;
|
|
350
|
+
/** Retrieves the interceptor result value from the current event context. */
|
|
351
|
+
declare function useInterceptResult<T = unknown>(ctx?: EventContext): T;
|
|
352
|
+
|
|
353
|
+
type TInterceptorDefFactory = () => TInterceptorDef | Promise<TInterceptorDef>;
|
|
354
|
+
/**
|
|
355
|
+
* ## @Interceptor
|
|
356
|
+
* ### Class Decorator
|
|
357
|
+
* Marks a class as a decorator-based interceptor.
|
|
358
|
+
* Auto-adds @Injectable() if not already present.
|
|
359
|
+
*
|
|
360
|
+
* Use @Before(), @After(), and @OnError() method decorators
|
|
361
|
+
* to register lifecycle hooks.
|
|
362
|
+
*
|
|
363
|
+
* @param priority — interceptor priority (default: INTERCEPTOR)
|
|
364
|
+
* @param scope — DI scope ('FOR_EVENT' | 'SINGLETON' | true)
|
|
365
|
+
*/
|
|
366
|
+
declare function Interceptor(priority?: TInterceptorPriority, scope?: true | TInjectableScope): ClassDecorator;
|
|
367
|
+
/**
|
|
368
|
+
* ## @Before
|
|
369
|
+
* ### Method Decorator
|
|
370
|
+
* Marks a method as a before-phase interceptor hook.
|
|
371
|
+
* Runs before argument resolution and handler execution.
|
|
372
|
+
* Use @Overtake() param to get the reply fn for short-circuiting.
|
|
373
|
+
*/
|
|
374
|
+
declare function Before(): MethodDecorator;
|
|
375
|
+
/**
|
|
376
|
+
* ## @After
|
|
377
|
+
* ### Method Decorator
|
|
378
|
+
* Marks a method as an after-phase interceptor hook.
|
|
379
|
+
* Runs after successful handler execution.
|
|
380
|
+
* Use @Response() param for the handler result, @Overtake() for the reply fn.
|
|
381
|
+
*/
|
|
382
|
+
declare function After(): MethodDecorator;
|
|
383
|
+
/**
|
|
384
|
+
* ## @OnError
|
|
385
|
+
* ### Method Decorator
|
|
386
|
+
* Marks a method as an error-phase interceptor hook.
|
|
387
|
+
* Runs when the handler throws or returns an Error.
|
|
388
|
+
* Use @Response() param for the error, @Overtake() for the reply fn.
|
|
389
|
+
*/
|
|
390
|
+
declare function OnError(): MethodDecorator;
|
|
391
|
+
/**
|
|
392
|
+
* ## @Overtake
|
|
393
|
+
* ### Parameter Decorator
|
|
394
|
+
* Injects the reply function into an interceptor method parameter.
|
|
395
|
+
* Call the reply fn to short-circuit the handler or replace the response.
|
|
396
|
+
*/
|
|
397
|
+
declare function Overtake(): ParameterDecorator;
|
|
398
|
+
/**
|
|
399
|
+
* ## @Response
|
|
400
|
+
* ### Parameter Decorator
|
|
401
|
+
* Injects the handler result into an interceptor method parameter.
|
|
402
|
+
* In @After methods, this is the successful response.
|
|
403
|
+
* In @OnError methods, this is the Error that was thrown.
|
|
404
|
+
*/
|
|
405
|
+
declare function Response(): ParameterDecorator;
|
|
406
|
+
|
|
281
407
|
/**
|
|
282
408
|
* Resolves event logger from event context
|
|
283
409
|
* @param topic
|
|
284
|
-
* @returns Resolver to '@wooksjs/event-core' (
|
|
410
|
+
* @returns Resolver to '@wooksjs/event-core' (Logger)
|
|
285
411
|
*/
|
|
286
412
|
declare function InjectEventLogger(topic?: string): ParameterDecorator & PropertyDecorator;
|
|
287
413
|
/**
|
|
@@ -382,54 +508,66 @@ declare function Const<T>(value: T, label?: string): ParameterDecorator & Proper
|
|
|
382
508
|
*/
|
|
383
509
|
declare function ConstFactory<T>(factory: () => T | Promise<T>, label?: string): ParameterDecorator & PropertyDecorator;
|
|
384
510
|
|
|
511
|
+
/** Entry for a single interceptor in the handler chain. */
|
|
512
|
+
interface TInterceptorEntry {
|
|
513
|
+
handler: TInterceptorDef | TInterceptorDefFactory;
|
|
514
|
+
name: string;
|
|
515
|
+
/** Pre-computed span name for ci.with(). */
|
|
516
|
+
spanName: string;
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Manages the before/after/error interceptor lifecycle for a single event.
|
|
520
|
+
* Optimised for the common sync path — only allocates promises when an interceptor goes async.
|
|
521
|
+
*/
|
|
385
522
|
declare class InterceptorHandler {
|
|
386
|
-
protected handlers:
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
}>;
|
|
390
|
-
constructor(handlers: Array<{
|
|
391
|
-
handler: TInterceptorFn;
|
|
523
|
+
protected handlers: TInterceptorEntry[];
|
|
524
|
+
constructor(handlers: TInterceptorEntry[]);
|
|
525
|
+
protected after: {
|
|
392
526
|
name: string;
|
|
393
|
-
|
|
394
|
-
|
|
527
|
+
fn: TInterceptorAfterFn;
|
|
528
|
+
}[] | undefined;
|
|
529
|
+
protected onError: {
|
|
395
530
|
name: string;
|
|
396
|
-
fn:
|
|
397
|
-
}
|
|
398
|
-
protected after: Array<{
|
|
399
|
-
name: string;
|
|
400
|
-
fn: TInterceptorAfter;
|
|
401
|
-
}>;
|
|
402
|
-
protected onError: Array<{
|
|
403
|
-
name: string;
|
|
404
|
-
fn: TInterceptorOnError;
|
|
405
|
-
}>;
|
|
531
|
+
fn: TInterceptorErrorFn;
|
|
532
|
+
}[] | undefined;
|
|
406
533
|
response?: unknown;
|
|
407
534
|
responseOverwritten: boolean;
|
|
535
|
+
protected _boundReplyFn?: (reply: unknown) => void;
|
|
536
|
+
protected getReplyFn(): (reply: unknown) => void;
|
|
408
537
|
get count(): number;
|
|
409
|
-
get countBefore(): number;
|
|
410
538
|
get countAfter(): number;
|
|
411
539
|
get countOnError(): number;
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
540
|
+
/**
|
|
541
|
+
* Register hooks from a TInterceptorDef.
|
|
542
|
+
* Returns a pending PromiseLike if `before` went async, or undefined.
|
|
543
|
+
*/
|
|
544
|
+
private registerDef;
|
|
545
|
+
before(): unknown;
|
|
546
|
+
private _beforeAsyncFactory;
|
|
547
|
+
private _beforeAsyncPending;
|
|
548
|
+
private _beforeFrom;
|
|
549
|
+
fireAfter(response: unknown): unknown;
|
|
550
|
+
private _fireAfterAsync;
|
|
416
551
|
}
|
|
417
552
|
|
|
553
|
+
/** Options passed to init/end hooks during the event handler lifecycle. */
|
|
418
554
|
interface TMoostEventHandlerHookOptions<T> {
|
|
419
555
|
scopeId: string;
|
|
420
|
-
logger:
|
|
556
|
+
logger: Logger;
|
|
421
557
|
unscope: () => void;
|
|
422
558
|
instance?: T;
|
|
423
559
|
method?: keyof T;
|
|
424
560
|
getResponse: () => unknown;
|
|
425
561
|
reply: (r: unknown) => void;
|
|
426
562
|
}
|
|
563
|
+
/** Configuration for `defineMoostEventHandler`, describing how to resolve and invoke a handler. */
|
|
427
564
|
interface TMoostEventHandlerOptions<T> {
|
|
428
565
|
contextType?: string | string[];
|
|
429
566
|
loggerTitle: string;
|
|
430
|
-
getIterceptorHandler: () =>
|
|
567
|
+
getIterceptorHandler: () => InterceptorHandler | undefined;
|
|
431
568
|
getControllerInstance: () => Promise<T> | T | undefined;
|
|
432
569
|
controllerMethod?: keyof T;
|
|
570
|
+
controllerName?: string;
|
|
433
571
|
callControllerMethod?: (args: unknown[]) => unknown;
|
|
434
572
|
resolveArgs?: () => Promise<unknown[]> | unknown[];
|
|
435
573
|
logErrors?: boolean;
|
|
@@ -441,29 +579,55 @@ interface TMoostEventHandlerOptions<T> {
|
|
|
441
579
|
targetPath: string;
|
|
442
580
|
handlerType: string;
|
|
443
581
|
}
|
|
582
|
+
/** Composable returning the moost scope ID for the current event. Generates on first call, caches for subsequent calls. */
|
|
583
|
+
declare const useScopeId: (ctx?: _wooksjs_event_core.EventContext) => string;
|
|
584
|
+
/** Registers a DI scope for the given ID and returns an unscope function. */
|
|
444
585
|
declare function registerEventScope(scopeId: string): () => void;
|
|
445
|
-
|
|
586
|
+
/**
|
|
587
|
+
* Creates the complete event handler lifecycle processor used by adapters.
|
|
588
|
+
* Handles scope registration, controller resolution, interceptors, argument resolution,
|
|
589
|
+
* handler invocation, and cleanup — optimised for sync-first execution.
|
|
590
|
+
*/
|
|
591
|
+
declare function defineMoostEventHandler<T>(options: TMoostEventHandlerOptions<T>): () => unknown;
|
|
592
|
+
|
|
593
|
+
/** Returns the own method names of an instance, including inherited methods from parent classes. */
|
|
594
|
+
declare function getInstanceOwnMethods<T = TAny>(instance: T): (keyof T)[];
|
|
595
|
+
/** Returns the own non-method property names of an instance, including inherited properties. */
|
|
596
|
+
declare function getInstanceOwnProps<T = TAny>(instance: T): (keyof T)[];
|
|
446
597
|
|
|
447
|
-
|
|
448
|
-
|
|
598
|
+
/**
|
|
599
|
+
* Checks whether a value is a PromiseLike (thenable).
|
|
600
|
+
* Single source of truth — import from here instead of duplicating.
|
|
601
|
+
*/
|
|
602
|
+
declare function isThenable(value: unknown): value is PromiseLike<unknown>;
|
|
603
|
+
/**
|
|
604
|
+
* Merges multiple arrays of items with a `priority` field, returning a
|
|
605
|
+
* single sorted-by-priority array. Accepts `undefined` entries for convenience.
|
|
606
|
+
*/
|
|
607
|
+
declare function mergeSorted<T extends {
|
|
608
|
+
priority: number;
|
|
609
|
+
}>(...lists: (T[] | readonly T[] | undefined)[]): T[];
|
|
449
610
|
|
|
611
|
+
/** Summary of a registered controller, its metadata, prefix, and handlers. */
|
|
450
612
|
interface TControllerOverview {
|
|
451
613
|
meta: TMoostMetadata;
|
|
452
614
|
computedPrefix: string;
|
|
453
615
|
type: TFunction;
|
|
454
616
|
handlers: THandlerOverview[];
|
|
455
617
|
}
|
|
618
|
+
/** Summary of a registered handler method within a controller. */
|
|
456
619
|
interface THandlerOverview {
|
|
457
620
|
meta: TMoostMetadata;
|
|
458
621
|
path?: string;
|
|
459
622
|
type: string;
|
|
460
623
|
method: string;
|
|
461
624
|
handler: TMoostHandler<TEmpty>;
|
|
462
|
-
registeredAs:
|
|
625
|
+
registeredAs: {
|
|
463
626
|
path: string;
|
|
464
627
|
args: string[];
|
|
465
|
-
}
|
|
628
|
+
}[];
|
|
466
629
|
}
|
|
630
|
+
/** Hook points in the event handler lifecycle where context injectors are invoked. */
|
|
467
631
|
type TContextInjectorHook = 'Event:start' | 'Interceptors:init' | 'Arguments:resolve' | 'Interceptors:before' | 'Handler' | 'Interceptors:after';
|
|
468
632
|
|
|
469
633
|
interface TMoostOptions {
|
|
@@ -528,11 +692,11 @@ declare class Moost extends Hookable {
|
|
|
528
692
|
protected logger: TConsoleBase;
|
|
529
693
|
protected pipes: TPipeData[];
|
|
530
694
|
protected interceptors: TInterceptorData[];
|
|
531
|
-
protected adapters:
|
|
695
|
+
protected adapters: TMoostAdapter<TAny>[];
|
|
532
696
|
protected controllersOverview: TControllerOverview[];
|
|
533
697
|
protected provide: TProvideRegistry;
|
|
534
698
|
protected replace: TReplaceRegistry;
|
|
535
|
-
protected unregisteredControllers:
|
|
699
|
+
protected unregisteredControllers: (TObject | TFunction | [string, TObject | TFunction])[];
|
|
536
700
|
constructor(options?: TMoostOptions | undefined);
|
|
537
701
|
_fireEventStart(source: TMoostAdapter<unknown>): void;
|
|
538
702
|
_fireEventEnd(source: TMoostAdapter<unknown>): void;
|
|
@@ -557,16 +721,16 @@ declare class Moost extends Hookable {
|
|
|
557
721
|
init(): Promise<void>;
|
|
558
722
|
protected bindControllers(): Promise<void>;
|
|
559
723
|
protected bindController(controller: TFunction | TObject, provide: TProvideRegistry, replace: TReplaceRegistry, globalPrefix: string, replaceOwnPrefix?: string): Promise<void>;
|
|
560
|
-
applyGlobalPipes(...items:
|
|
561
|
-
protected globalInterceptorHandler?: () =>
|
|
724
|
+
applyGlobalPipes(...items: (TPipeFn | TPipeData)[]): this;
|
|
725
|
+
protected globalInterceptorHandler?: () => InterceptorHandler | undefined;
|
|
562
726
|
/**
|
|
563
727
|
* Provides InterceptorHandler with global interceptors and pipes.
|
|
564
728
|
* Used to process interceptors when event handler was not found.
|
|
565
729
|
*
|
|
566
730
|
* @returns IterceptorHandler
|
|
567
731
|
*/
|
|
568
|
-
getGlobalInterceptorHandler():
|
|
569
|
-
applyGlobalInterceptors(...items:
|
|
732
|
+
getGlobalInterceptorHandler(): InterceptorHandler | undefined;
|
|
733
|
+
applyGlobalInterceptors(...items: (TClassConstructor | TInterceptorDef | TInterceptorData)[]): this;
|
|
570
734
|
/**
|
|
571
735
|
* Register new entries to provide as dependency injections
|
|
572
736
|
* @param provide - Provide Registry (use createProvideRegistry from '\@prostojs/infact')
|
|
@@ -584,17 +748,18 @@ declare class Moost extends Hookable {
|
|
|
584
748
|
* @param controllers - list of target controllers (instances)
|
|
585
749
|
* @returns
|
|
586
750
|
*/
|
|
587
|
-
registerControllers(...controllers:
|
|
751
|
+
registerControllers(...controllers: (TObject | TFunction | [string, TObject | TFunction])[]): this;
|
|
588
752
|
logMappedHandler(eventName: string, classConstructor: Function, method: string, stroke?: boolean, prefix?: string): void;
|
|
589
753
|
}
|
|
590
754
|
interface TMoostAdapterOptions<H, T> {
|
|
591
755
|
prefix: string;
|
|
592
756
|
fakeInstance: T;
|
|
593
|
-
getInstance: () => Promise<T
|
|
757
|
+
getInstance: () => Promise<T> | T;
|
|
594
758
|
method: keyof T;
|
|
595
|
-
handlers:
|
|
596
|
-
getIterceptorHandler: () =>
|
|
597
|
-
resolveArgs
|
|
759
|
+
handlers: TMoostHandler<H>[];
|
|
760
|
+
getIterceptorHandler: () => InterceptorHandler | undefined;
|
|
761
|
+
resolveArgs?: () => Promise<unknown[]> | unknown[];
|
|
762
|
+
controllerName?: string;
|
|
598
763
|
logHandler: (eventName: string) => void;
|
|
599
764
|
register: (handler: TMoostHandler<TEmpty>, path: string, args: string[]) => void;
|
|
600
765
|
}
|
|
@@ -605,10 +770,18 @@ interface TMoostAdapter<H> {
|
|
|
605
770
|
getProvideRegistry?: () => TProvideRegistry;
|
|
606
771
|
}
|
|
607
772
|
|
|
608
|
-
|
|
609
|
-
|
|
773
|
+
/**
|
|
774
|
+
* Sets the controller context for the current event scope.
|
|
775
|
+
* Called internally by adapters when dispatching events to handlers.
|
|
776
|
+
*/
|
|
777
|
+
declare function setControllerContext<T>(controller: T, method: keyof T, route: string, ctx?: EventContext): void;
|
|
778
|
+
/**
|
|
779
|
+
* Provides access to the current controller context within an event handler.
|
|
780
|
+
* Returns utilities for accessing the controller instance, method metadata, and DI.
|
|
781
|
+
*/
|
|
782
|
+
declare function useControllerContext<T extends object>(ctx?: EventContext): {
|
|
610
783
|
instantiate: <TT>(c: TClassConstructor<TT>) => Promise<TT>;
|
|
611
|
-
getRoute: () => string
|
|
784
|
+
getRoute: () => string;
|
|
612
785
|
getController: () => T;
|
|
613
786
|
getMethod: () => string | undefined;
|
|
614
787
|
getControllerMeta: <TT extends object>() => (TMoostMetadata<TEmpty> & TT & {
|
|
@@ -634,53 +807,60 @@ declare function useControllerContext<T extends object>(): {
|
|
|
634
807
|
};
|
|
635
808
|
|
|
636
809
|
/**
|
|
637
|
-
*
|
|
810
|
+
* Define a before-phase interceptor.
|
|
811
|
+
*
|
|
812
|
+
* Runs before argument resolution and handler execution.
|
|
813
|
+
* Call `reply(value)` to short-circuit the handler and respond early.
|
|
638
814
|
*
|
|
815
|
+
* @example
|
|
639
816
|
* ```ts
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
*
|
|
643
|
-
* // before handler
|
|
644
|
-
* })
|
|
645
|
-
* after((response, reply) => {
|
|
646
|
-
* // after handler
|
|
647
|
-
* })
|
|
648
|
-
* onError((error, reply) => {
|
|
649
|
-
* // when error occured
|
|
650
|
-
* })
|
|
651
|
-
* },
|
|
652
|
-
* TInterceptorPriority.INTERCEPTOR,
|
|
653
|
-
* )
|
|
817
|
+
* const authGuard = defineBeforeInterceptor((reply) => {
|
|
818
|
+
* if (!isAuthenticated()) reply(new HttpError(401))
|
|
819
|
+
* }, TInterceptorPriority.GUARD)
|
|
654
820
|
* ```
|
|
821
|
+
*/
|
|
822
|
+
declare function defineBeforeInterceptor(fn: TInterceptorBeforeFn, priority?: TInterceptorPriority): TInterceptorDef;
|
|
823
|
+
/**
|
|
824
|
+
* Define an after-phase interceptor.
|
|
655
825
|
*
|
|
656
|
-
*
|
|
657
|
-
*
|
|
658
|
-
*
|
|
826
|
+
* Runs after successful handler execution.
|
|
827
|
+
* Call `reply(value)` to transform/replace the response.
|
|
828
|
+
*
|
|
829
|
+
* @example
|
|
830
|
+
* ```ts
|
|
831
|
+
* const setHeader = defineAfterInterceptor(() => {
|
|
832
|
+
* useResponse().setHeader('x-server', 'my-server')
|
|
833
|
+
* }, TInterceptorPriority.AFTER_ALL)
|
|
834
|
+
* ```
|
|
659
835
|
*/
|
|
660
|
-
declare function
|
|
836
|
+
declare function defineAfterInterceptor(fn: TInterceptorAfterFn, priority?: TInterceptorPriority): TInterceptorDef;
|
|
661
837
|
/**
|
|
662
|
-
*
|
|
838
|
+
* Define an error-phase interceptor.
|
|
663
839
|
*
|
|
664
|
-
*
|
|
840
|
+
* Runs when the handler throws or returns an Error.
|
|
841
|
+
* Call `reply(value)` to recover from the error with a replacement response.
|
|
665
842
|
*
|
|
666
843
|
* @example
|
|
667
|
-
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
671
|
-
*
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
*
|
|
676
|
-
*
|
|
677
|
-
*
|
|
678
|
-
*
|
|
679
|
-
*
|
|
680
|
-
*
|
|
681
|
-
* }
|
|
844
|
+
* ```ts
|
|
845
|
+
* const errorFormatter = defineErrorInterceptor((error, reply) => {
|
|
846
|
+
* reply({ message: error.message, status: 500 })
|
|
847
|
+
* }, TInterceptorPriority.CATCH_ERROR)
|
|
848
|
+
* ```
|
|
849
|
+
*/
|
|
850
|
+
declare function defineErrorInterceptor(fn: TInterceptorErrorFn, priority?: TInterceptorPriority): TInterceptorDef;
|
|
851
|
+
/**
|
|
852
|
+
* Define a full interceptor with multiple lifecycle hooks.
|
|
853
|
+
*
|
|
854
|
+
* @example
|
|
855
|
+
* ```ts
|
|
856
|
+
* const myInterceptor = defineInterceptor({
|
|
857
|
+
* before(reply) { ... },
|
|
858
|
+
* after(response, reply) { ... },
|
|
859
|
+
* error(error, reply) { ... },
|
|
860
|
+
* }, TInterceptorPriority.INTERCEPTOR)
|
|
861
|
+
* ```
|
|
682
862
|
*/
|
|
683
|
-
|
|
863
|
+
declare function defineInterceptor(def: TInterceptorDef, priority?: TInterceptorPriority): TInterceptorDef;
|
|
684
864
|
/**
|
|
685
865
|
* ### Define Pipe Function
|
|
686
866
|
*
|
|
@@ -693,14 +873,16 @@ type TInterceptorClass = TClassFunction<TInterceptorFn>;
|
|
|
693
873
|
* )
|
|
694
874
|
* ```
|
|
695
875
|
*
|
|
696
|
-
* @param fn
|
|
697
|
-
* @param priority priority of the pipe
|
|
876
|
+
* @param fn pipe function
|
|
877
|
+
* @param priority priority of the pipe
|
|
698
878
|
* @returns
|
|
699
879
|
*/
|
|
700
880
|
declare function definePipeFn<T extends TObject = TEmpty>(fn: TPipeFn<T>, priority?: TPipePriority): TPipeFn<T>;
|
|
701
881
|
|
|
882
|
+
/** Creates a new `ProstoLogger` instance with sensible defaults (level 4, colored console). */
|
|
702
883
|
declare function createLogger(opts?: Partial<TProstoLoggerOptions>): ProstoLogger;
|
|
884
|
+
/** Default colored console transport used by Moost loggers. */
|
|
703
885
|
declare const loggerConsoleTransport: _prostojs_logger.TProstoLoggerTransportFn<any>;
|
|
704
886
|
|
|
705
|
-
export { ApplyDecorators, 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, TInterceptorPriority, TPipePriority, Value, createLogger, defineInfactScope,
|
|
706
|
-
export type {
|
|
887
|
+
export { After, ApplyDecorators, Before, Circular, Const, ConstFactory, Controller, Description, Id, ImportController, Inherit, Inject, InjectEventLogger, InjectFromScope, InjectMoostLogger, InjectScopeVars, Injectable, Intercept, Interceptor, InterceptorHandler, Label, LoggerTopic, Moost, OnError, Optional, Overtake, Param, Params, Pipe, Provide, Replace, Required, Resolve, Response, TInterceptorPriority, TPipePriority, Value, createLogger, defineAfterInterceptor, defineBeforeInterceptor, defineErrorInterceptor, defineInfactScope, defineInterceptor, defineMoostEventHandler, definePipeFn, getInfactScopeVars, getInstanceOwnMethods, getInstanceOwnProps, getMoostInfact, getMoostMate, getNewMoostInfact, isThenable, loggerConsoleTransport, mergeSorted, registerEventScope, resolvePipe, setControllerContext, setInfactLoggingOptions, setInterceptResult, setOvertake, useControllerContext, useInterceptResult, useOvertake, useScopeId };
|
|
888
|
+
export type { TAny, TAnyFn, TClassConstructor, TContextInjectorHook, TControllerOverview, TEmpty, TFunction, TInjectableScope, TInterceptorAfterFn, TInterceptorBeforeFn, TInterceptorData, TInterceptorDef, TInterceptorDefFactory, TInterceptorEntry, TInterceptorErrorFn, TLogger, TMoostAdapter, TMoostAdapterOptions, TMoostEventHandlerHookOptions, TMoostEventHandlerOptions, TMoostHandler, TMoostMetadata, TMoostOptions, TMoostParamsMetadata, TObject, TOvertakeFn, TPipeData, TPipeFn, TPipeMetas, TPrimitives };
|