@nestjs/cqrs 8.0.2 → 8.0.5

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.
@@ -17,7 +17,7 @@ const uuid_1 = require("uuid");
17
17
  const EventsHandler = (...events) => {
18
18
  return (target) => {
19
19
  events.forEach((event) => {
20
- if (!Reflect.hasMetadata(constants_1.EVENT_METADATA, event)) {
20
+ if (!Reflect.hasOwnMetadata(constants_1.EVENT_METADATA, event)) {
21
21
  Reflect.defineMetadata(constants_1.EVENT_METADATA, { id: (0, uuid_1.v4)() }, event);
22
22
  }
23
23
  });
@@ -8,9 +8,10 @@ export declare type EventHandlerType<EventBase extends IEvent = IEvent> = Type<I
8
8
  export declare class EventBus<EventBase extends IEvent = IEvent> extends ObservableBus<EventBase> implements IEventBus<EventBase>, OnModuleDestroy {
9
9
  private readonly commandBus;
10
10
  private readonly moduleRef;
11
- protected getEventId: (event: EventBase) => string;
11
+ protected getEventId: (event: EventBase) => string | null;
12
12
  protected readonly subscriptions: Subscription[];
13
13
  private _publisher;
14
+ private readonly _logger;
14
15
  constructor(commandBus: CommandBus, moduleRef: ModuleRef);
15
16
  get publisher(): IEventPublisher<EventBase>;
16
17
  set publisher(_publisher: IEventPublisher<EventBase>);
@@ -21,7 +22,7 @@ export declare class EventBus<EventBase extends IEvent = IEvent> extends Observa
21
22
  registerSagas(types?: Type<unknown>[]): void;
22
23
  register(handlers?: EventHandlerType<EventBase>[]): void;
23
24
  protected registerHandler(handler: EventHandlerType<EventBase>): void;
24
- protected ofEventId(name: string): Observable<EventBase>;
25
+ protected ofEventId(id: string): Observable<EventBase>;
25
26
  protected registerSaga(saga: ISaga<EventBase>): void;
26
27
  private reflectEvents;
27
28
  private useDefaultPublisher;
package/dist/event-bus.js CHANGED
@@ -8,6 +8,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
+ var EventBus_1;
11
12
  Object.defineProperty(exports, "__esModule", { value: true });
12
13
  exports.EventBus = void 0;
13
14
  const common_1 = require("@nestjs/common");
@@ -21,11 +22,12 @@ const exceptions_1 = require("./exceptions");
21
22
  const default_get_event_id_1 = require("./helpers/default-get-event-id");
22
23
  const default_pubsub_1 = require("./helpers/default-pubsub");
23
24
  const utils_1 = require("./utils");
24
- let EventBus = class EventBus extends utils_1.ObservableBus {
25
+ let EventBus = EventBus_1 = class EventBus extends utils_1.ObservableBus {
25
26
  constructor(commandBus, moduleRef) {
26
27
  super();
27
28
  this.commandBus = commandBus;
28
29
  this.moduleRef = moduleRef;
30
+ this._logger = new common_1.Logger(EventBus_1.name);
29
31
  this.subscriptions = [];
30
32
  this.getEventId = default_get_event_id_1.defaultGetEventId;
31
33
  this.useDefaultPublisher();
@@ -50,7 +52,14 @@ let EventBus = class EventBus extends utils_1.ObservableBus {
50
52
  }
51
53
  bind(handler, id) {
52
54
  const stream$ = id ? this.ofEventId(id) : this.subject$;
53
- const subscription = stream$.subscribe((event) => handler.handle(event));
55
+ const subscription = stream$
56
+ .pipe((0, operators_1.mergeMap)((event) => (0, rxjs_1.from)(Promise.resolve(handler.handle(event)))))
57
+ .subscribe({
58
+ error: (error) => {
59
+ this._logger.error(`"${handler.constructor.name}" has thrown an error.`, error);
60
+ throw error;
61
+ },
62
+ });
54
63
  this.subscriptions.push(subscription);
55
64
  }
56
65
  registerSagas(types = []) {
@@ -77,8 +86,8 @@ let EventBus = class EventBus extends utils_1.ObservableBus {
77
86
  const events = this.reflectEvents(handler);
78
87
  events.map((event) => this.bind(instance, (0, default_get_event_id_1.defaultReflectEventId)(event)));
79
88
  }
80
- ofEventId(name) {
81
- return this.subject$.pipe((0, operators_1.filter)((event) => this.getEventId(event) === name));
89
+ ofEventId(id) {
90
+ return this.subject$.pipe((0, operators_1.filter)((event) => this.getEventId(event) === id));
82
91
  }
83
92
  registerSaga(saga) {
84
93
  if (!(0, util_1.isFunction)(saga)) {
@@ -89,8 +98,13 @@ let EventBus = class EventBus extends utils_1.ObservableBus {
89
98
  throw new exceptions_1.InvalidSagaException();
90
99
  }
91
100
  const subscription = stream$
92
- .pipe((0, operators_1.filter)((e) => !!e))
93
- .subscribe((command) => this.commandBus.execute(command));
101
+ .pipe((0, operators_1.filter)((e) => !!e), (0, operators_1.mergeMap)((command) => (0, rxjs_1.from)(this.commandBus.execute(command))))
102
+ .subscribe({
103
+ error: (error) => {
104
+ this._logger.error(`Command handler which execution was triggered by Saga has thrown an error.`, error);
105
+ throw error;
106
+ },
107
+ });
94
108
  this.subscriptions.push(subscription);
95
109
  }
96
110
  reflectEvents(handler) {
@@ -100,7 +114,7 @@ let EventBus = class EventBus extends utils_1.ObservableBus {
100
114
  this._publisher = new default_pubsub_1.DefaultPubSub(this.subject$);
101
115
  }
102
116
  };
103
- EventBus = __decorate([
117
+ EventBus = EventBus_1 = __decorate([
104
118
  (0, common_1.Injectable)(),
105
119
  __metadata("design:paramtypes", [command_bus_1.CommandBus,
106
120
  core_1.ModuleRef])
@@ -1,4 +1,9 @@
1
1
  import { IEvent } from '../interfaces';
2
2
  import { Type } from '@nestjs/common';
3
+ /**
4
+ * Null if the published class is not connected to any handler
5
+ * @param event
6
+ * @returns
7
+ */
3
8
  export declare const defaultGetEventId: <EventBase extends IEvent = IEvent>(event: EventBase) => string;
4
9
  export declare const defaultReflectEventId: <EventBase extends Type<IEvent> = Type<IEvent>>(event: EventBase) => string;
@@ -2,9 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defaultReflectEventId = exports.defaultGetEventId = void 0;
4
4
  const constants_1 = require("../decorators/constants");
5
+ /**
6
+ * Null if the published class is not connected to any handler
7
+ * @param event
8
+ * @returns
9
+ */
5
10
  const defaultGetEventId = (event) => {
11
+ var _a, _b;
6
12
  const { constructor } = Object.getPrototypeOf(event);
7
- return Reflect.getMetadata(constants_1.EVENT_METADATA, constructor).id;
13
+ return (_b = (_a = Reflect.getMetadata(constants_1.EVENT_METADATA, constructor)) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : null;
8
14
  };
9
15
  exports.defaultGetEventId = defaultGetEventId;
10
16
  const defaultReflectEventId = (event) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/cqrs",
3
- "version": "8.0.2",
3
+ "version": "8.0.5",
4
4
  "description": "A lightweight CQRS module for Nest framework (node.js)",
5
5
  "license": "MIT",
6
6
  "url": "https://github.com/nestjs/cqrs#readme",
@@ -20,26 +20,26 @@
20
20
  "uuid": "8.3.2"
21
21
  },
22
22
  "devDependencies": {
23
- "@commitlint/cli": "16.2.1",
24
- "@commitlint/config-angular": "16.2.1",
25
- "@nestjs/common": "8.3.0",
26
- "@nestjs/core": "8.3.0",
27
- "@types/node": "16.11.24",
28
- "@types/jest": "27.4.0",
29
- "@typescript-eslint/eslint-plugin": "5.11.0",
30
- "@typescript-eslint/parser": "5.11.0",
31
- "eslint": "8.9.0",
32
- "eslint-config-prettier": "8.3.0",
33
- "eslint-plugin-import": "2.25.4",
34
- "husky": "7.0.4",
35
- "jest": "27.5.1",
36
- "lint-staged": "12.3.4",
37
- "prettier": "2.5.1",
23
+ "@commitlint/cli": "17.0.2",
24
+ "@commitlint/config-angular": "17.0.0",
25
+ "@nestjs/common": "8.4.6",
26
+ "@nestjs/core": "8.4.6",
27
+ "@types/node": "16.11.39",
28
+ "@types/jest": "28.1.1",
29
+ "@typescript-eslint/eslint-plugin": "5.28.0",
30
+ "@typescript-eslint/parser": "5.28.0",
31
+ "eslint": "8.17.0",
32
+ "eslint-config-prettier": "8.5.0",
33
+ "eslint-plugin-import": "2.26.0",
34
+ "husky": "8.0.1",
35
+ "jest": "28.1.1",
36
+ "lint-staged": "13.0.1",
37
+ "prettier": "2.6.2",
38
38
  "reflect-metadata": "0.1.13",
39
- "release-it": "14.12.4",
40
- "rxjs": "7.5.4",
41
- "ts-jest": "27.1.3",
42
- "typescript": "4.5.5"
39
+ "release-it": "15.0.0",
40
+ "rxjs": "7.5.5",
41
+ "ts-jest": "28.0.5",
42
+ "typescript": "4.7.3"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "@nestjs/common": "^8.0.0",