@nestjs/cqrs 11.0.2 → 12.0.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 (157) hide show
  1. package/.circleci/config.yml +3 -3
  2. package/.nvmrc +1 -0
  3. package/README.md +0 -1
  4. package/dist/aggregate-root.d.ts +3 -67
  5. package/dist/aggregate-root.js +6 -83
  6. package/dist/classes/command.d.ts +2 -2
  7. package/dist/classes/command.js +2 -6
  8. package/dist/classes/constants.js +1 -4
  9. package/dist/classes/index.d.ts +2 -2
  10. package/dist/classes/index.js +2 -18
  11. package/dist/classes/query.d.ts +2 -2
  12. package/dist/classes/query.js +2 -6
  13. package/dist/command-bus.d.ts +8 -5
  14. package/dist/command-bus.js +32 -32
  15. package/dist/constants.js +1 -4
  16. package/dist/cqrs.module.d.ts +8 -5
  17. package/dist/cqrs.module.js +37 -37
  18. package/dist/decorators/command-handler.decorator.d.ts +3 -1
  19. package/dist/decorators/command-handler.decorator.js +11 -13
  20. package/dist/decorators/constants.js +7 -10
  21. package/dist/decorators/events-handler.decorator.d.ts +3 -1
  22. package/dist/decorators/events-handler.decorator.js +12 -14
  23. package/dist/decorators/index.d.ts +5 -5
  24. package/dist/decorators/index.js +5 -21
  25. package/dist/decorators/publishable.decorator.d.ts +2 -0
  26. package/dist/decorators/publishable.decorator.js +5 -6
  27. package/dist/decorators/query-handler.decorator.d.ts +3 -1
  28. package/dist/decorators/query-handler.decorator.js +11 -13
  29. package/dist/decorators/saga.decorator.d.ts +2 -0
  30. package/dist/decorators/saga.decorator.js +7 -9
  31. package/dist/event-bus.d.ts +13 -8
  32. package/dist/event-bus.js +54 -51
  33. package/dist/event-publisher.d.ts +9 -7
  34. package/dist/event-publisher.js +10 -10
  35. package/dist/exceptions/command-not-found.exception.d.ts +3 -0
  36. package/dist/exceptions/command-not-found.exception.js +4 -5
  37. package/dist/exceptions/index.d.ts +7 -7
  38. package/dist/exceptions/index.js +7 -23
  39. package/dist/exceptions/invalid-command-handler.exception.d.ts +3 -0
  40. package/dist/exceptions/invalid-command-handler.exception.js +4 -5
  41. package/dist/exceptions/invalid-events-handler.exception.d.ts +3 -0
  42. package/dist/exceptions/invalid-events-handler.exception.js +4 -5
  43. package/dist/exceptions/invalid-query-handler.exception.js +1 -5
  44. package/dist/exceptions/invalid-saga.exception.d.ts +3 -0
  45. package/dist/exceptions/invalid-saga.exception.js +4 -5
  46. package/dist/exceptions/query-not-found.exception.d.ts +3 -0
  47. package/dist/exceptions/query-not-found.exception.js +4 -5
  48. package/dist/exceptions/unsupported-saga-scope.exception.d.ts +3 -0
  49. package/dist/exceptions/unsupported-saga-scope.exception.js +4 -5
  50. package/dist/helpers/default-command-pubsub.d.ts +1 -1
  51. package/dist/helpers/default-command-pubsub.js +1 -5
  52. package/dist/helpers/default-event-id-provider.d.ts +1 -1
  53. package/dist/helpers/default-event-id-provider.js +3 -6
  54. package/dist/helpers/default-pubsub.d.ts +1 -1
  55. package/dist/helpers/default-pubsub.js +1 -5
  56. package/dist/helpers/default-query-pubsub.d.ts +1 -1
  57. package/dist/helpers/default-query-pubsub.js +1 -5
  58. package/dist/helpers/default-unhandled-exception-pubsub.d.ts +1 -1
  59. package/dist/helpers/default-unhandled-exception-pubsub.js +1 -5
  60. package/dist/index.d.ts +15 -14
  61. package/dist/index.js +15 -30
  62. package/dist/interfaces/aggregate-root.interface.d.ts +53 -0
  63. package/dist/interfaces/aggregate-root.interface.js +1 -0
  64. package/dist/interfaces/commands/command-bus.interface.d.ts +5 -3
  65. package/dist/interfaces/commands/command-bus.interface.js +1 -2
  66. package/dist/interfaces/commands/command-handler.interface.d.ts +4 -2
  67. package/dist/interfaces/commands/command-handler.interface.js +1 -2
  68. package/dist/interfaces/commands/command-metadata.interface.js +1 -2
  69. package/dist/interfaces/commands/command-publisher.interface.d.ts +6 -1
  70. package/dist/interfaces/commands/command-publisher.interface.js +1 -2
  71. package/dist/interfaces/commands/command.interface.js +1 -2
  72. package/dist/interfaces/cqrs-module-options.interface.d.ts +17 -5
  73. package/dist/interfaces/cqrs-module-options.interface.js +1 -2
  74. package/dist/interfaces/events/event-bus.interface.d.ts +2 -2
  75. package/dist/interfaces/events/event-bus.interface.js +1 -2
  76. package/dist/interfaces/events/event-handler.interface.d.ts +1 -1
  77. package/dist/interfaces/events/event-handler.interface.js +1 -2
  78. package/dist/interfaces/events/event-id-provider.interface.d.ts +1 -1
  79. package/dist/interfaces/events/event-id-provider.interface.js +1 -2
  80. package/dist/interfaces/events/event-publisher.interface.d.ts +2 -2
  81. package/dist/interfaces/events/event-publisher.interface.js +1 -2
  82. package/dist/interfaces/events/event.interface.js +1 -2
  83. package/dist/interfaces/events/message-source.interface.d.ts +1 -1
  84. package/dist/interfaces/events/message-source.interface.js +1 -2
  85. package/dist/interfaces/exceptions/unhandled-exception-info.interface.d.ts +2 -2
  86. package/dist/interfaces/exceptions/unhandled-exception-info.interface.js +1 -2
  87. package/dist/interfaces/exceptions/unhandled-exception-publisher.interface.d.ts +3 -3
  88. package/dist/interfaces/exceptions/unhandled-exception-publisher.interface.js +1 -2
  89. package/dist/interfaces/index.d.ts +20 -19
  90. package/dist/interfaces/index.js +20 -35
  91. package/dist/interfaces/providers-introspection-result.interface.d.ts +4 -4
  92. package/dist/interfaces/providers-introspection-result.interface.js +1 -2
  93. package/dist/interfaces/queries/query-bus.interface.d.ts +5 -3
  94. package/dist/interfaces/queries/query-bus.interface.js +1 -2
  95. package/dist/interfaces/queries/query-handler.interface.d.ts +4 -2
  96. package/dist/interfaces/queries/query-handler.interface.js +1 -2
  97. package/dist/interfaces/queries/query-metadata.interface.js +1 -2
  98. package/dist/interfaces/queries/query-publisher.interface.d.ts +6 -1
  99. package/dist/interfaces/queries/query-publisher.interface.js +1 -2
  100. package/dist/interfaces/queries/query-result.interface.js +1 -2
  101. package/dist/interfaces/queries/query.interface.js +1 -2
  102. package/dist/interfaces/saga.type.d.ts +2 -2
  103. package/dist/interfaces/saga.type.js +1 -2
  104. package/dist/mixins/index.d.ts +1 -0
  105. package/dist/mixins/index.js +1 -0
  106. package/dist/mixins/with-aggregate-root.mixin.d.ts +11 -0
  107. package/dist/mixins/with-aggregate-root.mixin.js +67 -0
  108. package/dist/operators/index.d.ts +1 -1
  109. package/dist/operators/index.js +1 -17
  110. package/dist/operators/of-type.d.ts +1 -1
  111. package/dist/operators/of-type.js +3 -6
  112. package/dist/query-bus.d.ts +8 -5
  113. package/dist/query-bus.js +31 -31
  114. package/dist/scopes/async.context.js +9 -13
  115. package/dist/scopes/index.d.ts +1 -1
  116. package/dist/scopes/index.js +1 -17
  117. package/dist/services/explorer.service.d.ts +5 -5
  118. package/dist/services/explorer.service.js +14 -20
  119. package/dist/storages/aggregate-root.storage.d.ts +3 -3
  120. package/dist/storages/aggregate-root.storage.js +1 -5
  121. package/dist/unhandled-exception-bus.d.ts +2 -2
  122. package/dist/unhandled-exception-bus.js +14 -17
  123. package/dist/utils/index.d.ts +1 -1
  124. package/dist/utils/index.js +1 -17
  125. package/dist/utils/observable-bus.js +3 -7
  126. package/package.json +35 -27
  127. package/test/e2e/basic-flows.spec.ts +46 -26
  128. package/test/e2e/generics.spec.ts +3 -3
  129. package/test/e2e/request-scope.spec.ts +23 -22
  130. package/test/e2e/sync-event-handlers-app.spec.ts +123 -0
  131. package/test/e2e/unhandled-exceptions.spec.ts +36 -20
  132. package/tsconfig.build.json +3 -2
  133. package/vitest.config.e2e.ts +13 -0
  134. package/vitest.config.ts +8 -0
  135. package/dist/exceptions/invalid-module-ref.exception.d.ts +0 -3
  136. package/dist/exceptions/invalid-module-ref.exception.js +0 -8
  137. package/dist/helpers/default-get-event-id.d.ts +0 -9
  138. package/dist/helpers/default-get-event-id.js +0 -18
  139. package/dist/helpers/default-get-event-name.d.ts +0 -2
  140. package/dist/helpers/default-get-event-name.js +0 -8
  141. package/dist/interfaces/cqrs-options.interface.d.ts +0 -10
  142. package/dist/interfaces/cqrs-options.interface.js +0 -2
  143. package/dist/interfaces/event-observable.interface.d.ts +0 -4
  144. package/dist/interfaces/event-observable.interface.js +0 -2
  145. package/dist/utils/command-handler.decorator.d.ts +0 -3
  146. package/dist/utils/command-handler.decorator.js +0 -9
  147. package/dist/utils/constants.d.ts +0 -2
  148. package/dist/utils/constants.js +0 -4
  149. package/dist/utils/default-pubsub.d.ts +0 -9
  150. package/dist/utils/default-pubsub.js +0 -14
  151. package/dist/utils/events-handler.decorator.d.ts +0 -3
  152. package/dist/utils/events-handler.decorator.js +0 -9
  153. package/eslint.config.mjs +0 -42
  154. package/index.d.ts +0 -1
  155. package/index.js +0 -6
  156. package/jest.config.js +0 -9
  157. package/test/jest-e2e.json +0 -9
@@ -17,7 +17,7 @@ jobs:
17
17
  build:
18
18
  working_directory: ~/nest
19
19
  docker:
20
- - image: cimg/node:22.3
20
+ - image: cimg/node:24.19.0
21
21
  steps:
22
22
  - checkout
23
23
  - restore_cache:
@@ -35,7 +35,7 @@ jobs:
35
35
  e2e_test:
36
36
  working_directory: ~/nest
37
37
  docker:
38
- - image: cimg/node:22.3
38
+ - image: cimg/node:24.19.0
39
39
  steps:
40
40
  - checkout
41
41
  - *restore-cache
@@ -46,7 +46,7 @@ jobs:
46
46
  test:
47
47
  working_directory: ~/nest
48
48
  docker:
49
- - image: cimg/node:22.3
49
+ - image: cimg/node:24.19.0
50
50
  steps:
51
51
  - checkout
52
52
  - *restore-cache
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 24
package/README.md CHANGED
@@ -12,7 +12,6 @@
12
12
  <a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
13
13
  <a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
14
14
  <a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/dm/@nestjs/core.svg" alt="NPM Downloads" /></a>
15
- <a href="https://coveralls.io/github/nestjs/nest?branch=master"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#5" alt="Coverage" /></a>
16
15
  <a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
17
16
  <a href="https://opencollective.com/nest#backer"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
18
17
  <a href="https://opencollective.com/nest#sponsor"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
@@ -1,7 +1,6 @@
1
1
  import { Type } from '@nestjs/common';
2
- import { IEvent, IEventHandler } from './interfaces';
3
- declare const INTERNAL_EVENTS: unique symbol;
4
- declare const IS_AUTO_COMMIT_ENABLED: unique symbol;
2
+ import { IEvent, IEventHandler } from './interfaces/index.js';
3
+ declare const AggregateRoot_base: abstract new <T extends IEvent = IEvent>(...args: any[]) => import("./interfaces/aggregate-root.interface.js").IAggregateRoot<T> & {};
5
4
  /**
6
5
  * Represents an aggregate root.
7
6
  * An aggregate root is an entity that represents a meaningful concept in the domain.
@@ -9,70 +8,7 @@ declare const IS_AUTO_COMMIT_ENABLED: unique symbol;
9
8
  *
10
9
  * @template EventBase The base type of the events.
11
10
  */
12
- export declare abstract class AggregateRoot<EventBase extends IEvent = IEvent> {
13
- [IS_AUTO_COMMIT_ENABLED]: boolean;
14
- private readonly [INTERNAL_EVENTS];
15
- /**
16
- * Sets whether the aggregate root should automatically commit events.
17
- */
18
- set autoCommit(value: boolean);
19
- /**
20
- * Gets whether the aggregate root should automatically commit events.
21
- */
22
- get autoCommit(): boolean;
23
- /**
24
- * Publishes an event. Must be merged with the publisher context in order to work.
25
- * @param event The event to publish.
26
- */
27
- publish<T extends EventBase = EventBase>(event: T): void;
28
- /**
29
- * Publishes multiple events. Must be merged with the publisher context in order to work.
30
- * @param events The events to publish.
31
- */
32
- publishAll<T extends EventBase = EventBase>(events: T[]): void;
33
- /**
34
- * Commits all uncommitted events.
35
- */
36
- commit(): void;
37
- /**
38
- * Uncommits all events.
39
- */
40
- uncommit(): void;
41
- /**
42
- * Returns all uncommitted events.
43
- * @returns All uncommitted events.
44
- */
45
- getUncommittedEvents(): EventBase[];
46
- /**
47
- * Loads events from history.
48
- * @param history The history to load.
49
- */
50
- loadFromHistory(history: EventBase[]): void;
51
- /**
52
- * Applies an event.
53
- * If auto commit is enabled, the event will be published immediately (note: must be merged with the publisher context in order to work).
54
- * Otherwise, the event will be stored in the internal events array, and will be published when the commit method is called.
55
- * Also, the corresponding event handler will be called (if exists).
56
- * For example, if the event is called UserCreatedEvent, the "onUserCreatedEvent" method will be called.
57
- *
58
- * @param event The event to apply.
59
- * @param isFromHistory Whether the event is from history.
60
- */
61
- apply<T extends EventBase = EventBase>(event: T, isFromHistory?: boolean): void;
62
- /**
63
- * Applies an event.
64
- * If auto commit is enabled, the event will be published immediately (note: must be merged with the publisher context in order to work).
65
- * Otherwise, the event will be stored in the internal events array, and will be published when the commit method is called.
66
- * Also, the corresponding event handler will be called (if exists).
67
- * For example, if the event is called UserCreatedEvent, the "onUserCreatedEvent" method will be called.
68
- *
69
- * @param event The event to apply.
70
- * @param options The options.
71
- */
72
- apply<T extends EventBase = EventBase>(event: T, options?: {
73
- fromHistory?: boolean;
74
- skipHandler?: boolean;
75
- }): void;
11
+ export declare abstract class AggregateRoot<EventBase extends IEvent = IEvent> extends AggregateRoot_base<EventBase> {
76
12
  protected getEventHandler<T extends EventBase = EventBase>(event: T): Type<IEventHandler> | undefined;
77
13
  protected getEventName(event: any): string;
78
14
  }
@@ -1,10 +1,4 @@
1
- "use strict";
2
- /* eslint-disable @typescript-eslint/no-unused-vars */
3
- var _a, _b;
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.AggregateRoot = void 0;
6
- const INTERNAL_EVENTS = Symbol();
7
- const IS_AUTO_COMMIT_ENABLED = Symbol();
1
+ import { WithAggregateRoot } from './mixins/index.js';
8
2
  /**
9
3
  * Represents an aggregate root.
10
4
  * An aggregate root is an entity that represents a meaningful concept in the domain.
@@ -12,84 +6,13 @@ const IS_AUTO_COMMIT_ENABLED = Symbol();
12
6
  *
13
7
  * @template EventBase The base type of the events.
14
8
  */
15
- class AggregateRoot {
16
- constructor() {
17
- this[_a] = false;
18
- this[_b] = [];
19
- }
20
- /**
21
- * Sets whether the aggregate root should automatically commit events.
22
- */
23
- set autoCommit(value) {
24
- this[IS_AUTO_COMMIT_ENABLED] = value;
25
- }
26
- /**
27
- * Gets whether the aggregate root should automatically commit events.
28
- */
29
- get autoCommit() {
30
- return this[IS_AUTO_COMMIT_ENABLED];
31
- }
32
- /**
33
- * Publishes an event. Must be merged with the publisher context in order to work.
34
- * @param event The event to publish.
35
- */
36
- publish(event) { }
37
- /**
38
- * Publishes multiple events. Must be merged with the publisher context in order to work.
39
- * @param events The events to publish.
40
- */
41
- publishAll(events) { }
42
- /**
43
- * Commits all uncommitted events.
44
- */
45
- commit() {
46
- this.publishAll(this[INTERNAL_EVENTS]);
47
- this[INTERNAL_EVENTS].length = 0;
48
- }
49
- /**
50
- * Uncommits all events.
51
- */
52
- uncommit() {
53
- this[INTERNAL_EVENTS].length = 0;
54
- }
55
- /**
56
- * Returns all uncommitted events.
57
- * @returns All uncommitted events.
58
- */
59
- getUncommittedEvents() {
60
- return this[INTERNAL_EVENTS];
61
- }
62
- /**
63
- * Loads events from history.
64
- * @param history The history to load.
65
- */
66
- loadFromHistory(history) {
67
- history.forEach((event) => this.apply(event, true));
68
- }
69
- apply(event, optionsOrIsFromHistory = {}) {
70
- const isFromHistory = (typeof optionsOrIsFromHistory === 'boolean'
71
- ? optionsOrIsFromHistory
72
- : optionsOrIsFromHistory.fromHistory) ?? false;
73
- const skipHandler = typeof optionsOrIsFromHistory === 'boolean'
74
- ? false
75
- : optionsOrIsFromHistory.skipHandler;
76
- if (!isFromHistory && !this.autoCommit) {
77
- this[INTERNAL_EVENTS].push(event);
78
- }
79
- this.autoCommit && this.publish(event);
80
- if (!skipHandler) {
81
- const handler = this.getEventHandler(event);
82
- handler && handler.call(this, event);
83
- }
84
- }
9
+ export class AggregateRoot extends WithAggregateRoot(class {
10
+ }) {
11
+ // redeclaring protected methods to preserve typings (since this typescript version does not support the "declare" modifier on overrides)
85
12
  getEventHandler(event) {
86
- const handler = `on${this.getEventName(event)}`;
87
- return this[handler];
13
+ return super['getEventHandler'](event);
88
14
  }
89
15
  getEventName(event) {
90
- const { constructor } = Object.getPrototypeOf(event);
91
- return constructor.name;
16
+ return super['getEventName'](event);
92
17
  }
93
18
  }
94
- exports.AggregateRoot = AggregateRoot;
95
- _a = IS_AUTO_COMMIT_ENABLED, _b = INTERNAL_EVENTS;
@@ -1,5 +1,5 @@
1
- import { ICommand } from '../interfaces';
2
- import { RESULT_TYPE_SYMBOL } from './constants';
1
+ import { ICommand } from '../interfaces/index.js';
2
+ import { RESULT_TYPE_SYMBOL } from './constants.js';
3
3
  /**
4
4
  * Utility type to extract the result type of a command.
5
5
  */
@@ -1,7 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Command = void 0;
4
- const constants_1 = require("./constants");
5
- class Command {
1
+ import { RESULT_TYPE_SYMBOL } from './constants.js';
2
+ export class Command {
6
3
  }
7
- exports.Command = Command;
@@ -1,4 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RESULT_TYPE_SYMBOL = void 0;
4
- exports.RESULT_TYPE_SYMBOL = Symbol('RESULT_TYPE');
1
+ export const RESULT_TYPE_SYMBOL = Symbol('RESULT_TYPE');
@@ -1,2 +1,2 @@
1
- export * from './command';
2
- export * from './query';
1
+ export * from './command.js';
2
+ export * from './query.js';
@@ -1,18 +1,2 @@
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"), exports);
18
- __exportStar(require("./query"), exports);
1
+ export * from './command.js';
2
+ export * from './query.js';
@@ -1,5 +1,5 @@
1
- import { IQuery } from '../interfaces';
2
- import { RESULT_TYPE_SYMBOL } from './constants';
1
+ import { IQuery } from '../interfaces/index.js';
2
+ import { RESULT_TYPE_SYMBOL } from './constants.js';
3
3
  /**
4
4
  * Utility type to extract the result type of a query.
5
5
  */
@@ -1,7 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Query = void 0;
4
- const constants_1 = require("./constants");
5
- class Query {
1
+ import { RESULT_TYPE_SYMBOL } from './constants.js';
2
+ export class Query {
6
3
  }
7
- exports.Query = Query;
@@ -1,12 +1,15 @@
1
1
  import { Type } from '@nestjs/common';
2
2
  import { ModuleRef } from '@nestjs/core';
3
- import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
3
+ import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper.js';
4
4
  import 'reflect-metadata';
5
- import { Command } from './classes';
6
- import { CqrsModuleOptions, ICommand, ICommandBus, ICommandHandler, ICommandPublisher } from './interfaces/index';
7
- import { AsyncContext } from './scopes/async.context';
8
- import { ObservableBus } from './utils/observable-bus';
5
+ import { Command } from './classes/index.js';
6
+ import { CqrsModuleOptions, ICommand, ICommandBus, ICommandHandler, ICommandPublisher } from './interfaces/index.js';
7
+ import { AsyncContext } from './scopes/async.context.js';
8
+ import { ObservableBus } from './utils/observable-bus.js';
9
9
  export type CommandHandlerType<T extends ICommand = ICommand> = Type<ICommandHandler<T>>;
10
+ /**
11
+ * @publicApi
12
+ */
10
13
  export declare class CommandBus<CommandBase extends ICommand = ICommand> extends ObservableBus<CommandBase> implements ICommandBus<CommandBase> {
11
14
  private readonly moduleRef;
12
15
  private readonly options?;
@@ -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,24 +11,25 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
11
  return function (target, key) { decorator(target, key, paramIndex); }
13
12
  };
14
13
  var CommandBus_1;
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.CommandBus = void 0;
17
- const common_1 = require("@nestjs/common");
18
- const core_1 = require("@nestjs/core");
19
- require("reflect-metadata");
20
- const constants_1 = require("./constants");
21
- const constants_2 = require("./decorators/constants");
22
- const command_not_found_exception_1 = require("./exceptions/command-not-found.exception");
23
- const default_command_pubsub_1 = require("./helpers/default-command-pubsub");
24
- const index_1 = require("./index");
25
- const async_context_1 = require("./scopes/async.context");
26
- const observable_bus_1 = require("./utils/observable-bus");
27
- let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.ObservableBus {
14
+ import { Inject, Injectable, Logger, Optional } from '@nestjs/common';
15
+ import { ModuleRef } from '@nestjs/core';
16
+ import 'reflect-metadata';
17
+ import { CQRS_MODULE_OPTIONS } from './constants.js';
18
+ import { COMMAND_HANDLER_METADATA, COMMAND_METADATA, } from './decorators/constants.js';
19
+ import { CommandHandlerNotFoundException } from './exceptions/command-not-found.exception.js';
20
+ import { DefaultCommandPubSub } from './helpers/default-command-pubsub.js';
21
+ import { InvalidCommandHandlerException } from './index.js';
22
+ import { AsyncContext } from './scopes/async.context.js';
23
+ import { ObservableBus } from './utils/observable-bus.js';
24
+ /**
25
+ * @publicApi
26
+ */
27
+ let CommandBus = CommandBus_1 = class CommandBus extends ObservableBus {
28
28
  constructor(moduleRef, options) {
29
29
  super();
30
30
  this.moduleRef = moduleRef;
31
31
  this.options = options;
32
- this.logger = new common_1.Logger(CommandBus_1.name);
32
+ this.logger = new Logger(CommandBus_1.name);
33
33
  this.handlers = new Map();
34
34
  if (this.options?.commandPublisher) {
35
35
  this._publisher = this.options.commandPublisher;
@@ -64,7 +64,7 @@ let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.Observ
64
64
  const executeFn = this.handlers.get(commandId);
65
65
  if (!executeFn) {
66
66
  const commandName = this.getCommandName(command);
67
- throw new command_not_found_exception_1.CommandHandlerNotFoundException(commandName);
67
+ throw new CommandHandlerNotFoundException(commandName);
68
68
  }
69
69
  this._publisher.publish(command);
70
70
  return executeFn(command, context);
@@ -73,14 +73,14 @@ let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.Observ
73
73
  if (handler.isDependencyTreeStatic()) {
74
74
  const instance = handler.instance;
75
75
  if (!instance.execute) {
76
- throw new index_1.InvalidCommandHandlerException();
76
+ throw new InvalidCommandHandlerException();
77
77
  }
78
78
  this.handlers.set(id, (command) => instance.execute(command));
79
79
  return;
80
80
  }
81
81
  this.handlers.set(id, async (command, context) => {
82
- context ??= async_context_1.AsyncContext.of(command) ?? new async_context_1.AsyncContext();
83
- if (!async_context_1.AsyncContext.isAttached(context)) {
82
+ context ??= AsyncContext.of(command) ?? new AsyncContext();
83
+ if (!AsyncContext.isAttached(context)) {
84
84
  // Commands returned by sagas may already have an async context set
85
85
  // and a corresponding request provider registered.
86
86
  this.moduleRef.registerRequestByContextId(context, context.id);
@@ -96,10 +96,10 @@ let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.Observ
96
96
  handlers.forEach((handler) => this.registerHandler(handler));
97
97
  }
98
98
  registerHandler(handler) {
99
- const typeRef = handler.metatype;
99
+ const typeRef = (handler.inject ? handler.instance?.constructor : handler.metatype);
100
100
  const target = this.reflectCommandId(typeRef);
101
101
  if (!target) {
102
- throw new index_1.InvalidCommandHandlerException();
102
+ throw new InvalidCommandHandlerException();
103
103
  }
104
104
  if (this.handlers.has(target)) {
105
105
  this.logger.warn(`Command handler [${typeRef.name}] is already registered. Overriding previously registered handler.`);
@@ -108,9 +108,9 @@ let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.Observ
108
108
  }
109
109
  getCommandId(command) {
110
110
  const { constructor: commandType } = Object.getPrototypeOf(command);
111
- const commandMetadata = Reflect.getMetadata(constants_2.COMMAND_METADATA, commandType);
111
+ const commandMetadata = Reflect.getMetadata(COMMAND_METADATA, commandType);
112
112
  if (!commandMetadata) {
113
- throw new command_not_found_exception_1.CommandHandlerNotFoundException(commandType.name);
113
+ throw new CommandHandlerNotFoundException(commandType.name);
114
114
  }
115
115
  return commandMetadata.id;
116
116
  }
@@ -119,18 +119,18 @@ let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.Observ
119
119
  return constructor.name;
120
120
  }
121
121
  reflectCommandId(handler) {
122
- const command = Reflect.getMetadata(constants_2.COMMAND_HANDLER_METADATA, handler);
123
- const commandMetadata = Reflect.getMetadata(constants_2.COMMAND_METADATA, command);
122
+ const command = Reflect.getMetadata(COMMAND_HANDLER_METADATA, handler);
123
+ const commandMetadata = Reflect.getMetadata(COMMAND_METADATA, command);
124
124
  return commandMetadata.id;
125
125
  }
126
126
  useDefaultPublisher() {
127
- this._publisher = new default_command_pubsub_1.DefaultCommandPubSub(this.subject$);
127
+ this._publisher = new DefaultCommandPubSub(this.subject$);
128
128
  }
129
129
  };
130
- exports.CommandBus = CommandBus;
131
- exports.CommandBus = CommandBus = CommandBus_1 = __decorate([
132
- (0, common_1.Injectable)(),
133
- __param(1, (0, common_1.Optional)()),
134
- __param(1, (0, common_1.Inject)(constants_1.CQRS_MODULE_OPTIONS)),
135
- __metadata("design:paramtypes", [core_1.ModuleRef, Object])
130
+ CommandBus = CommandBus_1 = __decorate([
131
+ Injectable(),
132
+ __param(1, Optional()),
133
+ __param(1, Inject(CQRS_MODULE_OPTIONS)),
134
+ __metadata("design:paramtypes", [ModuleRef, Object])
136
135
  ], CommandBus);
136
+ export { CommandBus };
package/dist/constants.js CHANGED
@@ -1,4 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CQRS_MODULE_OPTIONS = void 0;
4
- exports.CQRS_MODULE_OPTIONS = 'CQRS_MODULE_OPTIONS';
1
+ export const CQRS_MODULE_OPTIONS = 'CQRS_MODULE_OPTIONS';
@@ -1,9 +1,12 @@
1
1
  import { DynamicModule, OnApplicationBootstrap } from '@nestjs/common';
2
- import { CommandBus } from './command-bus';
3
- import { EventBus } from './event-bus';
4
- import { CqrsModuleAsyncOptions, CqrsModuleOptions, IEvent } from './interfaces';
5
- import { QueryBus } from './query-bus';
6
- import { ExplorerService } from './services/explorer.service';
2
+ import { CommandBus } from './command-bus.js';
3
+ import { EventBus } from './event-bus.js';
4
+ import { CqrsModuleAsyncOptions, CqrsModuleOptions, IEvent } from './interfaces/index.js';
5
+ import { QueryBus } from './query-bus.js';
6
+ import { ExplorerService } from './services/explorer.service.js';
7
+ /**
8
+ * @publicApi
9
+ */
7
10
  export declare class CqrsModule<EventBase extends IEvent = IEvent> implements OnApplicationBootstrap {
8
11
  private readonly explorerService;
9
12
  private readonly eventBus;
@@ -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);
@@ -9,24 +8,25 @@ 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
10
  var CqrsModule_1;
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.CqrsModule = void 0;
14
- const common_1 = require("@nestjs/common");
15
- const command_bus_1 = require("./command-bus");
16
- const constants_1 = require("./constants");
17
- const event_bus_1 = require("./event-bus");
18
- const event_publisher_1 = require("./event-publisher");
19
- const query_bus_1 = require("./query-bus");
20
- const explorer_service_1 = require("./services/explorer.service");
21
- const unhandled_exception_bus_1 = require("./unhandled-exception-bus");
22
- const aggregate_root_storage_1 = require("./storages/aggregate-root.storage");
11
+ import { Module, } from '@nestjs/common';
12
+ import { CommandBus } from './command-bus.js';
13
+ import { CQRS_MODULE_OPTIONS } from './constants.js';
14
+ import { EventBus } from './event-bus.js';
15
+ import { EventPublisher } from './event-publisher.js';
16
+ import { QueryBus } from './query-bus.js';
17
+ import { ExplorerService } from './services/explorer.service.js';
18
+ import { UnhandledExceptionBus } from './unhandled-exception-bus.js';
19
+ import { AggregateRootStorage } from './storages/aggregate-root.storage.js';
20
+ /**
21
+ * @publicApi
22
+ */
23
23
  let CqrsModule = CqrsModule_1 = class CqrsModule {
24
24
  static forRoot(options) {
25
25
  return {
26
26
  module: CqrsModule_1,
27
27
  providers: [
28
28
  {
29
- provide: constants_1.CQRS_MODULE_OPTIONS,
29
+ provide: CQRS_MODULE_OPTIONS,
30
30
  useValue: options ?? {},
31
31
  },
32
32
  ],
@@ -39,14 +39,14 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
39
39
  global: true,
40
40
  imports: options.imports || [],
41
41
  providers: [...this.createAsyncProviders(options)],
42
- exports: [constants_1.CQRS_MODULE_OPTIONS],
42
+ exports: [CQRS_MODULE_OPTIONS],
43
43
  };
44
44
  }
45
45
  static createAsyncProviders(options) {
46
46
  if (options.useValue) {
47
47
  return [
48
48
  {
49
- provide: constants_1.CQRS_MODULE_OPTIONS,
49
+ provide: CQRS_MODULE_OPTIONS,
50
50
  useValue: options.useValue,
51
51
  },
52
52
  ];
@@ -54,7 +54,7 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
54
54
  if (options.useFactory) {
55
55
  return [
56
56
  {
57
- provide: constants_1.CQRS_MODULE_OPTIONS,
57
+ provide: CQRS_MODULE_OPTIONS,
58
58
  useFactory: options.useFactory,
59
59
  inject: options.inject || [],
60
60
  },
@@ -67,7 +67,7 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
67
67
  useClass: options.useClass,
68
68
  },
69
69
  {
70
- provide: constants_1.CQRS_MODULE_OPTIONS,
70
+ provide: CQRS_MODULE_OPTIONS,
71
71
  useFactory: async (optionsFactory) => optionsFactory.createCqrsOptions(),
72
72
  inject: [options.useClass],
73
73
  },
@@ -76,7 +76,7 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
76
76
  if (options.useExisting) {
77
77
  return [
78
78
  {
79
- provide: constants_1.CQRS_MODULE_OPTIONS,
79
+ provide: CQRS_MODULE_OPTIONS,
80
80
  useFactory: async (optionsFactory) => optionsFactory.createCqrsOptions(),
81
81
  inject: [options.useExisting],
82
82
  },
@@ -96,30 +96,30 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
96
96
  this.commandBus.register(commands);
97
97
  this.queryBus.register(queries);
98
98
  this.eventBus.registerSagas(sagas);
99
- aggregate_root_storage_1.AggregateRootStorage.mergeContext(this.eventBus);
99
+ AggregateRootStorage.mergeContext(this.eventBus);
100
100
  }
101
101
  };
102
- exports.CqrsModule = CqrsModule;
103
- exports.CqrsModule = CqrsModule = CqrsModule_1 = __decorate([
104
- (0, common_1.Module)({
102
+ CqrsModule = CqrsModule_1 = __decorate([
103
+ Module({
105
104
  providers: [
106
- command_bus_1.CommandBus,
107
- query_bus_1.QueryBus,
108
- event_bus_1.EventBus,
109
- unhandled_exception_bus_1.UnhandledExceptionBus,
110
- event_publisher_1.EventPublisher,
111
- explorer_service_1.ExplorerService,
105
+ CommandBus,
106
+ QueryBus,
107
+ EventBus,
108
+ UnhandledExceptionBus,
109
+ EventPublisher,
110
+ ExplorerService,
112
111
  ],
113
112
  exports: [
114
- command_bus_1.CommandBus,
115
- query_bus_1.QueryBus,
116
- event_bus_1.EventBus,
117
- unhandled_exception_bus_1.UnhandledExceptionBus,
118
- event_publisher_1.EventPublisher,
113
+ CommandBus,
114
+ QueryBus,
115
+ EventBus,
116
+ UnhandledExceptionBus,
117
+ EventPublisher,
119
118
  ],
120
119
  }),
121
- __metadata("design:paramtypes", [explorer_service_1.ExplorerService,
122
- event_bus_1.EventBus,
123
- command_bus_1.CommandBus,
124
- query_bus_1.QueryBus])
120
+ __metadata("design:paramtypes", [ExplorerService,
121
+ EventBus,
122
+ CommandBus,
123
+ QueryBus])
125
124
  ], CqrsModule);
125
+ export { CqrsModule };
@@ -1,6 +1,6 @@
1
1
  import { InjectableOptions } from '@nestjs/common';
2
2
  import 'reflect-metadata';
3
- import { ICommand } from '../index';
3
+ import { ICommand } from '../index.js';
4
4
  /**
5
5
  * Decorator that marks a class as a Nest command handler. A command handler
6
6
  * handles commands (actions) executed by your application code.
@@ -11,5 +11,7 @@ import { ICommand } from '../index';
11
11
  * @param options injectable options passed on to the "@Injectable" decorator.
12
12
  *
13
13
  * @see https://docs.nestjs.com/recipes/cqrs#commands
14
+ *
15
+ * @publicApi
14
16
  */
15
17
  export declare const CommandHandler: (command: ICommand | (new (...args: any[]) => ICommand), options?: InjectableOptions) => ClassDecorator;