@rolandsall24/nest-mediator 0.6.0 → 0.7.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 +354 -56
- package/dist/lib/decorators/event-criticality.decorator.d.ts +61 -0
- package/dist/lib/decorators/event-criticality.decorator.d.ts.map +1 -0
- package/dist/lib/decorators/event-criticality.decorator.js +71 -0
- package/dist/lib/decorators/event-criticality.decorator.js.map +1 -0
- package/dist/lib/decorators/event-handler.decorator.d.ts +21 -0
- package/dist/lib/decorators/event-handler.decorator.d.ts.map +1 -0
- package/dist/lib/decorators/event-handler.decorator.js +27 -0
- package/dist/lib/decorators/event-handler.decorator.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/interfaces/command-bus.interface.d.ts +25 -0
- package/dist/lib/interfaces/command-bus.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/command-bus.interface.js +3 -0
- package/dist/lib/interfaces/command-bus.interface.js.map +1 -0
- package/dist/lib/interfaces/event-bus.interface.d.ts +35 -0
- package/dist/lib/interfaces/event-bus.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/event-bus.interface.js +3 -0
- package/dist/lib/interfaces/event-bus.interface.js.map +1 -0
- package/dist/lib/interfaces/event-consumer.interface.d.ts +64 -0
- package/dist/lib/interfaces/event-consumer.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/event-consumer.interface.js +3 -0
- package/dist/lib/interfaces/event-consumer.interface.js.map +1 -0
- package/dist/lib/interfaces/event-criticality.interface.d.ts +28 -0
- package/dist/lib/interfaces/event-criticality.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/event-criticality.interface.js +25 -0
- package/dist/lib/interfaces/event-criticality.interface.js.map +1 -0
- package/dist/lib/interfaces/event-handler.interface.d.ts +24 -0
- package/dist/lib/interfaces/event-handler.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/event-handler.interface.js +3 -0
- package/dist/lib/interfaces/event-handler.interface.js.map +1 -0
- package/dist/lib/interfaces/event.interface.d.ts +30 -0
- package/dist/lib/interfaces/event.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/event.interface.js +3 -0
- package/dist/lib/interfaces/event.interface.js.map +1 -0
- package/dist/lib/interfaces/index.d.ts +7 -0
- package/dist/lib/interfaces/index.d.ts.map +1 -1
- package/dist/lib/interfaces/index.js +7 -0
- package/dist/lib/interfaces/index.js.map +1 -1
- package/dist/lib/interfaces/mediator.interface.d.ts +14 -0
- package/dist/lib/interfaces/mediator.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/mediator.interface.js +3 -0
- package/dist/lib/interfaces/mediator.interface.js.map +1 -0
- package/dist/lib/interfaces/query-bus.interface.d.ts +26 -0
- package/dist/lib/interfaces/query-bus.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/query-bus.interface.js +3 -0
- package/dist/lib/interfaces/query-bus.interface.js.map +1 -0
- package/dist/lib/nest-mediator.module.d.ts.map +1 -1
- package/dist/lib/nest-mediator.module.js +19 -0
- package/dist/lib/nest-mediator.module.js.map +1 -1
- package/dist/lib/services/command.bus.d.ts +30 -0
- package/dist/lib/services/command.bus.d.ts.map +1 -0
- package/dist/lib/services/command.bus.js +69 -0
- package/dist/lib/services/command.bus.js.map +1 -0
- package/dist/lib/services/event.bus.d.ts +61 -0
- package/dist/lib/services/event.bus.d.ts.map +1 -0
- package/dist/lib/services/event.bus.js +176 -0
- package/dist/lib/services/event.bus.js.map +1 -0
- package/dist/lib/services/mediator.bus.d.ts +48 -30
- package/dist/lib/services/mediator.bus.d.ts.map +1 -1
- package/dist/lib/services/mediator.bus.js +58 -101
- package/dist/lib/services/mediator.bus.js.map +1 -1
- package/dist/lib/services/pipeline.orchestrator.d.ts +46 -0
- package/dist/lib/services/pipeline.orchestrator.d.ts.map +1 -0
- package/dist/lib/services/pipeline.orchestrator.js +87 -0
- package/dist/lib/services/pipeline.orchestrator.js.map +1 -0
- package/dist/lib/services/query.bus.d.ts +31 -0
- package/dist/lib/services/query.bus.d.ts.map +1 -0
- package/dist/lib/services/query.bus.js +68 -0
- package/dist/lib/services/query.bus.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { ModuleRef } from '@nestjs/core';
|
|
3
|
+
import { IEvent, IEventConsumer, IEventBus, EventPublishResult, EventCriticalityMetadata } from '../interfaces/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Event bus implementation.
|
|
6
|
+
* Publishes events to their consumers with critical/non-critical handling.
|
|
7
|
+
* Supports saga-style compensation for critical consumers.
|
|
8
|
+
*/
|
|
9
|
+
export declare class EventBus implements IEventBus {
|
|
10
|
+
private readonly moduleRef;
|
|
11
|
+
private readonly logger;
|
|
12
|
+
private readonly handlers;
|
|
13
|
+
constructor(moduleRef: ModuleRef);
|
|
14
|
+
/**
|
|
15
|
+
* Publish an event to all its consumers
|
|
16
|
+
*
|
|
17
|
+
* Execution flow:
|
|
18
|
+
* 1. Critical consumers run sequentially in order (lower order first)
|
|
19
|
+
* 2. If a critical consumer fails:
|
|
20
|
+
* - Compensations run in reverse order for previously succeeded consumers
|
|
21
|
+
* - Non-critical consumers are NOT dispatched
|
|
22
|
+
* - Original error is thrown after compensations complete
|
|
23
|
+
* 3. If all critical consumers succeed, non-critical consumers are fired in parallel (fire-and-forget)
|
|
24
|
+
*
|
|
25
|
+
* @param event - The event instance
|
|
26
|
+
* @returns Promise with the publish result
|
|
27
|
+
*/
|
|
28
|
+
publish<TEvent extends IEvent>(event: TEvent): Promise<EventPublishResult>;
|
|
29
|
+
/**
|
|
30
|
+
* Run compensations for succeeded consumers in reverse order
|
|
31
|
+
* Continues even if individual compensations fail (logs errors)
|
|
32
|
+
*
|
|
33
|
+
* @param succeededConsumers - Consumers that succeeded and have compensate()
|
|
34
|
+
* @param event - The event to pass to compensate()
|
|
35
|
+
* @returns Number of compensations that were run
|
|
36
|
+
*/
|
|
37
|
+
private runCompensations;
|
|
38
|
+
/**
|
|
39
|
+
* Register an event consumer
|
|
40
|
+
* @param event - The event class
|
|
41
|
+
* @param handler - The consumer class
|
|
42
|
+
* @param criticalityMetadata - Criticality metadata
|
|
43
|
+
*/
|
|
44
|
+
registerEventHandler(event: Type<IEvent>, handler: Type<IEventConsumer<any>>, criticalityMetadata?: EventCriticalityMetadata): void;
|
|
45
|
+
/**
|
|
46
|
+
* Get registered events and their consumers (for debugging)
|
|
47
|
+
*/
|
|
48
|
+
getRegisteredEvents(): {
|
|
49
|
+
event: string;
|
|
50
|
+
handlers: {
|
|
51
|
+
name: string;
|
|
52
|
+
criticality: string;
|
|
53
|
+
order: number;
|
|
54
|
+
}[];
|
|
55
|
+
}[];
|
|
56
|
+
/**
|
|
57
|
+
* Execute a non-critical consumer in the background
|
|
58
|
+
*/
|
|
59
|
+
private executeNonCriticalConsumer;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=event.bus.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.bus.d.ts","sourceRoot":"","sources":["../../../src/lib/services/event.bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EACL,MAAM,EACN,cAAc,EAEd,SAAS,EACT,kBAAkB,EAClB,wBAAwB,EAEzB,MAAM,wBAAwB,CAAC;AAmBhC;;;;GAIG;AACH,qBACa,QAAS,YAAW,SAAS;IAI5B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAHtC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0B;IACjD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgD;gBAE5C,SAAS,EAAE,SAAS;IAEjD;;;;;;;;;;;;;OAaG;IACG,OAAO,CAAC,MAAM,SAAS,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAqFhF;;;;;;;OAOG;YACW,gBAAgB;IA0B9B;;;;;OAKG;IACH,oBAAoB,CAClB,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EACnB,OAAO,EAAE,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EAClC,mBAAmB,CAAC,EAAE,wBAAwB,GAC7C,IAAI;IAkBP;;OAEG;IACH,mBAAmB,IAAI;QACrB,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,WAAW,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,EAAE,CAAC;KAClE,EAAE;IAWH;;OAEG;IACH,OAAO,CAAC,0BAA0B;CAqBnC"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.EventBus = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@nestjs/core");
|
|
15
|
+
const index_js_1 = require("../interfaces/index.js");
|
|
16
|
+
/**
|
|
17
|
+
* Event bus implementation.
|
|
18
|
+
* Publishes events to their consumers with critical/non-critical handling.
|
|
19
|
+
* Supports saga-style compensation for critical consumers.
|
|
20
|
+
*/
|
|
21
|
+
let EventBus = class EventBus {
|
|
22
|
+
constructor(moduleRef) {
|
|
23
|
+
this.moduleRef = moduleRef;
|
|
24
|
+
this.logger = new common_1.Logger('EventBus');
|
|
25
|
+
this.handlers = new Map();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Publish an event to all its consumers
|
|
29
|
+
*
|
|
30
|
+
* Execution flow:
|
|
31
|
+
* 1. Critical consumers run sequentially in order (lower order first)
|
|
32
|
+
* 2. If a critical consumer fails:
|
|
33
|
+
* - Compensations run in reverse order for previously succeeded consumers
|
|
34
|
+
* - Non-critical consumers are NOT dispatched
|
|
35
|
+
* - Original error is thrown after compensations complete
|
|
36
|
+
* 3. If all critical consumers succeed, non-critical consumers are fired in parallel (fire-and-forget)
|
|
37
|
+
*
|
|
38
|
+
* @param event - The event instance
|
|
39
|
+
* @returns Promise with the publish result
|
|
40
|
+
*/
|
|
41
|
+
async publish(event) {
|
|
42
|
+
const eventName = event.constructor.name;
|
|
43
|
+
const consumers = this.handlers.get(eventName) || [];
|
|
44
|
+
if (consumers.length === 0) {
|
|
45
|
+
this.logger.log(`No consumers registered for event: ${eventName}`);
|
|
46
|
+
return {
|
|
47
|
+
totalHandlers: 0,
|
|
48
|
+
criticalSucceeded: 0,
|
|
49
|
+
nonCriticalDispatched: 0,
|
|
50
|
+
compensationsRun: 0,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
// Separate critical and non-critical consumers
|
|
54
|
+
const criticalConsumers = consumers
|
|
55
|
+
.filter((h) => h.criticality === index_js_1.EventCriticality.CRITICAL)
|
|
56
|
+
.sort((a, b) => a.order - b.order);
|
|
57
|
+
const nonCriticalConsumers = consumers.filter((h) => h.criticality === index_js_1.EventCriticality.NON_CRITICAL);
|
|
58
|
+
this.logger.log(`Publishing event: ${eventName} to ${criticalConsumers.length} critical and ${nonCriticalConsumers.length} non-critical consumers`);
|
|
59
|
+
// Phase 1: Execute critical consumers sequentially, tracking those with compensate()
|
|
60
|
+
const succeededWithCompensation = [];
|
|
61
|
+
let criticalSucceeded = 0;
|
|
62
|
+
for (const registeredConsumer of criticalConsumers) {
|
|
63
|
+
const consumer = this.moduleRef.get(registeredConsumer.type, { strict: false });
|
|
64
|
+
try {
|
|
65
|
+
await consumer.handle(event);
|
|
66
|
+
criticalSucceeded++;
|
|
67
|
+
this.logger.log(`Critical consumer ${registeredConsumer.type.name} completed successfully`);
|
|
68
|
+
// Track if it has a compensate method for potential rollback
|
|
69
|
+
if (typeof consumer.compensate === 'function') {
|
|
70
|
+
succeededWithCompensation.push({
|
|
71
|
+
consumer,
|
|
72
|
+
name: registeredConsumer.type.name,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
this.logger.error(`Critical consumer ${registeredConsumer.type.name} failed: ${error.message}`);
|
|
78
|
+
// Run compensations for previously succeeded consumers (in reverse order)
|
|
79
|
+
const compensationsRun = await this.runCompensations(succeededWithCompensation, event);
|
|
80
|
+
this.logger.log(`Ran ${compensationsRun} compensations after failure in ${registeredConsumer.type.name}`);
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// Phase 2: Fire non-critical consumers in parallel (fire-and-forget)
|
|
85
|
+
let nonCriticalDispatched = 0;
|
|
86
|
+
for (const registeredConsumer of nonCriticalConsumers) {
|
|
87
|
+
nonCriticalDispatched++;
|
|
88
|
+
this.executeNonCriticalConsumer(event, registeredConsumer);
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
totalHandlers: consumers.length,
|
|
92
|
+
criticalSucceeded,
|
|
93
|
+
nonCriticalDispatched,
|
|
94
|
+
compensationsRun: 0,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Run compensations for succeeded consumers in reverse order
|
|
99
|
+
* Continues even if individual compensations fail (logs errors)
|
|
100
|
+
*
|
|
101
|
+
* @param succeededConsumers - Consumers that succeeded and have compensate()
|
|
102
|
+
* @param event - The event to pass to compensate()
|
|
103
|
+
* @returns Number of compensations that were run
|
|
104
|
+
*/
|
|
105
|
+
async runCompensations(succeededConsumers, event) {
|
|
106
|
+
let compensationsRun = 0;
|
|
107
|
+
// Run in reverse order (last succeeded -> first succeeded)
|
|
108
|
+
for (const { consumer, name } of succeededConsumers.reverse()) {
|
|
109
|
+
try {
|
|
110
|
+
this.logger.log(`Running compensation for ${name}...`);
|
|
111
|
+
await consumer.compensate(event);
|
|
112
|
+
compensationsRun++;
|
|
113
|
+
this.logger.log(`Compensation for ${name} completed successfully`);
|
|
114
|
+
}
|
|
115
|
+
catch (compError) {
|
|
116
|
+
// Log but continue with other compensations
|
|
117
|
+
this.logger.error(`Compensation for ${name} failed: ${compError.message}. ` +
|
|
118
|
+
`Manual intervention may be required.`);
|
|
119
|
+
compensationsRun++; // Count as run even if failed
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return compensationsRun;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Register an event consumer
|
|
126
|
+
* @param event - The event class
|
|
127
|
+
* @param handler - The consumer class
|
|
128
|
+
* @param criticalityMetadata - Criticality metadata
|
|
129
|
+
*/
|
|
130
|
+
registerEventHandler(event, handler, criticalityMetadata) {
|
|
131
|
+
const eventName = event.name;
|
|
132
|
+
const consumers = this.handlers.get(eventName) || [];
|
|
133
|
+
const registeredConsumer = {
|
|
134
|
+
type: handler,
|
|
135
|
+
criticality: criticalityMetadata?.criticality ?? index_js_1.EventCriticality.NON_CRITICAL,
|
|
136
|
+
order: criticalityMetadata?.order ?? 0,
|
|
137
|
+
};
|
|
138
|
+
consumers.push(registeredConsumer);
|
|
139
|
+
this.handlers.set(eventName, consumers);
|
|
140
|
+
this.logger.log(`Registered event consumer: ${handler.name} for event: ${eventName} (criticality: ${registeredConsumer.criticality}, order: ${registeredConsumer.order})`);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Get registered events and their consumers (for debugging)
|
|
144
|
+
*/
|
|
145
|
+
getRegisteredEvents() {
|
|
146
|
+
return Array.from(this.handlers.entries()).map(([event, consumers]) => ({
|
|
147
|
+
event,
|
|
148
|
+
handlers: consumers.map((h) => ({
|
|
149
|
+
name: h.type.name,
|
|
150
|
+
criticality: h.criticality,
|
|
151
|
+
order: h.order,
|
|
152
|
+
})),
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Execute a non-critical consumer in the background
|
|
157
|
+
*/
|
|
158
|
+
executeNonCriticalConsumer(event, registeredConsumer) {
|
|
159
|
+
setImmediate(async () => {
|
|
160
|
+
try {
|
|
161
|
+
const consumer = this.moduleRef.get(registeredConsumer.type, { strict: false });
|
|
162
|
+
await consumer.handle(event);
|
|
163
|
+
this.logger.log(`Non-critical consumer ${registeredConsumer.type.name} completed successfully`);
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
this.logger.warn(`Non-critical consumer ${registeredConsumer.type.name} failed: ${error.message}`);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
exports.EventBus = EventBus;
|
|
172
|
+
exports.EventBus = EventBus = __decorate([
|
|
173
|
+
(0, common_1.Injectable)(),
|
|
174
|
+
__metadata("design:paramtypes", [core_1.ModuleRef])
|
|
175
|
+
], EventBus);
|
|
176
|
+
//# sourceMappingURL=event.bus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.bus.js","sourceRoot":"","sources":["../../../src/lib/services/event.bus.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA0D;AAC1D,uCAAyC;AACzC,qDAQgC;AAmBhC;;;;GAIG;AAEI,IAAM,QAA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IAAI,yBAAyB,CAC/E,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,yBAAyB,kBAAkB,CAAC,IAAI,CAAC,IAAI,YAAa,KAAe,CAAC,OAAO,EAAE,CAC5F,CAAC;YACJ,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAhNY,4BAAQ;mBAAR,QAAQ;IADpB,IAAA,mBAAU,GAAE;qCAK6B,gBAAS;GAJtC,QAAQ,CAgNpB"}
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
|
-
import {
|
|
3
|
-
import { ICommand, ICommandHandler, IQuery, IQueryHandler } from '../interfaces/index.js';
|
|
2
|
+
import { ICommand, ICommandHandler, IQuery, IQueryHandler, IEvent, IEventConsumer, IMediator, EventPublishResult, EventCriticalityMetadata } from '../interfaces/index.js';
|
|
4
3
|
import { IPipelineBehavior, PipelineBehaviorOptions } from '../interfaces/pipeline-behavior.interface.js';
|
|
4
|
+
import { CommandBus } from './command.bus.js';
|
|
5
|
+
import { QueryBus } from './query.bus.js';
|
|
6
|
+
import { EventBus } from './event.bus.js';
|
|
7
|
+
import { PipelineOrchestrator } from './pipeline.orchestrator.js';
|
|
5
8
|
/**
|
|
6
|
-
* Central mediator bus for dispatching commands and
|
|
7
|
-
*
|
|
9
|
+
* Central mediator bus for dispatching commands, queries, and events.
|
|
10
|
+
* Acts as a facade delegating to specialized buses.
|
|
8
11
|
*
|
|
9
12
|
* @example
|
|
10
13
|
* ```typescript
|
|
11
|
-
* // In a controller
|
|
12
14
|
* @Controller('users')
|
|
13
15
|
* export class UserController {
|
|
14
16
|
* constructor(private readonly mediator: MediatorBus) {}
|
|
@@ -25,14 +27,29 @@ import { IPipelineBehavior, PipelineBehaviorOptions } from '../interfaces/pipeli
|
|
|
25
27
|
* }
|
|
26
28
|
* ```
|
|
27
29
|
*/
|
|
28
|
-
export declare class MediatorBus {
|
|
29
|
-
private readonly
|
|
30
|
-
private readonly
|
|
31
|
-
private readonly
|
|
32
|
-
private
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
export declare class MediatorBus implements IMediator {
|
|
31
|
+
private readonly commandBus;
|
|
32
|
+
private readonly queryBus;
|
|
33
|
+
private readonly eventBus;
|
|
34
|
+
private readonly pipelineOrchestrator;
|
|
35
|
+
constructor(commandBus: CommandBus, queryBus: QueryBus, eventBus: EventBus, pipelineOrchestrator: PipelineOrchestrator);
|
|
36
|
+
/**
|
|
37
|
+
* Send a command to its handler through the pipeline
|
|
38
|
+
* @param command - The command instance
|
|
39
|
+
*/
|
|
40
|
+
send<TCommand extends ICommand>(command: TCommand): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Execute a query through its handler and the pipeline
|
|
43
|
+
* @param query - The query instance
|
|
44
|
+
* @returns Promise with the result
|
|
45
|
+
*/
|
|
46
|
+
query<TQuery extends IQuery, TResult = any>(query: TQuery): Promise<TResult>;
|
|
47
|
+
/**
|
|
48
|
+
* Publish an event to all its consumers
|
|
49
|
+
* @param event - The event instance
|
|
50
|
+
* @returns Promise with the publish result
|
|
51
|
+
*/
|
|
52
|
+
publish<TEvent extends IEvent>(event: TEvent): Promise<EventPublishResult>;
|
|
36
53
|
/**
|
|
37
54
|
* Register a command handler
|
|
38
55
|
* @param command - The command class
|
|
@@ -53,33 +70,34 @@ export declare class MediatorBus {
|
|
|
53
70
|
*/
|
|
54
71
|
registerPipelineBehavior(behaviorType: Type<IPipelineBehavior<any, any>>, options: PipelineBehaviorOptions, requestType?: Function): void;
|
|
55
72
|
/**
|
|
56
|
-
*
|
|
57
|
-
* @param
|
|
58
|
-
* @
|
|
73
|
+
* Register an event consumer
|
|
74
|
+
* @param event - The event class
|
|
75
|
+
* @param handler - The consumer class
|
|
76
|
+
* @param criticalityMetadata - Criticality metadata
|
|
59
77
|
*/
|
|
60
|
-
|
|
78
|
+
registerEventHandler(event: Type<IEvent>, handler: Type<IEventConsumer<any>>, criticalityMetadata?: EventCriticalityMetadata): void;
|
|
61
79
|
/**
|
|
62
|
-
*
|
|
63
|
-
* @param query - The query instance
|
|
64
|
-
* @returns Promise with the result
|
|
65
|
-
*/
|
|
66
|
-
query<TQuery extends IQuery, TResult = any>(query: TQuery): Promise<TResult>;
|
|
67
|
-
/**
|
|
68
|
-
* Build a pipeline of behaviors around the handler
|
|
69
|
-
* Uses reduceRight to create a chain where the first behavior wraps all others
|
|
70
|
-
*/
|
|
71
|
-
private buildPipeline;
|
|
72
|
-
/**
|
|
73
|
-
* Get registered command handler names (for debugging)
|
|
80
|
+
* Get registered command names (for debugging)
|
|
74
81
|
*/
|
|
75
82
|
getRegisteredCommands(): string[];
|
|
76
83
|
/**
|
|
77
|
-
* Get registered query
|
|
84
|
+
* Get registered query names (for debugging)
|
|
78
85
|
*/
|
|
79
86
|
getRegisteredQueries(): string[];
|
|
80
87
|
/**
|
|
81
88
|
* Get registered behavior names (for debugging)
|
|
82
89
|
*/
|
|
83
90
|
getRegisteredBehaviors(): string[];
|
|
91
|
+
/**
|
|
92
|
+
* Get registered events and their consumers (for debugging)
|
|
93
|
+
*/
|
|
94
|
+
getRegisteredEvents(): {
|
|
95
|
+
event: string;
|
|
96
|
+
handlers: {
|
|
97
|
+
name: string;
|
|
98
|
+
criticality: string;
|
|
99
|
+
order: number;
|
|
100
|
+
}[];
|
|
101
|
+
}[];
|
|
84
102
|
}
|
|
85
103
|
//# 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,EAAc,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAA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;IAIP;;;;OAIG;IACH,oBAAoB,CAClB,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EACnB,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,GACrC,IAAI;IAIP;;;;;OAKG;IACH,wBAAwB,CACtB,YAAY,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAC/C,OAAO,EAAE,uBAAuB,EAChC,WAAW,CAAC,EAAE,QAAQ,GACrB,IAAI;IAIP;;;;;OAKG;IACH,oBAAoB,CAClB,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EACnB,OAAO,EAAE,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EAClC,mBAAmB,CAAC,EAAE,wBAAwB,GAC7C,IAAI;IAIP;;OAEG;IACH,qBAAqB,IAAI,MAAM,EAAE;IAIjC;;OAEG;IACH,oBAAoB,IAAI,MAAM,EAAE;IAIhC;;OAEG;IACH,sBAAsB,IAAI,MAAM,EAAE;IAIlC;;OAEG;IACH,mBAAmB,IAAI;QACrB,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,WAAW,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,EAAE,CAAC;KAClE,EAAE;CAGJ"}
|
|
@@ -11,16 +11,16 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.MediatorBus = void 0;
|
|
13
13
|
const common_1 = require("@nestjs/common");
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
14
|
+
const command_bus_js_1 = require("./command.bus.js");
|
|
15
|
+
const query_bus_js_1 = require("./query.bus.js");
|
|
16
|
+
const event_bus_js_1 = require("./event.bus.js");
|
|
17
|
+
const pipeline_orchestrator_js_1 = require("./pipeline.orchestrator.js");
|
|
17
18
|
/**
|
|
18
|
-
* Central mediator bus for dispatching commands and
|
|
19
|
-
*
|
|
19
|
+
* Central mediator bus for dispatching commands, queries, and events.
|
|
20
|
+
* Acts as a facade delegating to specialized buses.
|
|
20
21
|
*
|
|
21
22
|
* @example
|
|
22
23
|
* ```typescript
|
|
23
|
-
* // In a controller
|
|
24
24
|
* @Controller('users')
|
|
25
25
|
* export class UserController {
|
|
26
26
|
* constructor(private readonly mediator: MediatorBus) {}
|
|
@@ -38,13 +38,34 @@ const skip_behavior_decorator_js_1 = require("../decorators/skip-behavior.decora
|
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
40
|
let MediatorBus = class MediatorBus {
|
|
41
|
-
constructor(
|
|
42
|
-
this.
|
|
43
|
-
this.
|
|
44
|
-
this.
|
|
45
|
-
this.
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
constructor(commandBus, queryBus, eventBus, pipelineOrchestrator) {
|
|
42
|
+
this.commandBus = commandBus;
|
|
43
|
+
this.queryBus = queryBus;
|
|
44
|
+
this.eventBus = eventBus;
|
|
45
|
+
this.pipelineOrchestrator = pipelineOrchestrator;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Send a command to its handler through the pipeline
|
|
49
|
+
* @param command - The command instance
|
|
50
|
+
*/
|
|
51
|
+
async send(command) {
|
|
52
|
+
return this.commandBus.send(command);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Execute a query through its handler and the pipeline
|
|
56
|
+
* @param query - The query instance
|
|
57
|
+
* @returns Promise with the result
|
|
58
|
+
*/
|
|
59
|
+
async query(query) {
|
|
60
|
+
return this.queryBus.query(query);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Publish an event to all its consumers
|
|
64
|
+
* @param event - The event instance
|
|
65
|
+
* @returns Promise with the publish result
|
|
66
|
+
*/
|
|
67
|
+
async publish(event) {
|
|
68
|
+
return this.eventBus.publish(event);
|
|
48
69
|
}
|
|
49
70
|
/**
|
|
50
71
|
* Register a command handler
|
|
@@ -52,11 +73,7 @@ let MediatorBus = class MediatorBus {
|
|
|
52
73
|
* @param handler - The handler class
|
|
53
74
|
*/
|
|
54
75
|
registerCommandHandler(command, handler) {
|
|
55
|
-
|
|
56
|
-
if (this.commandHandlers.has(commandName)) {
|
|
57
|
-
throw new Error(`Command handler for ${commandName} is already registered`);
|
|
58
|
-
}
|
|
59
|
-
this.commandHandlers.set(commandName, handler);
|
|
76
|
+
this.commandBus.registerCommandHandler(command, handler);
|
|
60
77
|
}
|
|
61
78
|
/**
|
|
62
79
|
* Register a query handler
|
|
@@ -64,11 +81,7 @@ let MediatorBus = class MediatorBus {
|
|
|
64
81
|
* @param handler - The handler class
|
|
65
82
|
*/
|
|
66
83
|
registerQueryHandler(query, handler) {
|
|
67
|
-
|
|
68
|
-
if (this.queryHandlers.has(queryName)) {
|
|
69
|
-
throw new Error(`Query handler for ${queryName} is already registered`);
|
|
70
|
-
}
|
|
71
|
-
this.queryHandlers.set(queryName, handler);
|
|
84
|
+
this.queryBus.registerQueryHandler(query, handler);
|
|
72
85
|
}
|
|
73
86
|
/**
|
|
74
87
|
* Register a pipeline behavior
|
|
@@ -77,104 +90,48 @@ let MediatorBus = class MediatorBus {
|
|
|
77
90
|
* @param requestType - Optional specific request type this behavior applies to
|
|
78
91
|
*/
|
|
79
92
|
registerPipelineBehavior(behaviorType, options, requestType) {
|
|
80
|
-
this.
|
|
81
|
-
// Keep behaviors sorted by priority (lower first)
|
|
82
|
-
this.pipelineBehaviors.sort((a, b) => (a.options.priority ?? 0) - (b.options.priority ?? 0));
|
|
83
|
-
const requestTypeInfo = requestType ? `, requestType: ${requestType.name}` : '';
|
|
84
|
-
this.logger.log(`Registered pipeline behavior: ${behaviorType.name} (priority: ${options.priority ?? 0}, scope: ${options.scope ?? 'all'}${requestTypeInfo})`);
|
|
93
|
+
this.pipelineOrchestrator.registerBehavior(behaviorType, options, requestType);
|
|
85
94
|
}
|
|
86
95
|
/**
|
|
87
|
-
*
|
|
88
|
-
* @param
|
|
89
|
-
* @
|
|
90
|
-
|
|
91
|
-
async send(command) {
|
|
92
|
-
const commandName = command.constructor.name;
|
|
93
|
-
const handlerType = this.commandHandlers.get(commandName);
|
|
94
|
-
if (!handlerType) {
|
|
95
|
-
throw new handler_not_found_exception_js_1.HandlerNotFoundException(commandName, 'command');
|
|
96
|
-
}
|
|
97
|
-
const handler = this.moduleRef.get(handlerType, {
|
|
98
|
-
strict: false,
|
|
99
|
-
});
|
|
100
|
-
// Build and execute pipeline
|
|
101
|
-
const pipeline = this.buildPipeline(command, 'command', () => handler.execute(command));
|
|
102
|
-
await pipeline();
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Execute a query through its handler and the pipeline
|
|
106
|
-
* @param query - The query instance
|
|
107
|
-
* @returns Promise with the result
|
|
108
|
-
*/
|
|
109
|
-
async query(query) {
|
|
110
|
-
const queryName = query.constructor.name;
|
|
111
|
-
const handlerType = this.queryHandlers.get(queryName);
|
|
112
|
-
if (!handlerType) {
|
|
113
|
-
throw new handler_not_found_exception_js_1.HandlerNotFoundException(queryName, 'query');
|
|
114
|
-
}
|
|
115
|
-
const handler = this.moduleRef.get(handlerType, { strict: false });
|
|
116
|
-
// Build and execute pipeline
|
|
117
|
-
const pipeline = this.buildPipeline(query, 'query', () => handler.execute(query));
|
|
118
|
-
return pipeline();
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Build a pipeline of behaviors around the handler
|
|
122
|
-
* Uses reduceRight to create a chain where the first behavior wraps all others
|
|
96
|
+
* Register an event consumer
|
|
97
|
+
* @param event - The event class
|
|
98
|
+
* @param handler - The consumer class
|
|
99
|
+
* @param criticalityMetadata - Criticality metadata
|
|
123
100
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const requestClass = request.constructor;
|
|
127
|
-
const behaviorsToSkip = this.reflector.get(skip_behavior_decorator_js_1.SKIP_BEHAVIORS_METADATA, requestClass) ?? [];
|
|
128
|
-
// Filter behaviors by scope, skip list, and request type
|
|
129
|
-
const applicableBehaviors = this.pipelineBehaviors.filter((b) => {
|
|
130
|
-
// Check scope
|
|
131
|
-
const behaviorScope = b.options.scope ?? 'all';
|
|
132
|
-
const scopeMatches = behaviorScope === 'all' || behaviorScope === scope;
|
|
133
|
-
// Check if this behavior should be skipped
|
|
134
|
-
const shouldSkip = behaviorsToSkip.some((skipType) => skipType === b.type);
|
|
135
|
-
// Check request type match (if behavior has a specific request type)
|
|
136
|
-
// If behavior has no requestType, it applies to all requests
|
|
137
|
-
// If behavior has a requestType, only apply if request is an instance of that type
|
|
138
|
-
const requestTypeMatches = !b.requestType || request instanceof b.requestType;
|
|
139
|
-
return scopeMatches && !shouldSkip && requestTypeMatches;
|
|
140
|
-
});
|
|
141
|
-
// If no behaviors, just return the handler
|
|
142
|
-
if (applicableBehaviors.length === 0) {
|
|
143
|
-
return handler;
|
|
144
|
-
}
|
|
145
|
-
// Build the pipeline from right to left (innermost to outermost)
|
|
146
|
-
// The last behavior in the array wraps the handler directly
|
|
147
|
-
// The first behavior in the array is the outermost wrapper
|
|
148
|
-
return applicableBehaviors.reduceRight((next, registeredBehavior) => {
|
|
149
|
-
return async () => {
|
|
150
|
-
const behavior = this.moduleRef.get(registeredBehavior.type, { strict: false });
|
|
151
|
-
return behavior.handle(request, next);
|
|
152
|
-
};
|
|
153
|
-
}, handler);
|
|
101
|
+
registerEventHandler(event, handler, criticalityMetadata) {
|
|
102
|
+
this.eventBus.registerEventHandler(event, handler, criticalityMetadata);
|
|
154
103
|
}
|
|
155
104
|
/**
|
|
156
|
-
* Get registered command
|
|
105
|
+
* Get registered command names (for debugging)
|
|
157
106
|
*/
|
|
158
107
|
getRegisteredCommands() {
|
|
159
|
-
return
|
|
108
|
+
return this.commandBus.getRegisteredCommands();
|
|
160
109
|
}
|
|
161
110
|
/**
|
|
162
|
-
* Get registered query
|
|
111
|
+
* Get registered query names (for debugging)
|
|
163
112
|
*/
|
|
164
113
|
getRegisteredQueries() {
|
|
165
|
-
return
|
|
114
|
+
return this.queryBus.getRegisteredQueries();
|
|
166
115
|
}
|
|
167
116
|
/**
|
|
168
117
|
* Get registered behavior names (for debugging)
|
|
169
118
|
*/
|
|
170
119
|
getRegisteredBehaviors() {
|
|
171
|
-
return this.
|
|
120
|
+
return this.pipelineOrchestrator.getRegisteredBehaviors();
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get registered events and their consumers (for debugging)
|
|
124
|
+
*/
|
|
125
|
+
getRegisteredEvents() {
|
|
126
|
+
return this.eventBus.getRegisteredEvents();
|
|
172
127
|
}
|
|
173
128
|
};
|
|
174
129
|
exports.MediatorBus = MediatorBus;
|
|
175
130
|
exports.MediatorBus = MediatorBus = __decorate([
|
|
176
131
|
(0, common_1.Injectable)(),
|
|
177
|
-
__metadata("design:paramtypes", [
|
|
178
|
-
|
|
132
|
+
__metadata("design:paramtypes", [command_bus_js_1.CommandBus,
|
|
133
|
+
query_bus_js_1.QueryBus,
|
|
134
|
+
event_bus_js_1.EventBus,
|
|
135
|
+
pipeline_orchestrator_js_1.PipelineOrchestrator])
|
|
179
136
|
], MediatorBus);
|
|
180
137
|
//# 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,2CAAkD;AAgBlD,qDAA8C;AAC9C,iDAA0C;AAC1C,iDAA0C;AAC1C,yEAAkE;AAElE;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEI,IAAM,WAAW,GAAjB,MAAM,WAAW;IACtB,YACmB,UAAsB,EACtB,QAAkB,EAClB,QAAkB,EAClB,oBAA0C;QAH1C,eAAU,GAAV,UA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