@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,46 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { ModuleRef, Reflector } from '@nestjs/core';
|
|
3
|
+
import { IPipelineBehavior, PipelineBehaviorOptions } from '../interfaces/pipeline-behavior.interface.js';
|
|
4
|
+
/**
|
|
5
|
+
* Registered behavior with its metadata
|
|
6
|
+
*/
|
|
7
|
+
export interface RegisteredBehavior {
|
|
8
|
+
type: Type<IPipelineBehavior<any, any>>;
|
|
9
|
+
options: PipelineBehaviorOptions;
|
|
10
|
+
/**
|
|
11
|
+
* The specific request type this behavior applies to.
|
|
12
|
+
* When undefined, behavior applies to all requests.
|
|
13
|
+
*/
|
|
14
|
+
requestType?: Function;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Orchestrates pipeline behaviors for commands and queries.
|
|
18
|
+
* Manages behavior registration and builds execution pipelines.
|
|
19
|
+
*/
|
|
20
|
+
export declare class PipelineOrchestrator {
|
|
21
|
+
private readonly moduleRef;
|
|
22
|
+
private readonly reflector;
|
|
23
|
+
private readonly logger;
|
|
24
|
+
private readonly behaviors;
|
|
25
|
+
constructor(moduleRef: ModuleRef, reflector: Reflector);
|
|
26
|
+
/**
|
|
27
|
+
* Register a pipeline behavior
|
|
28
|
+
* @param behaviorType - The behavior class
|
|
29
|
+
* @param options - Behavior options (priority, scope)
|
|
30
|
+
* @param requestType - Optional specific request type this behavior applies to
|
|
31
|
+
*/
|
|
32
|
+
registerBehavior(behaviorType: Type<IPipelineBehavior<any, any>>, options: PipelineBehaviorOptions, requestType?: Function): void;
|
|
33
|
+
/**
|
|
34
|
+
* Build a pipeline of behaviors around the handler
|
|
35
|
+
* @param request - The request object (command or query)
|
|
36
|
+
* @param scope - The scope ('command' or 'query')
|
|
37
|
+
* @param handler - The innermost handler function
|
|
38
|
+
* @returns A function that executes the full pipeline
|
|
39
|
+
*/
|
|
40
|
+
buildPipeline<TRequest, TResponse>(request: TRequest, scope: 'command' | 'query', handler: () => Promise<TResponse>): () => Promise<TResponse>;
|
|
41
|
+
/**
|
|
42
|
+
* Get registered behavior names (for debugging)
|
|
43
|
+
*/
|
|
44
|
+
getRegisteredBehaviors(): string[];
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=pipeline.orchestrator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.orchestrator.d.ts","sourceRoot":"","sources":["../../../src/lib/services/pipeline.orchestrator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EACL,iBAAiB,EACjB,uBAAuB,EACxB,MAAM,8CAA8C,CAAC;AAGtD;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IACxC,OAAO,EAAE,uBAAuB,CAAC;IACjC;;;OAGG;IACH,WAAW,CAAC,EAAE,QAAQ,CAAC;CACxB;AAED;;;GAGG;AACH,qBACa,oBAAoB;IAK7B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAL5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsC;IAC7D,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA4B;gBAGnC,SAAS,EAAE,SAAS,EACpB,SAAS,EAAE,SAAS;IAGvC;;;;;OAKG;IACH,gBAAgB,CACd,YAAY,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAC/C,OAAO,EAAE,uBAAuB,EAChC,WAAW,CAAC,EAAE,QAAQ,GACrB,IAAI;IAcP;;;;;;OAMG;IACH,aAAa,CAAC,QAAQ,EAAE,SAAS,EAC/B,OAAO,EAAE,QAAQ,EACjB,KAAK,EAAE,SAAS,GAAG,OAAO,EAC1B,OAAO,EAAE,MAAM,OAAO,CAAC,SAAS,CAAC,GAChC,MAAM,OAAO,CAAC,SAAS,CAAC;IA8C3B;;OAEG;IACH,sBAAsB,IAAI,MAAM,EAAE;CAGnC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
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.PipelineOrchestrator = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@nestjs/core");
|
|
15
|
+
const skip_behavior_decorator_js_1 = require("../decorators/skip-behavior.decorator.js");
|
|
16
|
+
/**
|
|
17
|
+
* Orchestrates pipeline behaviors for commands and queries.
|
|
18
|
+
* Manages behavior registration and builds execution pipelines.
|
|
19
|
+
*/
|
|
20
|
+
let PipelineOrchestrator = class PipelineOrchestrator {
|
|
21
|
+
constructor(moduleRef, reflector) {
|
|
22
|
+
this.moduleRef = moduleRef;
|
|
23
|
+
this.reflector = reflector;
|
|
24
|
+
this.logger = new common_1.Logger('PipelineOrchestrator');
|
|
25
|
+
this.behaviors = [];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Register a pipeline behavior
|
|
29
|
+
* @param behaviorType - The behavior class
|
|
30
|
+
* @param options - Behavior options (priority, scope)
|
|
31
|
+
* @param requestType - Optional specific request type this behavior applies to
|
|
32
|
+
*/
|
|
33
|
+
registerBehavior(behaviorType, options, requestType) {
|
|
34
|
+
this.behaviors.push({ type: behaviorType, options, requestType });
|
|
35
|
+
// Keep behaviors sorted by priority (lower first)
|
|
36
|
+
this.behaviors.sort((a, b) => (a.options.priority ?? 0) - (b.options.priority ?? 0));
|
|
37
|
+
const requestTypeInfo = requestType ? `, requestType: ${requestType.name}` : '';
|
|
38
|
+
this.logger.log(`Registered pipeline behavior: ${behaviorType.name} (priority: ${options.priority ?? 0}, scope: ${options.scope ?? 'all'}${requestTypeInfo})`);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Build a pipeline of behaviors around the handler
|
|
42
|
+
* @param request - The request object (command or query)
|
|
43
|
+
* @param scope - The scope ('command' or 'query')
|
|
44
|
+
* @param handler - The innermost handler function
|
|
45
|
+
* @returns A function that executes the full pipeline
|
|
46
|
+
*/
|
|
47
|
+
buildPipeline(request, scope, handler) {
|
|
48
|
+
// Get behaviors to skip from request metadata
|
|
49
|
+
const requestClass = request.constructor;
|
|
50
|
+
const behaviorsToSkip = this.reflector.get(skip_behavior_decorator_js_1.SKIP_BEHAVIORS_METADATA, requestClass) ?? [];
|
|
51
|
+
// Filter behaviors by scope, skip list, and request type
|
|
52
|
+
const applicableBehaviors = this.behaviors.filter((b) => {
|
|
53
|
+
// Check scope
|
|
54
|
+
const behaviorScope = b.options.scope ?? 'all';
|
|
55
|
+
const scopeMatches = behaviorScope === 'all' || behaviorScope === scope;
|
|
56
|
+
// Check if this behavior should be skipped
|
|
57
|
+
const shouldSkip = behaviorsToSkip.some((skipType) => skipType === b.type);
|
|
58
|
+
// Check request type match
|
|
59
|
+
const requestTypeMatches = !b.requestType || request instanceof b.requestType;
|
|
60
|
+
return scopeMatches && !shouldSkip && requestTypeMatches;
|
|
61
|
+
});
|
|
62
|
+
// If no behaviors, just return the handler
|
|
63
|
+
if (applicableBehaviors.length === 0) {
|
|
64
|
+
return handler;
|
|
65
|
+
}
|
|
66
|
+
// Build the pipeline from right to left (innermost to outermost)
|
|
67
|
+
return applicableBehaviors.reduceRight((next, registeredBehavior) => {
|
|
68
|
+
return async () => {
|
|
69
|
+
const behavior = this.moduleRef.get(registeredBehavior.type, { strict: false });
|
|
70
|
+
return behavior.handle(request, next);
|
|
71
|
+
};
|
|
72
|
+
}, handler);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get registered behavior names (for debugging)
|
|
76
|
+
*/
|
|
77
|
+
getRegisteredBehaviors() {
|
|
78
|
+
return this.behaviors.map((b) => b.type.name);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
exports.PipelineOrchestrator = PipelineOrchestrator;
|
|
82
|
+
exports.PipelineOrchestrator = PipelineOrchestrator = __decorate([
|
|
83
|
+
(0, common_1.Injectable)(),
|
|
84
|
+
__metadata("design:paramtypes", [core_1.ModuleRef,
|
|
85
|
+
core_1.Reflector])
|
|
86
|
+
], PipelineOrchestrator);
|
|
87
|
+
//# sourceMappingURL=pipeline.orchestrator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.orchestrator.js","sourceRoot":"","sources":["../../../src/lib/services/pipeline.orchestrator.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA0D;AAC1D,uCAAoD;AAKpD,yFAAmF;AAenF;;;GAGG;AAEI,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAI/B,YACmB,SAAoB,EACpB,SAAoB;QADpB,cAAS,GAAT,SAAS,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
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { ModuleRef } from '@nestjs/core';
|
|
3
|
+
import { IQuery, IQueryHandler, IQueryBus } from '../interfaces/index.js';
|
|
4
|
+
import { PipelineOrchestrator } from './pipeline.orchestrator.js';
|
|
5
|
+
/**
|
|
6
|
+
* Query bus implementation.
|
|
7
|
+
* Dispatches queries to their handlers through the pipeline.
|
|
8
|
+
*/
|
|
9
|
+
export declare class QueryBus implements IQueryBus {
|
|
10
|
+
private readonly moduleRef;
|
|
11
|
+
private readonly pipelineOrchestrator;
|
|
12
|
+
private readonly handlers;
|
|
13
|
+
constructor(moduleRef: ModuleRef, pipelineOrchestrator: PipelineOrchestrator);
|
|
14
|
+
/**
|
|
15
|
+
* Execute a query through its handler and the pipeline
|
|
16
|
+
* @param query - The query instance
|
|
17
|
+
* @returns Promise with the result
|
|
18
|
+
*/
|
|
19
|
+
query<TQuery extends IQuery, TResult = any>(query: TQuery): Promise<TResult>;
|
|
20
|
+
/**
|
|
21
|
+
* Register a query handler
|
|
22
|
+
* @param query - The query class
|
|
23
|
+
* @param handler - The handler class
|
|
24
|
+
*/
|
|
25
|
+
registerQueryHandler(query: Type<IQuery>, handler: Type<IQueryHandler<any, any>>): void;
|
|
26
|
+
/**
|
|
27
|
+
* Get registered query names (for debugging)
|
|
28
|
+
*/
|
|
29
|
+
getRegisteredQueries(): string[];
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=query.bus.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.bus.d.ts","sourceRoot":"","sources":["../../../src/lib/services/query.bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAE1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAElE;;;GAGG;AACH,qBACa,QAAS,YAAW,SAAS;IAItC,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,oBAAoB;IAJvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoD;gBAG1D,SAAS,EAAE,SAAS,EACpB,oBAAoB,EAAE,oBAAoB;IAG7D;;;;OAIG;IACG,KAAK,CAAC,MAAM,SAAS,MAAM,EAAE,OAAO,GAAG,GAAG,EAC9C,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,OAAO,CAAC;IAuBnB;;;;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;;OAEG;IACH,oBAAoB,IAAI,MAAM,EAAE;CAGjC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
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.QueryBus = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@nestjs/core");
|
|
15
|
+
const handler_not_found_exception_js_1 = require("../exceptions/handler-not-found.exception.js");
|
|
16
|
+
const pipeline_orchestrator_js_1 = require("./pipeline.orchestrator.js");
|
|
17
|
+
/**
|
|
18
|
+
* Query bus implementation.
|
|
19
|
+
* Dispatches queries to their handlers through the pipeline.
|
|
20
|
+
*/
|
|
21
|
+
let QueryBus = class QueryBus {
|
|
22
|
+
constructor(moduleRef, pipelineOrchestrator) {
|
|
23
|
+
this.moduleRef = moduleRef;
|
|
24
|
+
this.pipelineOrchestrator = pipelineOrchestrator;
|
|
25
|
+
this.handlers = new Map();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Execute a query through its handler and the pipeline
|
|
29
|
+
* @param query - The query instance
|
|
30
|
+
* @returns Promise with the result
|
|
31
|
+
*/
|
|
32
|
+
async query(query) {
|
|
33
|
+
const queryName = query.constructor.name;
|
|
34
|
+
const handlerType = this.handlers.get(queryName);
|
|
35
|
+
if (!handlerType) {
|
|
36
|
+
throw new handler_not_found_exception_js_1.HandlerNotFoundException(queryName, 'query');
|
|
37
|
+
}
|
|
38
|
+
const handler = this.moduleRef.get(handlerType, { strict: false });
|
|
39
|
+
// Build and execute pipeline
|
|
40
|
+
const pipeline = this.pipelineOrchestrator.buildPipeline(query, 'query', () => handler.execute(query));
|
|
41
|
+
return pipeline();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Register a query handler
|
|
45
|
+
* @param query - The query class
|
|
46
|
+
* @param handler - The handler class
|
|
47
|
+
*/
|
|
48
|
+
registerQueryHandler(query, handler) {
|
|
49
|
+
const queryName = query.name;
|
|
50
|
+
if (this.handlers.has(queryName)) {
|
|
51
|
+
throw new Error(`Query handler for ${queryName} is already registered`);
|
|
52
|
+
}
|
|
53
|
+
this.handlers.set(queryName, handler);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Get registered query names (for debugging)
|
|
57
|
+
*/
|
|
58
|
+
getRegisteredQueries() {
|
|
59
|
+
return Array.from(this.handlers.keys());
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
exports.QueryBus = QueryBus;
|
|
63
|
+
exports.QueryBus = QueryBus = __decorate([
|
|
64
|
+
(0, common_1.Injectable)(),
|
|
65
|
+
__metadata("design:paramtypes", [core_1.ModuleRef,
|
|
66
|
+
pipeline_orchestrator_js_1.PipelineOrchestrator])
|
|
67
|
+
], QueryBus);
|
|
68
|
+
//# sourceMappingURL=query.bus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.bus.js","sourceRoot":"","sources":["../../../src/lib/services/query.bus.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAkD;AAClD,uCAAyC;AAEzC,iGAAwF;AACxF,yEAAkE;AAElE;;;GAGG;AAEI,IAAM,QAAQ,GAAd,MAAM,QAAQ;IAGnB,YACmB,SAAoB,EACpB,oBAA0C;QAD1C,cAAS,GAAT,SAAS,CAAW;QACpB,yBAAoB,GAApB,oBAAoB,CAAsB;QAJ5C,aAAQ,GAAG,IAAI,GAAG,EAAyC,CAAC;IAK1E,CAAC;IAEJ;;;;OAIG;IACH,KAAK,CAAC,KAAK,CACT,KAAa;QAEb,MAAM,SAAS,GAAG,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC;QACzC,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAEjD,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,MAAM,IAAI,yDAAwB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QACzD,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAChC,WAAW,EACX,EAAE,MAAM,EAAE,KAAK,EAAE,CAClB,CAAC;QAEF,6BAA6B;QAC7B,MAAM,QAAQ,GAAG,IAAI,CAAC,oBAAoB,CAAC,aAAa,CACtD,KAAK,EACL,OAAO,EACP,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAC7B,CAAC;QAEF,OAAO,QAAQ,EAAE,CAAC;IACpB,CAAC;IAED;;;;OAIG;IACH,oBAAoB,CAClB,KAAmB,EACnB,OAAsC;QAEtC,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC;QAC7B,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,qBAAqB,SAAS,wBAAwB,CAAC,CAAC;QAC1E,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IACxC,CAAC;IAED;;OAEG;IACH,oBAAoB;QAClB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IAC1C,CAAC;CACF,CAAA;AA5DY,4BAAQ;mBAAR,QAAQ;IADpB,IAAA,mBAAU,GAAE;qCAKmB,gBAAS;QACE,+CAAoB;GALlD,QAAQ,CA4DpB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rolandsall24/nest-mediator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.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",
|