@nestjs/cqrs 11.0.3 → 12.1.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.
Files changed (149) hide show
  1. package/.circleci/config.yml +3 -3
  2. package/.nvmrc +1 -0
  3. package/dist/aggregate-root.d.ts +3 -67
  4. package/dist/aggregate-root.js +6 -83
  5. package/dist/classes/command.d.ts +2 -2
  6. package/dist/classes/command.js +2 -6
  7. package/dist/classes/constants.js +1 -4
  8. package/dist/classes/index.d.ts +2 -2
  9. package/dist/classes/index.js +2 -18
  10. package/dist/classes/query.d.ts +2 -2
  11. package/dist/classes/query.js +2 -6
  12. package/dist/command-bus.d.ts +5 -5
  13. package/dist/command-bus.js +29 -32
  14. package/dist/constants.js +1 -4
  15. package/dist/cqrs.module.d.ts +5 -5
  16. package/dist/cqrs.module.js +34 -37
  17. package/dist/decorators/command-handler.decorator.d.ts +1 -1
  18. package/dist/decorators/command-handler.decorator.js +9 -13
  19. package/dist/decorators/constants.js +7 -10
  20. package/dist/decorators/events-handler.decorator.d.ts +1 -1
  21. package/dist/decorators/events-handler.decorator.js +9 -13
  22. package/dist/decorators/index.d.ts +5 -5
  23. package/dist/decorators/index.js +5 -21
  24. package/dist/decorators/publishable.decorator.js +3 -6
  25. package/dist/decorators/query-handler.decorator.d.ts +1 -1
  26. package/dist/decorators/query-handler.decorator.js +9 -13
  27. package/dist/decorators/saga.decorator.js +5 -9
  28. package/dist/event-bus.d.ts +10 -8
  29. package/dist/event-bus.js +52 -52
  30. package/dist/event-publisher.d.ts +8 -7
  31. package/dist/event-publisher.js +17 -18
  32. package/dist/exceptions/command-not-found.exception.js +1 -5
  33. package/dist/exceptions/index.d.ts +7 -7
  34. package/dist/exceptions/index.js +7 -23
  35. package/dist/exceptions/invalid-command-handler.exception.js +1 -5
  36. package/dist/exceptions/invalid-events-handler.exception.js +1 -5
  37. package/dist/exceptions/invalid-query-handler.exception.js +1 -5
  38. package/dist/exceptions/invalid-saga.exception.js +1 -5
  39. package/dist/exceptions/query-not-found.exception.js +1 -5
  40. package/dist/exceptions/unsupported-saga-scope.exception.js +1 -5
  41. package/dist/helpers/default-command-pubsub.d.ts +1 -1
  42. package/dist/helpers/default-command-pubsub.js +1 -5
  43. package/dist/helpers/default-event-id-provider.d.ts +1 -1
  44. package/dist/helpers/default-event-id-provider.js +3 -6
  45. package/dist/helpers/default-pubsub.d.ts +1 -1
  46. package/dist/helpers/default-pubsub.js +1 -5
  47. package/dist/helpers/default-query-pubsub.d.ts +1 -1
  48. package/dist/helpers/default-query-pubsub.js +1 -5
  49. package/dist/helpers/default-unhandled-exception-pubsub.d.ts +1 -1
  50. package/dist/helpers/default-unhandled-exception-pubsub.js +1 -5
  51. package/dist/index.d.ts +15 -14
  52. package/dist/index.js +15 -30
  53. package/dist/interfaces/aggregate-root.interface.d.ts +63 -0
  54. package/dist/interfaces/aggregate-root.interface.js +1 -0
  55. package/dist/interfaces/commands/command-bus.interface.d.ts +3 -3
  56. package/dist/interfaces/commands/command-bus.interface.js +1 -2
  57. package/dist/interfaces/commands/command-handler.interface.d.ts +2 -2
  58. package/dist/interfaces/commands/command-handler.interface.js +1 -2
  59. package/dist/interfaces/commands/command-metadata.interface.js +1 -2
  60. package/dist/interfaces/commands/command-publisher.interface.d.ts +1 -1
  61. package/dist/interfaces/commands/command-publisher.interface.js +1 -2
  62. package/dist/interfaces/commands/command.interface.js +1 -2
  63. package/dist/interfaces/cqrs-module-options.interface.d.ts +5 -5
  64. package/dist/interfaces/cqrs-module-options.interface.js +1 -2
  65. package/dist/interfaces/events/event-bus.interface.d.ts +2 -2
  66. package/dist/interfaces/events/event-bus.interface.js +1 -2
  67. package/dist/interfaces/events/event-handler.interface.d.ts +1 -1
  68. package/dist/interfaces/events/event-handler.interface.js +1 -2
  69. package/dist/interfaces/events/event-id-provider.interface.d.ts +1 -1
  70. package/dist/interfaces/events/event-id-provider.interface.js +1 -2
  71. package/dist/interfaces/events/event-publisher.interface.d.ts +2 -2
  72. package/dist/interfaces/events/event-publisher.interface.js +1 -2
  73. package/dist/interfaces/events/event.interface.js +1 -2
  74. package/dist/interfaces/events/message-source.interface.d.ts +1 -1
  75. package/dist/interfaces/events/message-source.interface.js +1 -2
  76. package/dist/interfaces/exceptions/unhandled-exception-info.interface.d.ts +2 -2
  77. package/dist/interfaces/exceptions/unhandled-exception-info.interface.js +1 -2
  78. package/dist/interfaces/exceptions/unhandled-exception-publisher.interface.d.ts +3 -3
  79. package/dist/interfaces/exceptions/unhandled-exception-publisher.interface.js +1 -2
  80. package/dist/interfaces/index.d.ts +20 -19
  81. package/dist/interfaces/index.js +20 -35
  82. package/dist/interfaces/providers-introspection-result.interface.d.ts +4 -4
  83. package/dist/interfaces/providers-introspection-result.interface.js +1 -2
  84. package/dist/interfaces/queries/query-bus.interface.d.ts +3 -3
  85. package/dist/interfaces/queries/query-bus.interface.js +1 -2
  86. package/dist/interfaces/queries/query-handler.interface.d.ts +2 -2
  87. package/dist/interfaces/queries/query-handler.interface.js +1 -2
  88. package/dist/interfaces/queries/query-metadata.interface.js +1 -2
  89. package/dist/interfaces/queries/query-publisher.interface.d.ts +1 -1
  90. package/dist/interfaces/queries/query-publisher.interface.js +1 -2
  91. package/dist/interfaces/queries/query-result.interface.js +1 -2
  92. package/dist/interfaces/queries/query.interface.js +1 -2
  93. package/dist/interfaces/saga.type.d.ts +2 -2
  94. package/dist/interfaces/saga.type.js +1 -2
  95. package/dist/mixins/index.d.ts +1 -0
  96. package/dist/mixins/index.js +1 -0
  97. package/dist/mixins/with-aggregate-root.mixin.d.ts +11 -0
  98. package/dist/mixins/with-aggregate-root.mixin.js +75 -0
  99. package/dist/operators/index.d.ts +1 -1
  100. package/dist/operators/index.js +1 -17
  101. package/dist/operators/of-type.d.ts +1 -1
  102. package/dist/operators/of-type.js +3 -6
  103. package/dist/query-bus.d.ts +5 -5
  104. package/dist/query-bus.js +28 -31
  105. package/dist/scopes/async.context.js +9 -13
  106. package/dist/scopes/index.d.ts +1 -1
  107. package/dist/scopes/index.js +1 -17
  108. package/dist/services/explorer.service.d.ts +5 -5
  109. package/dist/services/explorer.service.js +14 -20
  110. package/dist/storages/aggregate-root.storage.d.ts +3 -3
  111. package/dist/storages/aggregate-root.storage.js +9 -9
  112. package/dist/unhandled-exception-bus.d.ts +2 -2
  113. package/dist/unhandled-exception-bus.js +14 -17
  114. package/dist/utils/index.d.ts +1 -1
  115. package/dist/utils/index.js +1 -17
  116. package/dist/utils/observable-bus.js +3 -7
  117. package/package.json +34 -26
  118. package/test/e2e/aggregate-commit-context.spec.ts +243 -0
  119. package/test/e2e/basic-flows.spec.ts +46 -26
  120. package/test/e2e/generics.spec.ts +3 -3
  121. package/test/e2e/request-scope.spec.ts +23 -22
  122. package/test/e2e/sync-event-handlers-app.spec.ts +123 -0
  123. package/test/e2e/unhandled-exceptions.spec.ts +55 -20
  124. package/tsconfig.build.json +3 -2
  125. package/vitest.config.e2e.ts +13 -0
  126. package/vitest.config.ts +8 -0
  127. package/dist/exceptions/invalid-module-ref.exception.d.ts +0 -3
  128. package/dist/exceptions/invalid-module-ref.exception.js +0 -8
  129. package/dist/helpers/default-get-event-id.d.ts +0 -9
  130. package/dist/helpers/default-get-event-id.js +0 -18
  131. package/dist/helpers/default-get-event-name.d.ts +0 -2
  132. package/dist/helpers/default-get-event-name.js +0 -8
  133. package/dist/interfaces/cqrs-options.interface.d.ts +0 -10
  134. package/dist/interfaces/cqrs-options.interface.js +0 -2
  135. package/dist/interfaces/event-observable.interface.d.ts +0 -4
  136. package/dist/interfaces/event-observable.interface.js +0 -2
  137. package/dist/utils/command-handler.decorator.d.ts +0 -3
  138. package/dist/utils/command-handler.decorator.js +0 -9
  139. package/dist/utils/constants.d.ts +0 -2
  140. package/dist/utils/constants.js +0 -4
  141. package/dist/utils/default-pubsub.d.ts +0 -9
  142. package/dist/utils/default-pubsub.js +0 -14
  143. package/dist/utils/events-handler.decorator.d.ts +0 -3
  144. package/dist/utils/events-handler.decorator.js +0 -9
  145. package/eslint.config.mjs +0 -42
  146. package/index.d.ts +0 -1
  147. package/index.js +0 -6
  148. package/jest.config.js +0 -9
  149. package/test/jest-e2e.json +0 -9
@@ -1,6 +1,6 @@
1
1
  import { InjectableOptions } from '@nestjs/common';
2
2
  import 'reflect-metadata';
3
- import { IEvent } from '../index';
3
+ import { IEvent } from '../index.js';
4
4
  /**
5
5
  * Decorator that marks a class as a Nest event handler. An event handler
6
6
  * handles events executed by your application code.
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EventsHandler = void 0;
4
- const common_1 = require("@nestjs/common");
5
- const crypto_1 = require("crypto");
6
- require("reflect-metadata");
7
- const constants_1 = require("./constants");
1
+ import { Injectable } from '@nestjs/common';
2
+ import { randomUUID } from 'crypto';
3
+ import 'reflect-metadata';
4
+ import { EVENT_METADATA, EVENTS_HANDLER_METADATA } from './constants.js';
8
5
  /**
9
6
  * Decorator that marks a class as a Nest event handler. An event handler
10
7
  * handles events executed by your application code.
@@ -18,19 +15,18 @@ const constants_1 = require("./constants");
18
15
  *
19
16
  * @publicApi
20
17
  */
21
- const EventsHandler = (...events) => {
18
+ export const EventsHandler = (...events) => {
22
19
  return (target) => {
23
20
  const last = events[events.length - 1];
24
21
  if (last && typeof last !== 'function' && 'scope' in last) {
25
- (0, common_1.Injectable)(last)(target);
22
+ Injectable(last)(target);
26
23
  events.pop();
27
24
  }
28
25
  events.forEach((event) => {
29
- if (!Reflect.hasOwnMetadata(constants_1.EVENT_METADATA, event)) {
30
- Reflect.defineMetadata(constants_1.EVENT_METADATA, { id: (0, crypto_1.randomUUID)() }, event);
26
+ if (!Reflect.hasOwnMetadata(EVENT_METADATA, event)) {
27
+ Reflect.defineMetadata(EVENT_METADATA, { id: randomUUID() }, event);
31
28
  }
32
29
  });
33
- Reflect.defineMetadata(constants_1.EVENTS_HANDLER_METADATA, events, target);
30
+ Reflect.defineMetadata(EVENTS_HANDLER_METADATA, events, target);
34
31
  };
35
32
  };
36
- exports.EventsHandler = EventsHandler;
@@ -1,5 +1,5 @@
1
- export * from './command-handler.decorator';
2
- export * from './events-handler.decorator';
3
- export * from './query-handler.decorator';
4
- export * from './saga.decorator';
5
- export * from './publishable.decorator';
1
+ export * from './command-handler.decorator.js';
2
+ export * from './events-handler.decorator.js';
3
+ export * from './query-handler.decorator.js';
4
+ export * from './saga.decorator.js';
5
+ export * from './publishable.decorator.js';
@@ -1,21 +1,5 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./command-handler.decorator"), exports);
18
- __exportStar(require("./events-handler.decorator"), exports);
19
- __exportStar(require("./query-handler.decorator"), exports);
20
- __exportStar(require("./saga.decorator"), exports);
21
- __exportStar(require("./publishable.decorator"), exports);
1
+ export * from './command-handler.decorator.js';
2
+ export * from './events-handler.decorator.js';
3
+ export * from './query-handler.decorator.js';
4
+ export * from './saga.decorator.js';
5
+ export * from './publishable.decorator.js';
@@ -1,15 +1,12 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Publishable = Publishable;
4
- const aggregate_root_storage_1 = require("../storages/aggregate-root.storage");
1
+ import { AggregateRootStorage } from '../storages/aggregate-root.storage.js';
5
2
  /**
6
3
  * Merges event publisher with the decorated class.
7
4
  * Implements the `publish` and `publishAll` methods in a similar way to {@link EventPublisher#mergeClassContext}.
8
5
  *
9
6
  * @publicApi
10
7
  */
11
- function Publishable() {
8
+ export function Publishable() {
12
9
  return (target) => {
13
- aggregate_root_storage_1.AggregateRootStorage.add(target);
10
+ AggregateRootStorage.add(target);
14
11
  };
15
12
  }
@@ -1,6 +1,6 @@
1
1
  import { InjectableOptions } from '@nestjs/common';
2
2
  import 'reflect-metadata';
3
- import { IQuery } from '../interfaces';
3
+ import { IQuery } from '../interfaces/index.js';
4
4
  /**
5
5
  * Decorator that marks a class as a Nest query handler. A query handler
6
6
  * handles queries executed by your application code.
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.QueryHandler = void 0;
4
- const common_1 = require("@nestjs/common");
5
- const crypto_1 = require("crypto");
6
- require("reflect-metadata");
7
- const constants_1 = require("./constants");
1
+ import { Injectable } from '@nestjs/common';
2
+ import { randomUUID } from 'crypto';
3
+ import 'reflect-metadata';
4
+ import { QUERY_HANDLER_METADATA, QUERY_METADATA } from './constants.js';
8
5
  /**
9
6
  * Decorator that marks a class as a Nest query handler. A query handler
10
7
  * handles queries executed by your application code.
@@ -18,15 +15,14 @@ const constants_1 = require("./constants");
18
15
  *
19
16
  * @publicApi
20
17
  */
21
- const QueryHandler = (query, options) => {
18
+ export const QueryHandler = (query, options) => {
22
19
  return (target) => {
23
- if (!Reflect.hasOwnMetadata(constants_1.QUERY_METADATA, query)) {
24
- Reflect.defineMetadata(constants_1.QUERY_METADATA, { id: (0, crypto_1.randomUUID)() }, query);
20
+ if (!Reflect.hasOwnMetadata(QUERY_METADATA, query)) {
21
+ Reflect.defineMetadata(QUERY_METADATA, { id: randomUUID() }, query);
25
22
  }
26
- Reflect.defineMetadata(constants_1.QUERY_HANDLER_METADATA, query, target);
23
+ Reflect.defineMetadata(QUERY_HANDLER_METADATA, query, target);
27
24
  if (options) {
28
- (0, common_1.Injectable)(options)(target);
25
+ Injectable(options)(target);
29
26
  }
30
27
  };
31
28
  };
32
- exports.QueryHandler = QueryHandler;
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Saga = void 0;
4
- require("reflect-metadata");
5
- const constants_1 = require("./constants");
1
+ import 'reflect-metadata';
2
+ import { SAGA_METADATA } from './constants.js';
6
3
  /**
7
4
  * Decorator that marks a class as a Nest saga. Sagas may listen and react to 1..N events.
8
5
  *
@@ -10,10 +7,9 @@ const constants_1 = require("./constants");
10
7
  *
11
8
  * @publicApi
12
9
  */
13
- const Saga = () => {
10
+ export const Saga = () => {
14
11
  return (target, propertyKey) => {
15
- const properties = Reflect.getMetadata(constants_1.SAGA_METADATA, target.constructor) || [];
16
- Reflect.defineMetadata(constants_1.SAGA_METADATA, [...properties, propertyKey], target.constructor);
12
+ const properties = Reflect.getMetadata(SAGA_METADATA, target.constructor) || [];
13
+ Reflect.defineMetadata(SAGA_METADATA, [...properties, propertyKey], target.constructor);
17
14
  };
18
15
  };
19
- exports.Saga = Saga;
@@ -1,13 +1,14 @@
1
1
  import { OnModuleDestroy, Type } from '@nestjs/common';
2
2
  import { ModuleRef } from '@nestjs/core';
3
- import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
4
- import { Observable, Subscription } from 'rxjs';
5
- import { CommandBus } from './command-bus';
6
- import { CqrsModuleOptions, EventIdProvider, IEvent, IEventBus, IEventHandler, IEventPublisher, ISaga } from './interfaces';
7
- import { AsyncContext } from './scopes';
8
- import { UnhandledExceptionBus } from './unhandled-exception-bus';
9
- import { ObservableBus } from './utils';
3
+ import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper.js';
4
+ import { Observable, Subscription, UnaryFunction } from 'rxjs';
5
+ import { CommandBus } from './command-bus.js';
6
+ import { CqrsModuleOptions, EventIdProvider, IEvent, IEventBus, IEventHandler, IEventPublisher, ISaga } from './interfaces/index.js';
7
+ import { AsyncContext } from './scopes/index.js';
8
+ import { UnhandledExceptionBus } from './unhandled-exception-bus.js';
9
+ import { ObservableBus } from './utils/index.js';
10
10
  export type EventHandlerType<EventBase extends IEvent = IEvent> = Type<IEventHandler<EventBase>>;
11
+ export type EventOperator<EventBase extends IEvent = IEvent> = UnaryFunction<Observable<EventBase>, Observable<any>>;
11
12
  /**
12
13
  * @publicApi
13
14
  */
@@ -18,6 +19,7 @@ export declare class EventBus<EventBase extends IEvent = IEvent> extends Observa
18
19
  private readonly options?;
19
20
  protected eventIdProvider: EventIdProvider<EventBase>;
20
21
  protected readonly subscriptions: Subscription[];
22
+ readonly eventOperators: EventOperator<EventBase>[];
21
23
  private _publisher;
22
24
  private readonly _logger;
23
25
  constructor(commandBus: CommandBus, moduleRef: ModuleRef, unhandledExceptionBus: UnhandledExceptionBus, options?: CqrsModuleOptions | undefined);
@@ -85,7 +87,7 @@ export declare class EventBus<EventBase extends IEvent = IEvent> extends Observa
85
87
  registerSagas(wrappers?: InstanceWrapper<object>[]): void;
86
88
  register(handlers?: InstanceWrapper<IEventHandler<EventBase>>[]): void;
87
89
  protected registerHandler(handler: InstanceWrapper<IEventHandler<EventBase>>): void;
88
- protected ofEventId(id: string): Observable<EventBase>;
90
+ protected filterEventWithId(id: string | null): import("rxjs").MonoTypeOperatorFunction<EventBase>;
89
91
  protected registerSaga(saga: ISaga<EventBase>): void;
90
92
  private reflectEvents;
91
93
  private useDefaultPublisher;
package/dist/event-bus.js CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
2
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
3
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -12,35 +11,34 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
11
  return function (target, key) { decorator(target, key, paramIndex); }
13
12
  };
14
13
  var EventBus_1;
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.EventBus = void 0;
17
- const common_1 = require("@nestjs/common");
18
- const core_1 = require("@nestjs/core");
19
- const rxjs_1 = require("rxjs");
20
- const operators_1 = require("rxjs/operators");
21
- const command_bus_1 = require("./command-bus");
22
- const constants_1 = require("./constants");
23
- const constants_2 = require("./decorators/constants");
24
- const exceptions_1 = require("./exceptions");
25
- const default_event_id_provider_1 = require("./helpers/default-event-id-provider");
26
- const default_pubsub_1 = require("./helpers/default-pubsub");
27
- const scopes_1 = require("./scopes");
28
- const unhandled_exception_bus_1 = require("./unhandled-exception-bus");
29
- const utils_1 = require("./utils");
14
+ import { Inject, Injectable, Logger, Optional, } from '@nestjs/common';
15
+ import { ModuleRef } from '@nestjs/core';
16
+ import { Observable, defer, of, pipe } from 'rxjs';
17
+ import { catchError, filter, mergeMap, tap } from 'rxjs/operators';
18
+ import { CommandBus } from './command-bus.js';
19
+ import { CQRS_MODULE_OPTIONS } from './constants.js';
20
+ import { EVENTS_HANDLER_METADATA, SAGA_METADATA, } from './decorators/constants.js';
21
+ import { InvalidSagaException, UnsupportedSagaScopeException, } from './exceptions/index.js';
22
+ import { defaultEventIdProvider } from './helpers/default-event-id-provider.js';
23
+ import { DefaultPubSub } from './helpers/default-pubsub.js';
24
+ import { AsyncContext } from './scopes/index.js';
25
+ import { UnhandledExceptionBus } from './unhandled-exception-bus.js';
26
+ import { ObservableBus } from './utils/index.js';
30
27
  /**
31
28
  * @publicApi
32
29
  */
33
- let EventBus = EventBus_1 = class EventBus extends utils_1.ObservableBus {
30
+ let EventBus = EventBus_1 = class EventBus extends ObservableBus {
34
31
  constructor(commandBus, moduleRef, unhandledExceptionBus, options) {
35
32
  super();
36
33
  this.commandBus = commandBus;
37
34
  this.moduleRef = moduleRef;
38
35
  this.unhandledExceptionBus = unhandledExceptionBus;
39
36
  this.options = options;
40
- this._logger = new common_1.Logger(EventBus_1.name);
37
+ this.eventOperators = [];
38
+ this._logger = new Logger(EventBus_1.name);
41
39
  this.subscriptions = [];
42
40
  this.eventIdProvider =
43
- this.options?.eventIdProvider ?? default_event_id_provider_1.defaultEventIdProvider;
41
+ this.options?.eventIdProvider ?? defaultEventIdProvider;
44
42
  if (this.options?.eventPublisher) {
45
43
  this._publisher = this.options.eventPublisher;
46
44
  }
@@ -73,7 +71,7 @@ let EventBus = EventBus_1 = class EventBus extends utils_1.ObservableBus {
73
71
  * @param asyncContext Async context
74
72
  */
75
73
  publish(event, dispatcherOrAsyncContext, asyncContext) {
76
- if (!asyncContext && dispatcherOrAsyncContext instanceof scopes_1.AsyncContext) {
74
+ if (!asyncContext && dispatcherOrAsyncContext instanceof AsyncContext) {
77
75
  asyncContext = dispatcherOrAsyncContext;
78
76
  dispatcherOrAsyncContext = undefined;
79
77
  }
@@ -89,13 +87,13 @@ let EventBus = EventBus_1 = class EventBus extends utils_1.ObservableBus {
89
87
  * @param asyncContext Async context
90
88
  */
91
89
  publishAll(events, dispatcherOrAsyncContext, asyncContext) {
92
- if (!asyncContext && dispatcherOrAsyncContext instanceof scopes_1.AsyncContext) {
90
+ if (!asyncContext && dispatcherOrAsyncContext instanceof AsyncContext) {
93
91
  asyncContext = dispatcherOrAsyncContext;
94
92
  dispatcherOrAsyncContext = undefined;
95
93
  }
96
94
  if (asyncContext) {
97
95
  events.forEach((event) => {
98
- if (scopes_1.AsyncContext.isAttached(event)) {
96
+ if (AsyncContext.isAttached(event)) {
99
97
  return;
100
98
  }
101
99
  asyncContext.attachTo(event);
@@ -107,38 +105,37 @@ let EventBus = EventBus_1 = class EventBus extends utils_1.ObservableBus {
107
105
  return (events || []).map((event) => this._publisher.publish(event, dispatcherOrAsyncContext, asyncContext));
108
106
  }
109
107
  bind(handler, id) {
110
- const stream$ = id ? this.ofEventId(id) : this.subject$;
111
108
  const deferred = handler.isDependencyTreeStatic()
112
109
  ? (event) => () => {
113
110
  return Promise.resolve(handler.instance.handle(event));
114
111
  }
115
112
  : (event) => async () => {
116
- const asyncContext = scopes_1.AsyncContext.of(event) ?? new scopes_1.AsyncContext();
113
+ const asyncContext = AsyncContext.of(event) ?? new AsyncContext();
117
114
  const instance = await this.moduleRef.resolve(handler.metatype, asyncContext.id, {
118
115
  strict: false,
119
116
  });
120
117
  return instance.handle(event);
121
118
  };
122
- const subscription = stream$
123
- .pipe((0, operators_1.mergeMap)((event) => (0, rxjs_1.defer)(deferred(event)).pipe((0, operators_1.catchError)((error) => {
119
+ const eventOperator = pipe(this.filterEventWithId(id), mergeMap((event) => defer(deferred(event)).pipe(catchError((error) => {
124
120
  if (this.options?.rethrowUnhandled) {
125
121
  throw error;
126
122
  }
127
123
  const unhandledError = this.mapToUnhandledErrorInfo(event, error);
128
124
  this.unhandledExceptionBus.publish(unhandledError);
129
- this._logger.error(`"${handler.constructor.name}" has thrown an unhandled exception.`, error);
130
- return (0, rxjs_1.of)();
131
- }))))
132
- .subscribe();
125
+ this._logger.error(`"${handler.instance?.constructor.name ?? handler.name}" has thrown an unhandled exception.`, error);
126
+ return of();
127
+ }))));
128
+ this.eventOperators.push(eventOperator);
129
+ const subscription = this.subject$.pipe(eventOperator).subscribe();
133
130
  this.subscriptions.push(subscription);
134
131
  }
135
132
  registerSagas(wrappers = []) {
136
133
  const sagas = wrappers
137
134
  .map((wrapper) => {
138
135
  const { metatype: target } = wrapper;
139
- const metadata = Reflect.getMetadata(constants_2.SAGA_METADATA, target) ?? [];
136
+ const metadata = Reflect.getMetadata(SAGA_METADATA, target) ?? [];
140
137
  if (!wrapper.isDependencyTreeStatic()) {
141
- throw new exceptions_1.UnsupportedSagaScopeException();
138
+ throw new UnsupportedSagaScopeException();
142
139
  }
143
140
  const instance = wrapper.instance;
144
141
  return metadata.map((key) => {
@@ -167,49 +164,52 @@ let EventBus = EventBus_1 = class EventBus extends utils_1.ObservableBus {
167
164
  this.bind(handler, eventId);
168
165
  });
169
166
  }
170
- ofEventId(id) {
171
- return this.subject$.pipe((0, operators_1.filter)((event) => {
167
+ filterEventWithId(id) {
168
+ if (!id) {
169
+ return tap();
170
+ }
171
+ return filter((event) => {
172
172
  const { constructor } = Object.getPrototypeOf(event);
173
173
  if (!constructor) {
174
174
  return false;
175
175
  }
176
176
  return this.eventIdProvider.getEventId(constructor) === id;
177
- }));
177
+ });
178
178
  }
179
179
  registerSaga(saga) {
180
180
  if (typeof saga !== 'function') {
181
- throw new exceptions_1.InvalidSagaException();
181
+ throw new InvalidSagaException();
182
182
  }
183
183
  const stream$ = saga(this);
184
- if (!(stream$ instanceof rxjs_1.Observable)) {
185
- throw new exceptions_1.InvalidSagaException();
184
+ if (!(stream$ instanceof Observable)) {
185
+ throw new InvalidSagaException();
186
186
  }
187
187
  const subscription = stream$
188
- .pipe((0, operators_1.filter)((e) => !!e), (0, operators_1.catchError)((error) => {
188
+ .pipe(filter((e) => !!e), catchError((error) => {
189
189
  if (this.options?.rethrowUnhandled) {
190
190
  throw error;
191
191
  }
192
192
  const unhandledError = this.mapToUnhandledErrorInfo(saga.name, error);
193
193
  this.unhandledExceptionBus.publish(unhandledError);
194
194
  this._logger.error(`Saga "${saga.name}" has thrown an unhandled exception.`, error);
195
- return (0, rxjs_1.of)();
196
- }), (0, operators_1.mergeMap)((command) => (0, rxjs_1.defer)(() => this.commandBus.execute(command)).pipe((0, operators_1.catchError)((error) => {
195
+ return of();
196
+ }), mergeMap((command) => defer(() => this.commandBus.execute(command)).pipe(catchError((error) => {
197
197
  if (this.options?.rethrowUnhandled) {
198
198
  throw error;
199
199
  }
200
200
  const unhandledError = this.mapToUnhandledErrorInfo(command, error);
201
201
  this.unhandledExceptionBus.publish(unhandledError);
202
202
  this._logger.error(`Command handler which execution was triggered by Saga has thrown an unhandled exception.`, error);
203
- return (0, rxjs_1.of)();
203
+ return of();
204
204
  }))))
205
205
  .subscribe();
206
206
  this.subscriptions.push(subscription);
207
207
  }
208
208
  reflectEvents(handler) {
209
- return Reflect.getMetadata(constants_2.EVENTS_HANDLER_METADATA, handler);
209
+ return Reflect.getMetadata(EVENTS_HANDLER_METADATA, handler);
210
210
  }
211
211
  useDefaultPublisher() {
212
- this._publisher = new default_pubsub_1.DefaultPubSub(this.subject$);
212
+ this._publisher = new DefaultPubSub(this.subject$);
213
213
  }
214
214
  mapToUnhandledErrorInfo(eventOrCommand, exception) {
215
215
  return {
@@ -218,12 +218,12 @@ let EventBus = EventBus_1 = class EventBus extends utils_1.ObservableBus {
218
218
  };
219
219
  }
220
220
  };
221
- exports.EventBus = EventBus;
222
- exports.EventBus = EventBus = EventBus_1 = __decorate([
223
- (0, common_1.Injectable)(),
224
- __param(3, (0, common_1.Optional)()),
225
- __param(3, (0, common_1.Inject)(constants_1.CQRS_MODULE_OPTIONS)),
226
- __metadata("design:paramtypes", [command_bus_1.CommandBus,
227
- core_1.ModuleRef,
228
- unhandled_exception_bus_1.UnhandledExceptionBus, Object])
221
+ EventBus = EventBus_1 = __decorate([
222
+ Injectable(),
223
+ __param(3, Optional()),
224
+ __param(3, Inject(CQRS_MODULE_OPTIONS)),
225
+ __metadata("design:paramtypes", [CommandBus,
226
+ ModuleRef,
227
+ UnhandledExceptionBus, Object])
229
228
  ], EventBus);
229
+ export { EventBus };
@@ -1,7 +1,6 @@
1
- import { AggregateRoot } from './aggregate-root';
2
- import { EventBus } from './event-bus';
3
- import { IEvent } from './interfaces';
4
- import { AsyncContext } from './scopes';
1
+ import { EventBus } from './event-bus.js';
2
+ import { IAggregateRoot, IEvent } from './interfaces/index.js';
3
+ import { AsyncContext } from './scopes/index.js';
5
4
  export interface Constructor<T> {
6
5
  new (...args: any[]): T;
7
6
  }
@@ -14,15 +13,17 @@ export declare class EventPublisher<EventBase extends IEvent = IEvent> {
14
13
  /**
15
14
  * Merge the event publisher into the provided class.
16
15
  * This is required to make `publish` and `publishAll` available on the `AggregateRoot` class.
16
+ * The aggregate is the dispatcher context, unless `commit()`, `publish()` or `publishAll()` is given one.
17
17
  * @param metatype The class to merge into.
18
18
  * @param asyncContext The async context (if scoped).
19
19
  */
20
- mergeClassContext<T extends Constructor<AggregateRoot<EventBase>>>(metatype: T, asyncContext?: AsyncContext): T;
20
+ mergeClassContext<T extends Constructor<IAggregateRoot<EventBase>>>(metatype: T, asyncContext?: AsyncContext): T;
21
21
  /**
22
22
  * Merge the event publisher into the provided object.
23
- * This is required to make `publish` and `publishAll` available on the `AggregateRoot` class instance.
23
+ * This is required to make `publish` and `publishAll` available on the `IAggregateRoot` class instance.
24
+ * The aggregate is the dispatcher context, unless `commit()`, `publish()` or `publishAll()` is given one.
24
25
  * @param object The object to merge into.
25
26
  * @param asyncContext The async context (if scoped).
26
27
  */
27
- mergeObjectContext<T extends AggregateRoot<EventBase>>(object: T, asyncContext?: AsyncContext): T;
28
+ mergeObjectContext<T extends IAggregateRoot<EventBase>>(object: T, asyncContext?: AsyncContext): T;
28
29
  }
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
2
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
3
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -8,10 +7,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
7
  var __metadata = (this && this.__metadata) || function (k, v) {
9
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
9
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.EventPublisher = void 0;
13
- const common_1 = require("@nestjs/common");
14
- const event_bus_1 = require("./event-bus");
10
+ import { Injectable } from '@nestjs/common';
11
+ import { EventBus } from './event-bus.js';
15
12
  /**
16
13
  * @publicApi
17
14
  */
@@ -22,39 +19,41 @@ let EventPublisher = class EventPublisher {
22
19
  /**
23
20
  * Merge the event publisher into the provided class.
24
21
  * This is required to make `publish` and `publishAll` available on the `AggregateRoot` class.
22
+ * The aggregate is the dispatcher context, unless `commit()`, `publish()` or `publishAll()` is given one.
25
23
  * @param metatype The class to merge into.
26
24
  * @param asyncContext The async context (if scoped).
27
25
  */
28
26
  mergeClassContext(metatype, asyncContext) {
29
27
  const eventBus = this.eventBus;
30
28
  return class extends metatype {
31
- publish(event) {
32
- eventBus.publish(event, this, asyncContext);
29
+ publish(event, dispatcherContext) {
30
+ return eventBus.publish(event, dispatcherContext === undefined ? this : dispatcherContext, asyncContext);
33
31
  }
34
- publishAll(events) {
35
- eventBus.publishAll(events, this, asyncContext);
32
+ publishAll(events, dispatcherContext) {
33
+ return eventBus.publishAll(events, dispatcherContext === undefined ? this : dispatcherContext, asyncContext);
36
34
  }
37
35
  };
38
36
  }
39
37
  /**
40
38
  * Merge the event publisher into the provided object.
41
- * This is required to make `publish` and `publishAll` available on the `AggregateRoot` class instance.
39
+ * This is required to make `publish` and `publishAll` available on the `IAggregateRoot` class instance.
40
+ * The aggregate is the dispatcher context, unless `commit()`, `publish()` or `publishAll()` is given one.
42
41
  * @param object The object to merge into.
43
42
  * @param asyncContext The async context (if scoped).
44
43
  */
45
44
  mergeObjectContext(object, asyncContext) {
46
45
  const eventBus = this.eventBus;
47
- object.publish = (event) => {
48
- eventBus.publish(event, object, asyncContext);
46
+ object.publish = (event, dispatcherContext) => {
47
+ return eventBus.publish(event, dispatcherContext === undefined ? object : dispatcherContext, asyncContext);
49
48
  };
50
- object.publishAll = (events) => {
51
- eventBus.publishAll(events, object, asyncContext);
49
+ object.publishAll = (events, dispatcherContext) => {
50
+ return eventBus.publishAll(events, dispatcherContext === undefined ? object : dispatcherContext, asyncContext);
52
51
  };
53
52
  return object;
54
53
  }
55
54
  };
56
- exports.EventPublisher = EventPublisher;
57
- exports.EventPublisher = EventPublisher = __decorate([
58
- (0, common_1.Injectable)(),
59
- __metadata("design:paramtypes", [event_bus_1.EventBus])
55
+ EventPublisher = __decorate([
56
+ Injectable(),
57
+ __metadata("design:paramtypes", [EventBus])
60
58
  ], EventPublisher);
59
+ export { EventPublisher };
@@ -1,12 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CommandHandlerNotFoundException = void 0;
4
1
  /**
5
2
  * @publicApi
6
3
  */
7
- class CommandHandlerNotFoundException extends Error {
4
+ export class CommandHandlerNotFoundException extends Error {
8
5
  constructor(commandName) {
9
6
  super(`No handler found for the command: "${commandName}".`);
10
7
  }
11
8
  }
12
- exports.CommandHandlerNotFoundException = CommandHandlerNotFoundException;
@@ -1,7 +1,7 @@
1
- export * from './command-not-found.exception';
2
- export * from './invalid-command-handler.exception';
3
- export * from './invalid-events-handler.exception';
4
- export * from './invalid-query-handler.exception';
5
- export * from './invalid-saga.exception';
6
- export * from './query-not-found.exception';
7
- export * from './unsupported-saga-scope.exception';
1
+ export * from './command-not-found.exception.js';
2
+ export * from './invalid-command-handler.exception.js';
3
+ export * from './invalid-events-handler.exception.js';
4
+ export * from './invalid-query-handler.exception.js';
5
+ export * from './invalid-saga.exception.js';
6
+ export * from './query-not-found.exception.js';
7
+ export * from './unsupported-saga-scope.exception.js';
@@ -1,23 +1,7 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./command-not-found.exception"), exports);
18
- __exportStar(require("./invalid-command-handler.exception"), exports);
19
- __exportStar(require("./invalid-events-handler.exception"), exports);
20
- __exportStar(require("./invalid-query-handler.exception"), exports);
21
- __exportStar(require("./invalid-saga.exception"), exports);
22
- __exportStar(require("./query-not-found.exception"), exports);
23
- __exportStar(require("./unsupported-saga-scope.exception"), exports);
1
+ export * from './command-not-found.exception.js';
2
+ export * from './invalid-command-handler.exception.js';
3
+ export * from './invalid-events-handler.exception.js';
4
+ export * from './invalid-query-handler.exception.js';
5
+ export * from './invalid-saga.exception.js';
6
+ export * from './query-not-found.exception.js';
7
+ export * from './unsupported-saga-scope.exception.js';
@@ -1,12 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidCommandHandlerException = void 0;
4
1
  /**
5
2
  * @publicApi
6
3
  */
7
- class InvalidCommandHandlerException extends Error {
4
+ export class InvalidCommandHandlerException extends Error {
8
5
  constructor() {
9
6
  super(`An invalid command handler has been provided. Please ensure that the provided handler is a class annotated with @CommandHandler and contains an 'execute' method.`);
10
7
  }
11
8
  }
12
- exports.InvalidCommandHandlerException = InvalidCommandHandlerException;
@@ -1,12 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidEventsHandlerException = void 0;
4
1
  /**
5
2
  * @publicApi
6
3
  */
7
- class InvalidEventsHandlerException extends Error {
4
+ export class InvalidEventsHandlerException extends Error {
8
5
  constructor() {
9
6
  super(`An invalid events handler has been provided. Please ensure that the provided handler is a class annotated with @EventsHandler and contains a 'handle' method.`);
10
7
  }
11
8
  }
12
- exports.InvalidEventsHandlerException = InvalidEventsHandlerException;