@rolandsall24/nest-mediator 0.4.3 → 0.5.0
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 +492 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/behaviors/exception-handling.behavior.d.ts +46 -0
- package/dist/lib/behaviors/exception-handling.behavior.d.ts.map +1 -0
- package/dist/lib/behaviors/exception-handling.behavior.js +76 -0
- package/dist/lib/behaviors/exception-handling.behavior.js.map +1 -0
- package/dist/lib/behaviors/index.d.ts +5 -0
- package/dist/lib/behaviors/index.d.ts.map +1 -0
- package/dist/lib/behaviors/index.js +21 -0
- package/dist/lib/behaviors/index.js.map +1 -0
- package/dist/lib/behaviors/logging.behavior.d.ts +26 -0
- package/dist/lib/behaviors/logging.behavior.d.ts.map +1 -0
- package/dist/lib/behaviors/logging.behavior.js +64 -0
- package/dist/lib/behaviors/logging.behavior.js.map +1 -0
- package/dist/lib/behaviors/performance.behavior.d.ts +41 -0
- package/dist/lib/behaviors/performance.behavior.d.ts.map +1 -0
- package/dist/lib/behaviors/performance.behavior.js +71 -0
- package/dist/lib/behaviors/performance.behavior.js.map +1 -0
- package/dist/lib/behaviors/validation.behavior.d.ts +69 -0
- package/dist/lib/behaviors/validation.behavior.d.ts.map +1 -0
- package/dist/lib/behaviors/validation.behavior.js +137 -0
- package/dist/lib/behaviors/validation.behavior.js.map +1 -0
- package/dist/lib/decorators/index.d.ts +2 -0
- package/dist/lib/decorators/index.d.ts.map +1 -1
- package/dist/lib/decorators/index.js +2 -0
- package/dist/lib/decorators/index.js.map +1 -1
- package/dist/lib/decorators/pipeline-behavior.decorator.d.ts +51 -0
- package/dist/lib/decorators/pipeline-behavior.decorator.d.ts.map +1 -0
- package/dist/lib/decorators/pipeline-behavior.decorator.js +68 -0
- package/dist/lib/decorators/pipeline-behavior.decorator.js.map +1 -0
- package/dist/lib/decorators/skip-behavior.decorator.d.ts +52 -0
- package/dist/lib/decorators/skip-behavior.decorator.d.ts.map +1 -0
- package/dist/lib/decorators/skip-behavior.decorator.js +61 -0
- package/dist/lib/decorators/skip-behavior.decorator.js.map +1 -0
- package/dist/lib/exceptions/handler-not-found.exception.d.ts +20 -0
- package/dist/lib/exceptions/handler-not-found.exception.d.ts.map +1 -0
- package/dist/lib/exceptions/handler-not-found.exception.js +33 -0
- package/dist/lib/exceptions/handler-not-found.exception.js.map +1 -0
- package/dist/lib/exceptions/index.d.ts +3 -0
- package/dist/lib/exceptions/index.d.ts.map +1 -0
- package/dist/lib/exceptions/index.js +19 -0
- package/dist/lib/exceptions/index.js.map +1 -0
- package/dist/lib/exceptions/validation.exception.d.ts +62 -0
- package/dist/lib/exceptions/validation.exception.d.ts.map +1 -0
- package/dist/lib/exceptions/validation.exception.js +66 -0
- package/dist/lib/exceptions/validation.exception.js.map +1 -0
- package/dist/lib/interfaces/index.d.ts +1 -0
- package/dist/lib/interfaces/index.d.ts.map +1 -1
- package/dist/lib/interfaces/index.js +1 -0
- package/dist/lib/interfaces/index.js.map +1 -1
- package/dist/lib/interfaces/pipeline-behavior.interface.d.ts +60 -0
- package/dist/lib/interfaces/pipeline-behavior.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/pipeline-behavior.interface.js +3 -0
- package/dist/lib/interfaces/pipeline-behavior.interface.js.map +1 -0
- package/dist/lib/nest-mediator.module.d.ts +74 -3
- package/dist/lib/nest-mediator.module.d.ts.map +1 -1
- package/dist/lib/nest-mediator.module.js +66 -6
- package/dist/lib/nest-mediator.module.js.map +1 -1
- package/dist/lib/services/mediator.bus.d.ts +54 -4
- package/dist/lib/services/mediator.bus.d.ts.map +1 -1
- package/dist/lib/services/mediator.bus.js +104 -9
- package/dist/lib/services/mediator.bus.js.map +1 -1
- package/package.json +6 -2
|
@@ -10,11 +10,16 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
};
|
|
11
11
|
var NestMediatorModule_1;
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.NestMediatorModule = void 0;
|
|
13
|
+
exports.NestMediatorModule = exports.NEST_MEDIATOR_OPTIONS = void 0;
|
|
14
14
|
const common_1 = require("@nestjs/common");
|
|
15
15
|
const core_1 = require("@nestjs/core");
|
|
16
16
|
const index_js_1 = require("./services/index.js");
|
|
17
17
|
const index_js_2 = require("./decorators/index.js");
|
|
18
|
+
const index_js_3 = require("./behaviors/index.js");
|
|
19
|
+
/**
|
|
20
|
+
* Token for module options injection
|
|
21
|
+
*/
|
|
22
|
+
exports.NEST_MEDIATOR_OPTIONS = 'NEST_MEDIATOR_OPTIONS';
|
|
18
23
|
let NestMediatorModule = NestMediatorModule_1 = class NestMediatorModule {
|
|
19
24
|
constructor(mediatorBus, reflector, discoveryService) {
|
|
20
25
|
this.mediatorBus = mediatorBus;
|
|
@@ -33,31 +38,86 @@ let NestMediatorModule = NestMediatorModule_1 = class NestMediatorModule {
|
|
|
33
38
|
continue;
|
|
34
39
|
}
|
|
35
40
|
const handlerType = wrapper.metatype;
|
|
41
|
+
// Register command handlers
|
|
36
42
|
const commandMetadata = this.reflector.get(index_js_2.COMMAND_HANDLER_METADATA, handlerType);
|
|
37
43
|
if (commandMetadata) {
|
|
38
44
|
console.log(`[NestMediator] Registering command handler: ${handlerType.name} for command: ${commandMetadata.name}`);
|
|
39
45
|
this.mediatorBus.registerCommandHandler(commandMetadata, handlerType);
|
|
40
46
|
}
|
|
47
|
+
// Register query handlers
|
|
41
48
|
const queryMetadata = this.reflector.get(index_js_2.QUERY_HANDLER_METADATA, handlerType);
|
|
42
49
|
if (queryMetadata) {
|
|
43
50
|
console.log(`[NestMediator] Registering query handler: ${handlerType.name} for query: ${queryMetadata.name}`);
|
|
44
51
|
this.mediatorBus.registerQueryHandler(queryMetadata, handlerType);
|
|
45
52
|
}
|
|
53
|
+
// Register pipeline behaviors
|
|
54
|
+
const behaviorMetadata = this.reflector.get(index_js_2.PIPELINE_BEHAVIOR_METADATA, handlerType);
|
|
55
|
+
if (behaviorMetadata) {
|
|
56
|
+
console.log(`[NestMediator] Registering pipeline behavior: ${handlerType.name} (priority: ${behaviorMetadata.priority ?? 0}, scope: ${behaviorMetadata.scope ?? 'all'})`);
|
|
57
|
+
this.mediatorBus.registerPipelineBehavior(handlerType, behaviorMetadata);
|
|
58
|
+
}
|
|
46
59
|
}
|
|
47
60
|
}
|
|
48
61
|
/**
|
|
49
|
-
* Register the NestMediator module
|
|
50
|
-
* Handlers are automatically discovered from the application's providers
|
|
62
|
+
* Register the NestMediator module with default settings.
|
|
63
|
+
* Handlers and behaviors are automatically discovered from the application's providers.
|
|
51
64
|
* @returns Dynamic module
|
|
52
65
|
*/
|
|
53
66
|
static forRoot() {
|
|
67
|
+
return this.forRootAsync({});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Register the NestMediator module with custom configuration.
|
|
71
|
+
*
|
|
72
|
+
* @param options - Configuration options
|
|
73
|
+
* @returns Dynamic module
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* // Enable built-in behaviors
|
|
78
|
+
* NestMediatorModule.forRootAsync({
|
|
79
|
+
* enableLogging: true,
|
|
80
|
+
* enableValidation: true,
|
|
81
|
+
* enableExceptionHandling: true,
|
|
82
|
+
* enablePerformanceTracking: true,
|
|
83
|
+
* performanceThresholdMs: 1000,
|
|
84
|
+
* })
|
|
85
|
+
*
|
|
86
|
+
* // With custom behaviors
|
|
87
|
+
* NestMediatorModule.forRootAsync({
|
|
88
|
+
* behaviors: [MyCustomBehavior],
|
|
89
|
+
* })
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
static forRootAsync(options = {}) {
|
|
93
|
+
const builtInProviders = [];
|
|
94
|
+
// Add built-in behaviors based on options
|
|
95
|
+
if (options.enableExceptionHandling) {
|
|
96
|
+
builtInProviders.push(index_js_3.ExceptionHandlingBehavior);
|
|
97
|
+
}
|
|
98
|
+
if (options.enableLogging) {
|
|
99
|
+
builtInProviders.push(index_js_3.LoggingBehavior);
|
|
100
|
+
}
|
|
101
|
+
if (options.enablePerformanceTracking) {
|
|
102
|
+
builtInProviders.push(index_js_3.PerformanceBehavior);
|
|
103
|
+
}
|
|
104
|
+
if (options.enableValidation) {
|
|
105
|
+
builtInProviders.push(index_js_3.ValidationBehavior);
|
|
106
|
+
}
|
|
107
|
+
// Add custom behaviors
|
|
108
|
+
const customBehaviors = options.behaviors ?? [];
|
|
54
109
|
return {
|
|
55
110
|
module: NestMediatorModule_1,
|
|
56
|
-
imports: [
|
|
57
|
-
core_1.DiscoveryModule,
|
|
58
|
-
],
|
|
111
|
+
imports: [core_1.DiscoveryModule],
|
|
59
112
|
providers: [
|
|
60
113
|
index_js_1.MediatorBus,
|
|
114
|
+
core_1.Reflector,
|
|
115
|
+
...builtInProviders,
|
|
116
|
+
...customBehaviors,
|
|
117
|
+
{
|
|
118
|
+
provide: exports.NEST_MEDIATOR_OPTIONS,
|
|
119
|
+
useValue: options,
|
|
120
|
+
},
|
|
61
121
|
],
|
|
62
122
|
exports: [index_js_1.MediatorBus],
|
|
63
123
|
global: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nest-mediator.module.js","sourceRoot":"","sources":["../../src/lib/nest-mediator.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAA2E;AAC3E,uCAA4E;AAC5E,kDAAkD;AAClD,
|
|
1
|
+
{"version":3,"file":"nest-mediator.module.js","sourceRoot":"","sources":["../../src/lib/nest-mediator.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAA2E;AAC3E,uCAA4E;AAC5E,kDAAkD;AAClD,oDAI+B;AAS/B,mDAK8B;AAkD9B;;GAEG;AACU,QAAA,qBAAqB,GAAG,uBAAuB,CAAC;AAGtD,IAAM,kBAAkB,0BAAxB,MAAM,kBAAkB;IAC7B,YACmB,WAAwB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L,MAAM,EAAE,oBAAkB;YAC1B,OAAO,EAAE,CAAC,sBAAe,CAAC;YAC1B,SAAS,EAAE;gBACT,sBAAW;gBACX,gBAAS;gBACT,GAAG,gBAAgB;gBACnB,GAAG,eAAe;gBAClB;oBACE,OAAO,EAAE,6BAAqB;oBAC9B,QAAQ,EAAE,OAAO;iBAClB;aACF;YACD,OAAO,EAAE,CAAC,sBAAW,CAAC;YACtB,MAAM,EAAE,IAAI;SACb,CAAC;IACJ,CAAC;CACF,CAAA;AAnJY,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,eAAM,EAAC,EAAE,CAAC;qCAGuB,sBAAW;QACb,gBAAS;QACF,uBAAgB;GAJ1C,kBAAkB,CAmJ9B"}
|
|
@@ -1,11 +1,38 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
|
-
import { ModuleRef } from '@nestjs/core';
|
|
2
|
+
import { ModuleRef, Reflector } from '@nestjs/core';
|
|
3
3
|
import { ICommand, ICommandHandler, IQuery, IQueryHandler } from '../interfaces/index.js';
|
|
4
|
+
import { IPipelineBehavior, PipelineBehaviorOptions } from '../interfaces/pipeline-behavior.interface.js';
|
|
5
|
+
/**
|
|
6
|
+
* Central mediator bus for dispatching commands and queries.
|
|
7
|
+
* Supports pipeline behaviors for cross-cutting concerns.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // In a controller
|
|
12
|
+
* @Controller('users')
|
|
13
|
+
* export class UserController {
|
|
14
|
+
* constructor(private readonly mediator: MediatorBus) {}
|
|
15
|
+
*
|
|
16
|
+
* @Post()
|
|
17
|
+
* async create(@Body() dto: CreateUserDto) {
|
|
18
|
+
* await this.mediator.send(new CreateUserCommand(dto));
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @Get(':id')
|
|
22
|
+
* async findOne(@Param('id') id: string) {
|
|
23
|
+
* return this.mediator.query(new GetUserQuery(id));
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
4
28
|
export declare class MediatorBus {
|
|
5
29
|
private readonly moduleRef;
|
|
30
|
+
private readonly reflector;
|
|
31
|
+
private readonly logger;
|
|
6
32
|
private commandHandlers;
|
|
7
33
|
private queryHandlers;
|
|
8
|
-
|
|
34
|
+
private pipelineBehaviors;
|
|
35
|
+
constructor(moduleRef: ModuleRef, reflector: Reflector);
|
|
9
36
|
/**
|
|
10
37
|
* Register a command handler
|
|
11
38
|
* @param command - The command class
|
|
@@ -19,16 +46,39 @@ export declare class MediatorBus {
|
|
|
19
46
|
*/
|
|
20
47
|
registerQueryHandler(query: Type<IQuery>, handler: Type<IQueryHandler<any, any>>): void;
|
|
21
48
|
/**
|
|
22
|
-
*
|
|
49
|
+
* Register a pipeline behavior
|
|
50
|
+
* @param behaviorType - The behavior class
|
|
51
|
+
* @param options - Behavior options (priority, scope)
|
|
52
|
+
*/
|
|
53
|
+
registerPipelineBehavior(behaviorType: Type<IPipelineBehavior<any, any>>, options: PipelineBehaviorOptions): void;
|
|
54
|
+
/**
|
|
55
|
+
* Send a command to its handler through the pipeline
|
|
23
56
|
* @param command - The command instance
|
|
24
57
|
* @returns Promise<void>
|
|
25
58
|
*/
|
|
26
59
|
send<TCommand extends ICommand>(command: TCommand): Promise<void>;
|
|
27
60
|
/**
|
|
28
|
-
* Execute a query through its handler
|
|
61
|
+
* Execute a query through its handler and the pipeline
|
|
29
62
|
* @param query - The query instance
|
|
30
63
|
* @returns Promise with the result
|
|
31
64
|
*/
|
|
32
65
|
query<TQuery extends IQuery, TResult = any>(query: TQuery): Promise<TResult>;
|
|
66
|
+
/**
|
|
67
|
+
* Build a pipeline of behaviors around the handler
|
|
68
|
+
* Uses reduceRight to create a chain where the first behavior wraps all others
|
|
69
|
+
*/
|
|
70
|
+
private buildPipeline;
|
|
71
|
+
/**
|
|
72
|
+
* Get registered command handler names (for debugging)
|
|
73
|
+
*/
|
|
74
|
+
getRegisteredCommands(): string[];
|
|
75
|
+
/**
|
|
76
|
+
* Get registered query handler names (for debugging)
|
|
77
|
+
*/
|
|
78
|
+
getRegisteredQueries(): string[];
|
|
79
|
+
/**
|
|
80
|
+
* Get registered behavior names (for debugging)
|
|
81
|
+
*/
|
|
82
|
+
getRegisteredBehaviors(): string[];
|
|
33
83
|
}
|
|
34
84
|
//# sourceMappingURL=mediator.bus.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mediator.bus.d.ts","sourceRoot":"","sources":["../../../src/lib/services/mediator.bus.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"mediator.bus.d.ts","sourceRoot":"","sources":["../../../src/lib/services/mediator.bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EACL,QAAQ,EACR,eAAe,EACf,MAAM,EACN,aAAa,EACd,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,iBAAiB,EACjB,uBAAuB,EACxB,MAAM,8CAA8C,CAAC;AAYtD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBACa,WAAW;IAQpB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAR5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA6B;IAEpD,OAAO,CAAC,eAAe,CAAiD;IACxE,OAAO,CAAC,aAAa,CAAoD;IACzE,OAAO,CAAC,iBAAiB,CAA4B;gBAGlC,SAAS,EAAE,SAAS,EACpB,SAAS,EAAE,SAAS;IAGvC;;;;OAIG;IACH,sBAAsB,CACpB,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,EACvB,OAAO,EAAE,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,GAClC,IAAI;IAUP;;;;OAIG;IACH,oBAAoB,CAClB,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EACnB,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,GACrC,IAAI;IAQP;;;;OAIG;IACH,wBAAwB,CACtB,YAAY,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAC/C,OAAO,EAAE,uBAAuB,GAC/B,IAAI;IAaP;;;;OAIG;IACG,IAAI,CAAC,QAAQ,SAAS,QAAQ,EAAE,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAsBvE;;;;OAIG;IACG,KAAK,CAAC,MAAM,SAAS,MAAM,EAAE,OAAO,GAAG,GAAG,EAC9C,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,OAAO,CAAC;IAqBnB;;;OAGG;IACH,OAAO,CAAC,aAAa;IAiDrB;;OAEG;IACH,qBAAqB,IAAI,MAAM,EAAE;IAIjC;;OAEG;IACH,oBAAoB,IAAI,MAAM,EAAE;IAIhC;;OAEG;IACH,sBAAsB,IAAI,MAAM,EAAE;CAGnC"}
|
|
@@ -12,11 +12,39 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.MediatorBus = void 0;
|
|
13
13
|
const common_1 = require("@nestjs/common");
|
|
14
14
|
const core_1 = require("@nestjs/core");
|
|
15
|
+
const handler_not_found_exception_js_1 = require("../exceptions/handler-not-found.exception.js");
|
|
16
|
+
const skip_behavior_decorator_js_1 = require("../decorators/skip-behavior.decorator.js");
|
|
17
|
+
/**
|
|
18
|
+
* Central mediator bus for dispatching commands and queries.
|
|
19
|
+
* Supports pipeline behaviors for cross-cutting concerns.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // In a controller
|
|
24
|
+
* @Controller('users')
|
|
25
|
+
* export class UserController {
|
|
26
|
+
* constructor(private readonly mediator: MediatorBus) {}
|
|
27
|
+
*
|
|
28
|
+
* @Post()
|
|
29
|
+
* async create(@Body() dto: CreateUserDto) {
|
|
30
|
+
* await this.mediator.send(new CreateUserCommand(dto));
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* @Get(':id')
|
|
34
|
+
* async findOne(@Param('id') id: string) {
|
|
35
|
+
* return this.mediator.query(new GetUserQuery(id));
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
15
40
|
let MediatorBus = class MediatorBus {
|
|
16
|
-
constructor(moduleRef) {
|
|
41
|
+
constructor(moduleRef, reflector) {
|
|
17
42
|
this.moduleRef = moduleRef;
|
|
43
|
+
this.reflector = reflector;
|
|
44
|
+
this.logger = new common_1.Logger('MediatorBus');
|
|
18
45
|
this.commandHandlers = new Map();
|
|
19
46
|
this.queryHandlers = new Map();
|
|
47
|
+
this.pipelineBehaviors = [];
|
|
20
48
|
}
|
|
21
49
|
/**
|
|
22
50
|
* Register a command handler
|
|
@@ -43,7 +71,18 @@ let MediatorBus = class MediatorBus {
|
|
|
43
71
|
this.queryHandlers.set(queryName, handler);
|
|
44
72
|
}
|
|
45
73
|
/**
|
|
46
|
-
*
|
|
74
|
+
* Register a pipeline behavior
|
|
75
|
+
* @param behaviorType - The behavior class
|
|
76
|
+
* @param options - Behavior options (priority, scope)
|
|
77
|
+
*/
|
|
78
|
+
registerPipelineBehavior(behaviorType, options) {
|
|
79
|
+
this.pipelineBehaviors.push({ type: behaviorType, options });
|
|
80
|
+
// Keep behaviors sorted by priority (lower first)
|
|
81
|
+
this.pipelineBehaviors.sort((a, b) => (a.options.priority ?? 0) - (b.options.priority ?? 0));
|
|
82
|
+
this.logger.log(`Registered pipeline behavior: ${behaviorType.name} (priority: ${options.priority ?? 0}, scope: ${options.scope ?? 'all'})`);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Send a command to its handler through the pipeline
|
|
47
86
|
* @param command - The command instance
|
|
48
87
|
* @returns Promise<void>
|
|
49
88
|
*/
|
|
@@ -51,13 +90,17 @@ let MediatorBus = class MediatorBus {
|
|
|
51
90
|
const commandName = command.constructor.name;
|
|
52
91
|
const handlerType = this.commandHandlers.get(commandName);
|
|
53
92
|
if (!handlerType) {
|
|
54
|
-
throw new
|
|
93
|
+
throw new handler_not_found_exception_js_1.HandlerNotFoundException(commandName, 'command');
|
|
55
94
|
}
|
|
56
|
-
const handler = this.moduleRef.get(handlerType, {
|
|
57
|
-
|
|
95
|
+
const handler = this.moduleRef.get(handlerType, {
|
|
96
|
+
strict: false,
|
|
97
|
+
});
|
|
98
|
+
// Build and execute pipeline
|
|
99
|
+
const pipeline = this.buildPipeline(command, 'command', () => handler.execute(command));
|
|
100
|
+
await pipeline();
|
|
58
101
|
}
|
|
59
102
|
/**
|
|
60
|
-
* Execute a query through its handler
|
|
103
|
+
* Execute a query through its handler and the pipeline
|
|
61
104
|
* @param query - The query instance
|
|
62
105
|
* @returns Promise with the result
|
|
63
106
|
*/
|
|
@@ -65,15 +108,67 @@ let MediatorBus = class MediatorBus {
|
|
|
65
108
|
const queryName = query.constructor.name;
|
|
66
109
|
const handlerType = this.queryHandlers.get(queryName);
|
|
67
110
|
if (!handlerType) {
|
|
68
|
-
throw new
|
|
111
|
+
throw new handler_not_found_exception_js_1.HandlerNotFoundException(queryName, 'query');
|
|
69
112
|
}
|
|
70
113
|
const handler = this.moduleRef.get(handlerType, { strict: false });
|
|
71
|
-
|
|
114
|
+
// Build and execute pipeline
|
|
115
|
+
const pipeline = this.buildPipeline(query, 'query', () => handler.execute(query));
|
|
116
|
+
return pipeline();
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Build a pipeline of behaviors around the handler
|
|
120
|
+
* Uses reduceRight to create a chain where the first behavior wraps all others
|
|
121
|
+
*/
|
|
122
|
+
buildPipeline(request, scope, handler) {
|
|
123
|
+
// Get behaviors to skip from request metadata
|
|
124
|
+
const requestClass = request.constructor;
|
|
125
|
+
const behaviorsToSkip = this.reflector.get(skip_behavior_decorator_js_1.SKIP_BEHAVIORS_METADATA, requestClass) ?? [];
|
|
126
|
+
// Filter behaviors by scope and skip list
|
|
127
|
+
const applicableBehaviors = this.pipelineBehaviors.filter((b) => {
|
|
128
|
+
// Check scope
|
|
129
|
+
const behaviorScope = b.options.scope ?? 'all';
|
|
130
|
+
const scopeMatches = behaviorScope === 'all' || behaviorScope === scope;
|
|
131
|
+
// Check if this behavior should be skipped
|
|
132
|
+
const shouldSkip = behaviorsToSkip.some((skipType) => skipType === b.type);
|
|
133
|
+
return scopeMatches && !shouldSkip;
|
|
134
|
+
});
|
|
135
|
+
// If no behaviors, just return the handler
|
|
136
|
+
if (applicableBehaviors.length === 0) {
|
|
137
|
+
return handler;
|
|
138
|
+
}
|
|
139
|
+
// Build the pipeline from right to left (innermost to outermost)
|
|
140
|
+
// The last behavior in the array wraps the handler directly
|
|
141
|
+
// The first behavior in the array is the outermost wrapper
|
|
142
|
+
return applicableBehaviors.reduceRight((next, registeredBehavior) => {
|
|
143
|
+
return async () => {
|
|
144
|
+
const behavior = this.moduleRef.get(registeredBehavior.type, { strict: false });
|
|
145
|
+
return behavior.handle(request, next);
|
|
146
|
+
};
|
|
147
|
+
}, handler);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Get registered command handler names (for debugging)
|
|
151
|
+
*/
|
|
152
|
+
getRegisteredCommands() {
|
|
153
|
+
return Array.from(this.commandHandlers.keys());
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Get registered query handler names (for debugging)
|
|
157
|
+
*/
|
|
158
|
+
getRegisteredQueries() {
|
|
159
|
+
return Array.from(this.queryHandlers.keys());
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Get registered behavior names (for debugging)
|
|
163
|
+
*/
|
|
164
|
+
getRegisteredBehaviors() {
|
|
165
|
+
return this.pipelineBehaviors.map((b) => b.type.name);
|
|
72
166
|
}
|
|
73
167
|
};
|
|
74
168
|
exports.MediatorBus = MediatorBus;
|
|
75
169
|
exports.MediatorBus = MediatorBus = __decorate([
|
|
76
170
|
(0, common_1.Injectable)(),
|
|
77
|
-
__metadata("design:paramtypes", [core_1.ModuleRef
|
|
171
|
+
__metadata("design:paramtypes", [core_1.ModuleRef,
|
|
172
|
+
core_1.Reflector])
|
|
78
173
|
], MediatorBus);
|
|
79
174
|
//# sourceMappingURL=mediator.bus.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mediator.bus.js","sourceRoot":"","sources":["../../../src/lib/services/mediator.bus.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"mediator.bus.js","sourceRoot":"","sources":["../../../src/lib/services/mediator.bus.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA0D;AAC1D,uCAAoD;AAWpD,iGAAwF;AACxF,yFAAmF;AAUnF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEI,IAAM,WAAW,GAAjB,MAAM,WAAW;IAOtB,YACmB,SAAoB,EACpB,SAAoB;QADpB,cAAS,GAAT,SAAS,CAAW;QACpB,cAAS,GAAT,SAAS,CAAW;QARtB,WAAM,GAAG,IAAI,eAAM,CAAC,aAAa,CAAC,CAAC;QAE5C,oBAAe,GAAG,IAAI,GAAG,EAAsC,CAAC;QAChE,kBAAa,GAAG,IAAI,GAAG,EAAyC,CAAC;QACjE,sBAAiB,GAAyB,EAAE,CAAC;IAKlD,CAAC;IAEJ;;;;OAIG;IACH,sBAAsB,CACpB,OAAuB,EACvB,OAAmC;QAEnC,MAAM,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;QACjC,IAAI,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,CAAC;YAC1C,MAAM,IAAI,KAAK,CACb,uBAAuB,WAAW,wBAAwB,CAC3D,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;IACjD,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AAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxD,CAAC;CACF,CAAA;AAnMY,kCAAW;sBAAX,WAAW;IADvB,IAAA,mBAAU,GAAE;qCASmB,gBAAS;QACT,gBAAS;GAT5B,WAAW,CAmMvB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rolandsall24/nest-mediator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "A lightweight CQRS (Command Query Responsibility Segregation) mediator pattern implementation for NestJS applications",
|
|
5
5
|
"author": "Roland Salloum",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,7 +12,11 @@
|
|
|
12
12
|
"query",
|
|
13
13
|
"handler",
|
|
14
14
|
"pattern",
|
|
15
|
-
"typescript"
|
|
15
|
+
"typescript",
|
|
16
|
+
"pipeline",
|
|
17
|
+
"behaviors",
|
|
18
|
+
"validation",
|
|
19
|
+
"middleware"
|
|
16
20
|
],
|
|
17
21
|
"main": "./dist/index.js",
|
|
18
22
|
"module": "./dist/index.js",
|