@rolandsall24/nest-mediator 0.0.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/LICENSE +21 -0
- package/README.md +655 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/lib/decorators/command-handler.decorator.d.ts +19 -0
- package/dist/lib/decorators/command-handler.decorator.d.ts.map +1 -0
- package/dist/lib/decorators/command-handler.decorator.js +20 -0
- package/dist/lib/decorators/index.d.ts +3 -0
- package/dist/lib/decorators/index.d.ts.map +1 -0
- package/dist/lib/decorators/index.js +2 -0
- package/dist/lib/decorators/query-handler.decorator.d.ts +19 -0
- package/dist/lib/decorators/query-handler.decorator.d.ts.map +1 -0
- package/dist/lib/decorators/query-handler.decorator.js +20 -0
- package/dist/lib/interfaces/command-handler.interface.d.ts +14 -0
- package/dist/lib/interfaces/command-handler.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/command-handler.interface.js +1 -0
- package/dist/lib/interfaces/command.interface.d.ts +7 -0
- package/dist/lib/interfaces/command.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/command.interface.js +1 -0
- package/dist/lib/interfaces/index.d.ts +5 -0
- package/dist/lib/interfaces/index.d.ts.map +1 -0
- package/dist/lib/interfaces/index.js +4 -0
- package/dist/lib/interfaces/query-handler.interface.d.ts +15 -0
- package/dist/lib/interfaces/query-handler.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/query-handler.interface.js +1 -0
- package/dist/lib/interfaces/query.interface.d.ts +7 -0
- package/dist/lib/interfaces/query.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/query.interface.js +1 -0
- package/dist/lib/nest-mediator.d.ts +2 -0
- package/dist/lib/nest-mediator.d.ts.map +1 -0
- package/dist/lib/nest-mediator.js +3 -0
- package/dist/lib/nest-mediator.module.d.ts +21 -0
- package/dist/lib/nest-mediator.module.d.ts.map +1 -0
- package/dist/lib/nest-mediator.module.js +58 -0
- package/dist/lib/services/index.d.ts +2 -0
- package/dist/lib/services/index.d.ts.map +1 -0
- package/dist/lib/services/index.js +1 -0
- package/dist/lib/services/mediator.service.d.ts +34 -0
- package/dist/lib/services/mediator.service.d.ts.map +1 -0
- package/dist/lib/services/mediator.service.js +68 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/package.json +56 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-handler.decorator.d.ts","sourceRoot":"","sources":["../../../src/lib/decorators/command-handler.decorator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAElD,eAAO,MAAM,wBAAwB,6BAA6B,CAAC;AAEnE;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,cAAc,GAAI,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,QAAQ,KAAG,cAE1E,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { SetMetadata } from '@nestjs/common';
|
|
2
|
+
export const COMMAND_HANDLER_METADATA = 'COMMAND_HANDLER_METADATA';
|
|
3
|
+
/**
|
|
4
|
+
* Decorator to mark a class as a command handler
|
|
5
|
+
* @param command - The command class that this handler handles
|
|
6
|
+
* @returns Class decorator
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @CommandHandler(AddCategoryCommand)
|
|
11
|
+
* export class AddCategoryCommandHandler implements ICommandHandler<AddCategoryCommand> {
|
|
12
|
+
* async execute(command: AddCategoryCommand): Promise<Category> {
|
|
13
|
+
* // Implementation
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export const CommandHandler = (command) => {
|
|
19
|
+
return SetMetadata(COMMAND_HANDLER_METADATA, command);
|
|
20
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/decorators/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,8BAA8B,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IQuery } from '../interfaces/index.js';
|
|
2
|
+
export declare const QUERY_HANDLER_METADATA = "QUERY_HANDLER_METADATA";
|
|
3
|
+
/**
|
|
4
|
+
* Decorator to mark a class as a query handler
|
|
5
|
+
* @param query - The query class that this handler handles
|
|
6
|
+
* @returns Class decorator
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @QueryHandler(GetCategoryQuery)
|
|
11
|
+
* export class GetCategoryQueryHandler implements IQueryHandler<GetCategoryQuery, Category> {
|
|
12
|
+
* async execute(query: GetCategoryQuery): Promise<Category> {
|
|
13
|
+
* // Implementation
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare const QueryHandler: (query: new (...args: any[]) => IQuery) => ClassDecorator;
|
|
19
|
+
//# sourceMappingURL=query-handler.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-handler.decorator.d.ts","sourceRoot":"","sources":["../../../src/lib/decorators/query-handler.decorator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAEhD,eAAO,MAAM,sBAAsB,2BAA2B,CAAC;AAE/D;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,MAAM,KAAG,cAEpE,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { SetMetadata } from '@nestjs/common';
|
|
2
|
+
export const QUERY_HANDLER_METADATA = 'QUERY_HANDLER_METADATA';
|
|
3
|
+
/**
|
|
4
|
+
* Decorator to mark a class as a query handler
|
|
5
|
+
* @param query - The query class that this handler handles
|
|
6
|
+
* @returns Class decorator
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @QueryHandler(GetCategoryQuery)
|
|
11
|
+
* export class GetCategoryQueryHandler implements IQueryHandler<GetCategoryQuery, Category> {
|
|
12
|
+
* async execute(query: GetCategoryQuery): Promise<Category> {
|
|
13
|
+
* // Implementation
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export const QueryHandler = (query) => {
|
|
19
|
+
return SetMetadata(QUERY_HANDLER_METADATA, query);
|
|
20
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ICommand } from './command.interface.js';
|
|
2
|
+
/**
|
|
3
|
+
* Interface for command handlers
|
|
4
|
+
* @template TCommand - The command type that extends ICommand
|
|
5
|
+
*/
|
|
6
|
+
export interface ICommandHandler<TCommand extends ICommand> {
|
|
7
|
+
/**
|
|
8
|
+
* Execute the command
|
|
9
|
+
* @param command - The command to execute
|
|
10
|
+
* @returns Promise<void>
|
|
11
|
+
*/
|
|
12
|
+
execute(command: TCommand): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=command-handler.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-handler.interface.d.ts","sourceRoot":"","sources":["../../../src/lib/interfaces/command-handler.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAElD;;;GAGG;AACH,MAAM,WAAW,eAAe,CAAC,QAAQ,SAAS,QAAQ;IACxD;;;;OAIG;IACH,OAAO,CAAC,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command.interface.d.ts","sourceRoot":"","sources":["../../../src/lib/interfaces/command.interface.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,QAAQ;CAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,8BAA8B,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { IQuery } from './query.interface.js';
|
|
2
|
+
/**
|
|
3
|
+
* Interface for query handlers
|
|
4
|
+
* @template TQuery - The query type that extends IQuery
|
|
5
|
+
* @template TResult - The result type (default: any)
|
|
6
|
+
*/
|
|
7
|
+
export interface IQueryHandler<TQuery extends IQuery, TResult = any> {
|
|
8
|
+
/**
|
|
9
|
+
* Execute the query
|
|
10
|
+
* @param query - The query to execute
|
|
11
|
+
* @returns Promise with the result
|
|
12
|
+
*/
|
|
13
|
+
execute(query: TQuery): Promise<TResult>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=query-handler.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-handler.interface.d.ts","sourceRoot":"","sources":["../../../src/lib/interfaces/query-handler.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAE9C;;;;GAIG;AACH,MAAM,WAAW,aAAa,CAAC,MAAM,SAAS,MAAM,EAAE,OAAO,GAAG,GAAG;IACjE;;;;OAIG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.interface.d.ts","sourceRoot":"","sources":["../../../src/lib/interfaces/query.interface.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,MAAM;CAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nest-mediator.d.ts","sourceRoot":"","sources":["../../src/lib/nest-mediator.ts"],"names":[],"mappings":"AAAA,wBAAgB,YAAY,IAAI,MAAM,CAErC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { DynamicModule, Type, OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { Reflector } from '@nestjs/core';
|
|
3
|
+
import { MediatorService } from './services/index.js';
|
|
4
|
+
import { ICommandHandler, IQueryHandler } from './interfaces/index.js';
|
|
5
|
+
export interface NestMediatorModuleOptions {
|
|
6
|
+
handlers?: Type<ICommandHandler<any> | IQueryHandler<any, any>>[];
|
|
7
|
+
}
|
|
8
|
+
export declare class NestMediatorModule implements OnModuleInit {
|
|
9
|
+
private readonly mediatorService;
|
|
10
|
+
private readonly reflector;
|
|
11
|
+
private readonly handlers;
|
|
12
|
+
constructor(mediatorService: MediatorService, reflector: Reflector, handlers: Type<ICommandHandler<any> | IQueryHandler<any, any>>[]);
|
|
13
|
+
onModuleInit(): void;
|
|
14
|
+
/**
|
|
15
|
+
* Register the NestMediator module with handlers
|
|
16
|
+
* @param options - Module options containing handlers to register
|
|
17
|
+
* @returns Dynamic module
|
|
18
|
+
*/
|
|
19
|
+
static forRoot(options?: NestMediatorModuleOptions): DynamicModule;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=nest-mediator.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nest-mediator.module.d.ts","sourceRoot":"","sources":["../../src/lib/nest-mediator.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAU,IAAI,EAAE,YAAY,EAAU,MAAM,gBAAgB,CAAC;AACnF,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD,OAAO,EAAY,eAAe,EAAU,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEzF,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,EAAE,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,GAAG,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC;CACnE;AAED,qBACa,kBAAmB,YAAW,YAAY;IAEjD,OAAO,CAAC,QAAQ,CAAC,eAAe;IAChC,OAAO,CAAC,QAAQ,CAAC,SAAS;IAE1B,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAHR,eAAe,EAAE,eAAe,EAChC,SAAS,EAAE,SAAS,EAEpB,QAAQ,EAAE,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,GAAG,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE;IAGrF,YAAY;IAoCZ;;;;OAIG;IACH,MAAM,CAAC,OAAO,CAAC,OAAO,GAAE,yBAA8B,GAAG,aAAa;CAgBvE"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
var NestMediatorModule_1;
|
|
2
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
3
|
+
import { Module, Inject } from '@nestjs/common';
|
|
4
|
+
import { Reflector } from '@nestjs/core';
|
|
5
|
+
import { MediatorService } from './services/index.js';
|
|
6
|
+
import { COMMAND_HANDLER_METADATA, QUERY_HANDLER_METADATA } from './decorators/index.js';
|
|
7
|
+
let NestMediatorModule = NestMediatorModule_1 = class NestMediatorModule {
|
|
8
|
+
mediatorService;
|
|
9
|
+
reflector;
|
|
10
|
+
handlers;
|
|
11
|
+
constructor(mediatorService, reflector, handlers) {
|
|
12
|
+
this.mediatorService = mediatorService;
|
|
13
|
+
this.reflector = reflector;
|
|
14
|
+
this.handlers = handlers;
|
|
15
|
+
}
|
|
16
|
+
onModuleInit() {
|
|
17
|
+
console.log(`[NestMediator] Registering ${this.handlers.length} handlers...`);
|
|
18
|
+
this.handlers.forEach((handlerType) => {
|
|
19
|
+
const commandMetadata = this.reflector.get(COMMAND_HANDLER_METADATA, handlerType);
|
|
20
|
+
if (commandMetadata) {
|
|
21
|
+
console.log(`[NestMediator] Registering command handler: ${handlerType.name} for command: ${commandMetadata.name}`);
|
|
22
|
+
this.mediatorService.registerCommandHandler(commandMetadata, handlerType);
|
|
23
|
+
}
|
|
24
|
+
const queryMetadata = this.reflector.get(QUERY_HANDLER_METADATA, handlerType);
|
|
25
|
+
if (queryMetadata) {
|
|
26
|
+
console.log(`[NestMediator] Registering query handler: ${handlerType.name} for query: ${queryMetadata.name}`);
|
|
27
|
+
this.mediatorService.registerQueryHandler(queryMetadata, handlerType);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Register the NestMediator module with handlers
|
|
33
|
+
* @param options - Module options containing handlers to register
|
|
34
|
+
* @returns Dynamic module
|
|
35
|
+
*/
|
|
36
|
+
static forRoot(options = {}) {
|
|
37
|
+
const handlers = options.handlers || [];
|
|
38
|
+
return {
|
|
39
|
+
module: NestMediatorModule_1,
|
|
40
|
+
providers: [
|
|
41
|
+
MediatorService,
|
|
42
|
+
{
|
|
43
|
+
provide: 'HANDLERS_CONFIG',
|
|
44
|
+
useValue: handlers,
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
exports: [MediatorService],
|
|
48
|
+
global: true,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
NestMediatorModule = NestMediatorModule_1 = __decorate([
|
|
53
|
+
Module({}),
|
|
54
|
+
__param(2, Inject('HANDLERS_CONFIG')),
|
|
55
|
+
__metadata("design:paramtypes", [MediatorService,
|
|
56
|
+
Reflector, Array])
|
|
57
|
+
], NestMediatorModule);
|
|
58
|
+
export { NestMediatorModule };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './mediator.service.js';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { ModuleRef } from '@nestjs/core';
|
|
3
|
+
import { ICommand, ICommandHandler, IQuery, IQueryHandler } from '../interfaces/index.js';
|
|
4
|
+
export declare class MediatorService {
|
|
5
|
+
private readonly moduleRef;
|
|
6
|
+
private commandHandlers;
|
|
7
|
+
private queryHandlers;
|
|
8
|
+
constructor(moduleRef: ModuleRef);
|
|
9
|
+
/**
|
|
10
|
+
* Register a command handler
|
|
11
|
+
* @param command - The command class
|
|
12
|
+
* @param handler - The handler class
|
|
13
|
+
*/
|
|
14
|
+
registerCommandHandler(command: Type<ICommand>, handler: Type<ICommandHandler<any>>): void;
|
|
15
|
+
/**
|
|
16
|
+
* Register a query handler
|
|
17
|
+
* @param query - The query class
|
|
18
|
+
* @param handler - The handler class
|
|
19
|
+
*/
|
|
20
|
+
registerQueryHandler(query: Type<IQuery>, handler: Type<IQueryHandler<any, any>>): void;
|
|
21
|
+
/**
|
|
22
|
+
* Send a command to its handler
|
|
23
|
+
* @param command - The command instance
|
|
24
|
+
* @returns Promise<void>
|
|
25
|
+
*/
|
|
26
|
+
send<TCommand extends ICommand>(command: TCommand): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Execute a query through its handler
|
|
29
|
+
* @param query - The query instance
|
|
30
|
+
* @returns Promise with the result
|
|
31
|
+
*/
|
|
32
|
+
query<TQuery extends IQuery, TResult = any>(query: TQuery): Promise<TResult>;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=mediator.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mediator.service.d.ts","sourceRoot":"","sources":["../../../src/lib/services/mediator.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAE1F,qBACa,eAAe;IAId,OAAO,CAAC,QAAQ,CAAC,SAAS;IAHtC,OAAO,CAAC,eAAe,CAAiD;IACxE,OAAO,CAAC,aAAa,CAAoD;gBAE5C,SAAS,EAAE,SAAS;IAEjD;;;;OAIG;IACH,sBAAsB,CACpB,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,EACvB,OAAO,EAAE,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,GAClC,IAAI;IAQP;;;;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;IACG,IAAI,CAAC,QAAQ,SAAS,QAAQ,EAClC,OAAO,EAAE,QAAQ,GAChB,OAAO,CAAC,IAAI,CAAC;IAkBhB;;;;OAIG;IACG,KAAK,CAAC,MAAM,SAAS,MAAM,EAAE,OAAO,GAAG,GAAG,EAC9C,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,OAAO,CAAC;CAiBpB"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { Injectable } from '@nestjs/common';
|
|
3
|
+
import { ModuleRef } from '@nestjs/core';
|
|
4
|
+
let MediatorService = class MediatorService {
|
|
5
|
+
moduleRef;
|
|
6
|
+
commandHandlers = new Map();
|
|
7
|
+
queryHandlers = new Map();
|
|
8
|
+
constructor(moduleRef) {
|
|
9
|
+
this.moduleRef = moduleRef;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Register a command handler
|
|
13
|
+
* @param command - The command class
|
|
14
|
+
* @param handler - The handler class
|
|
15
|
+
*/
|
|
16
|
+
registerCommandHandler(command, handler) {
|
|
17
|
+
const commandName = command.name;
|
|
18
|
+
if (this.commandHandlers.has(commandName)) {
|
|
19
|
+
throw new Error(`Command handler for ${commandName} is already registered`);
|
|
20
|
+
}
|
|
21
|
+
this.commandHandlers.set(commandName, handler);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Register a query handler
|
|
25
|
+
* @param query - The query class
|
|
26
|
+
* @param handler - The handler class
|
|
27
|
+
*/
|
|
28
|
+
registerQueryHandler(query, handler) {
|
|
29
|
+
const queryName = query.name;
|
|
30
|
+
if (this.queryHandlers.has(queryName)) {
|
|
31
|
+
throw new Error(`Query handler for ${queryName} is already registered`);
|
|
32
|
+
}
|
|
33
|
+
this.queryHandlers.set(queryName, handler);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Send a command to its handler
|
|
37
|
+
* @param command - The command instance
|
|
38
|
+
* @returns Promise<void>
|
|
39
|
+
*/
|
|
40
|
+
async send(command) {
|
|
41
|
+
const commandName = command.constructor.name;
|
|
42
|
+
const handlerType = this.commandHandlers.get(commandName);
|
|
43
|
+
if (!handlerType) {
|
|
44
|
+
throw new Error(`No handler registered for command: ${commandName}. Did you forget to add @CommandHandler decorator?`);
|
|
45
|
+
}
|
|
46
|
+
const handler = this.moduleRef.get(handlerType, { strict: false });
|
|
47
|
+
await handler.execute(command);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Execute a query through its handler
|
|
51
|
+
* @param query - The query instance
|
|
52
|
+
* @returns Promise with the result
|
|
53
|
+
*/
|
|
54
|
+
async query(query) {
|
|
55
|
+
const queryName = query.constructor.name;
|
|
56
|
+
const handlerType = this.queryHandlers.get(queryName);
|
|
57
|
+
if (!handlerType) {
|
|
58
|
+
throw new Error(`No handler registered for query: ${queryName}. Did you forget to add @QueryHandler decorator?`);
|
|
59
|
+
}
|
|
60
|
+
const handler = this.moduleRef.get(handlerType, { strict: false });
|
|
61
|
+
return handler.execute(query);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
MediatorService = __decorate([
|
|
65
|
+
Injectable(),
|
|
66
|
+
__metadata("design:paramtypes", [ModuleRef])
|
|
67
|
+
], MediatorService);
|
|
68
|
+
export { MediatorService };
|