@nathapp/nestjs-outbox 3.3.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 (109) hide show
  1. package/.eslintrc.js +8 -0
  2. package/.nax/features/nestjs-outbox/.nax-acceptance.test.ts +878 -0
  3. package/.turbo/turbo-build.log +4 -0
  4. package/CHANGELOG.md +15 -0
  5. package/README.md +50 -0
  6. package/dist/constant/index.d.ts +1 -0
  7. package/dist/constant/index.js +18 -0
  8. package/dist/constant/index.js.map +1 -0
  9. package/dist/constant/outbox.constants.d.ts +3 -0
  10. package/dist/constant/outbox.constants.js +7 -0
  11. package/dist/constant/outbox.constants.js.map +1 -0
  12. package/dist/exception/index.d.ts +3 -0
  13. package/dist/exception/index.js +20 -0
  14. package/dist/exception/index.js.map +1 -0
  15. package/dist/exception/outbox-exception-code.enum.d.ts +4 -0
  16. package/dist/exception/outbox-exception-code.enum.js +9 -0
  17. package/dist/exception/outbox-exception-code.enum.js.map +1 -0
  18. package/dist/exception/outbox-invalid-event.exception.d.ts +4 -0
  19. package/dist/exception/outbox-invalid-event.exception.js +13 -0
  20. package/dist/exception/outbox-invalid-event.exception.js.map +1 -0
  21. package/dist/exception/outbox-misconfigured.exception.d.ts +4 -0
  22. package/dist/exception/outbox-misconfigured.exception.js +13 -0
  23. package/dist/exception/outbox-misconfigured.exception.js.map +1 -0
  24. package/dist/index.d.ts +6 -0
  25. package/dist/index.js +23 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/interface/index.d.ts +6 -0
  28. package/dist/interface/index.js +6 -0
  29. package/dist/interface/index.js.map +1 -0
  30. package/dist/interface/outbox-module-options.interface.d.ts +16 -0
  31. package/dist/interface/outbox-module-options.interface.js +3 -0
  32. package/dist/interface/outbox-module-options.interface.js.map +1 -0
  33. package/dist/interface/outbox-publisher.interface.d.ts +4 -0
  34. package/dist/interface/outbox-publisher.interface.js +3 -0
  35. package/dist/interface/outbox-publisher.interface.js.map +1 -0
  36. package/dist/interface/outbox-record.interface.d.ts +21 -0
  37. package/dist/interface/outbox-record.interface.js +3 -0
  38. package/dist/interface/outbox-record.interface.js.map +1 -0
  39. package/dist/interface/outbox-status.enum.d.ts +6 -0
  40. package/dist/interface/outbox-status.enum.js +11 -0
  41. package/dist/interface/outbox-status.enum.js.map +1 -0
  42. package/dist/interface/outbox-store.interface.d.ts +8 -0
  43. package/dist/interface/outbox-store.interface.js +3 -0
  44. package/dist/interface/outbox-store.interface.js.map +1 -0
  45. package/dist/module/index.d.ts +1 -0
  46. package/dist/module/index.js +18 -0
  47. package/dist/module/index.js.map +1 -0
  48. package/dist/module/outbox.module.d.ts +10 -0
  49. package/dist/module/outbox.module.js +63 -0
  50. package/dist/module/outbox.module.js.map +1 -0
  51. package/dist/provider/in-memory-outbox-store.d.ts +12 -0
  52. package/dist/provider/in-memory-outbox-store.js +97 -0
  53. package/dist/provider/in-memory-outbox-store.js.map +1 -0
  54. package/dist/provider/index.d.ts +4 -0
  55. package/dist/provider/index.js +21 -0
  56. package/dist/provider/index.js.map +1 -0
  57. package/dist/provider/outbox-relay.d.ts +20 -0
  58. package/dist/provider/outbox-relay.js +129 -0
  59. package/dist/provider/outbox-relay.js.map +1 -0
  60. package/dist/provider/outbox.service.d.ts +12 -0
  61. package/dist/provider/outbox.service.js +68 -0
  62. package/dist/provider/outbox.service.js.map +1 -0
  63. package/dist/provider/resolve-outbox-options.d.ts +29 -0
  64. package/dist/provider/resolve-outbox-options.js +34 -0
  65. package/dist/provider/resolve-outbox-options.js.map +1 -0
  66. package/dist/util/compute-backoff.d.ts +1 -0
  67. package/dist/util/compute-backoff.js +8 -0
  68. package/dist/util/compute-backoff.js.map +1 -0
  69. package/dist/util/index.d.ts +1 -0
  70. package/dist/util/index.js +18 -0
  71. package/dist/util/index.js.map +1 -0
  72. package/lib/constant/index.ts +1 -0
  73. package/lib/constant/outbox.constants.ts +3 -0
  74. package/lib/exception/index.ts +3 -0
  75. package/lib/exception/outbox-exception-code.enum.ts +4 -0
  76. package/lib/exception/outbox-invalid-event.exception.ts +9 -0
  77. package/lib/exception/outbox-misconfigured.exception.ts +9 -0
  78. package/lib/index.ts +6 -0
  79. package/lib/interface/index.ts +18 -0
  80. package/lib/interface/outbox-module-options.interface.ts +18 -0
  81. package/lib/interface/outbox-publisher.interface.ts +5 -0
  82. package/lib/interface/outbox-record.interface.ts +23 -0
  83. package/lib/interface/outbox-status.enum.ts +6 -0
  84. package/lib/interface/outbox-store.interface.ts +23 -0
  85. package/lib/module/index.ts +1 -0
  86. package/lib/module/outbox.module.ts +64 -0
  87. package/lib/provider/in-memory-outbox-store.ts +114 -0
  88. package/lib/provider/index.ts +4 -0
  89. package/lib/provider/outbox-relay.ts +147 -0
  90. package/lib/provider/outbox.service.ts +70 -0
  91. package/lib/provider/resolve-outbox-options.ts +51 -0
  92. package/lib/util/compute-backoff.ts +4 -0
  93. package/lib/util/index.ts +1 -0
  94. package/package.json +48 -0
  95. package/test/barrel/index.spec.ts +45 -0
  96. package/test/constant/outbox.constants.spec.ts +20 -0
  97. package/test/e2e/outbox.module.spec.ts +371 -0
  98. package/test/exception/outbox.exceptions.spec.ts +38 -0
  99. package/test/interface/outbox-status.enum.spec.ts +19 -0
  100. package/test/jest-nax-setup.js +31 -0
  101. package/test/jest-nax.json +30 -0
  102. package/test/jest-test.json +28 -0
  103. package/test/module/outbox.module.spec.ts +139 -0
  104. package/test/provider/in-memory-outbox-store.spec.ts +216 -0
  105. package/test/provider/outbox-relay.spec.ts +565 -0
  106. package/test/provider/outbox-service.spec.ts +229 -0
  107. package/test/provider/resolve-outbox-options.spec.ts +50 -0
  108. package/test/util/compute-backoff.spec.ts +25 -0
  109. package/tsconfig.json +9 -0
@@ -0,0 +1,114 @@
1
+ import { IOutboxStore } from '../interface/outbox-store.interface';
2
+ import { OutboxRecord } from '../interface/outbox-record.interface';
3
+ import { OutboxStatus } from '../interface/outbox-status.enum';
4
+
5
+ export class InMemoryOutboxStore implements IOutboxStore {
6
+ private readonly map = new Map<string, OutboxRecord>();
7
+
8
+ async save(record: OutboxRecord, _client: unknown): Promise<void> {
9
+ this.map.set(record.id, { ...record });
10
+ }
11
+
12
+ async claimBatch(
13
+ limit: number,
14
+ leaseMs: number,
15
+ now: Date,
16
+ owner: string,
17
+ ): Promise<OutboxRecord[]> {
18
+ const leaseUntil = new Date(now.getTime() + leaseMs);
19
+ const claimed: OutboxRecord[] = [];
20
+
21
+ for (const record of this.map.values()) {
22
+ if (claimed.length >= limit) {
23
+ break;
24
+ }
25
+ if (!this.isClaimable(record, now)) {
26
+ continue;
27
+ }
28
+ const updated: OutboxRecord = {
29
+ ...record,
30
+ status: OutboxStatus.PROCESSING,
31
+ owner,
32
+ leaseUntil,
33
+ };
34
+ this.map.set(record.id, updated);
35
+ claimed.push({ ...updated });
36
+ }
37
+
38
+ return claimed;
39
+ }
40
+
41
+ async markPublished(id: string, publishedAt: Date, owner: string): Promise<void> {
42
+ const existing = this.assertOwned(id, owner);
43
+ if (existing === undefined) {
44
+ return;
45
+ }
46
+ this.map.set(id, {
47
+ ...existing,
48
+ status: OutboxStatus.PUBLISHED,
49
+ publishedAt,
50
+ leaseUntil: undefined,
51
+ owner: undefined,
52
+ });
53
+ }
54
+
55
+ async markRetry(
56
+ id: string,
57
+ attempts: number,
58
+ nextAttemptAt: Date,
59
+ owner: string,
60
+ ): Promise<void> {
61
+ const existing = this.assertOwned(id, owner);
62
+ if (existing === undefined) {
63
+ return;
64
+ }
65
+ this.map.set(id, {
66
+ ...existing,
67
+ status: OutboxStatus.PENDING,
68
+ attempts,
69
+ nextAttemptAt,
70
+ leaseUntil: undefined,
71
+ owner: undefined,
72
+ });
73
+ }
74
+
75
+ async markDead(id: string, attempts: number, owner: string): Promise<void> {
76
+ const existing = this.assertOwned(id, owner);
77
+ if (existing === undefined) {
78
+ return;
79
+ }
80
+ this.map.set(id, {
81
+ ...existing,
82
+ status: OutboxStatus.DEAD,
83
+ attempts,
84
+ leaseUntil: undefined,
85
+ owner: undefined,
86
+ });
87
+ }
88
+
89
+ private assertOwned(id: string, owner: string): OutboxRecord | undefined {
90
+ const existing = this.map.get(id);
91
+ if (existing === undefined) {
92
+ return undefined;
93
+ }
94
+ if (existing.owner !== owner) {
95
+ throw new Error(
96
+ `outbox record "${id}" is no longer held by lease owner "${owner}" (lease reclaimed)`,
97
+ );
98
+ }
99
+ return existing;
100
+ }
101
+
102
+ private isClaimable(record: OutboxRecord, now: Date): boolean {
103
+ if (record.status === OutboxStatus.PUBLISHED || record.status === OutboxStatus.DEAD) {
104
+ return false;
105
+ }
106
+ if (record.status === OutboxStatus.PENDING) {
107
+ return record.nextAttemptAt.getTime() <= now.getTime();
108
+ }
109
+ if (record.status === OutboxStatus.PROCESSING) {
110
+ return record.leaseUntil !== undefined && record.leaseUntil.getTime() <= now.getTime();
111
+ }
112
+ return false;
113
+ }
114
+ }
@@ -0,0 +1,4 @@
1
+ export * from './in-memory-outbox-store';
2
+ export * from './outbox-relay';
3
+ export * from './outbox.service';
4
+ export * from './resolve-outbox-options';
@@ -0,0 +1,147 @@
1
+ import {
2
+ Inject,
3
+ Injectable,
4
+ Logger,
5
+ OnApplicationBootstrap,
6
+ OnModuleDestroy,
7
+ Optional,
8
+ } from '@nestjs/common';
9
+ import {
10
+ CLOCK,
11
+ IClock,
12
+ ID_GENERATOR,
13
+ IIdGenerator,
14
+ SystemClock,
15
+ UuidIdGenerator,
16
+ } from '@nathapp/nestjs-common';
17
+ import {
18
+ OUTBOX_MODULE_OPTIONS,
19
+ OUTBOX_PUBLISHER,
20
+ OUTBOX_STORE,
21
+ } from '../constant/outbox.constants';
22
+ import { OutboxMisconfiguredException } from '../exception/outbox-misconfigured.exception';
23
+ import { IOutboxPublisher } from '../interface/outbox-publisher.interface';
24
+ import { OutboxModuleOptions } from '../interface/outbox-module-options.interface';
25
+ import { IOutboxStore } from '../interface/outbox-store.interface';
26
+ import { resolveOutboxOptions, ResolvedOutboxRelayOptions } from './resolve-outbox-options';
27
+ import { computeBackoff } from '../util/compute-backoff';
28
+
29
+ @Injectable()
30
+ export class OutboxRelay implements OnApplicationBootstrap, OnModuleDestroy {
31
+ private readonly clock: IClock;
32
+ private readonly idGenerator: IIdGenerator;
33
+ private readonly store: IOutboxStore;
34
+ private readonly publisher: IOutboxPublisher | undefined;
35
+ private readonly options: ResolvedOutboxRelayOptions;
36
+ private inFlight: Promise<void> | null = null;
37
+ private interval: ReturnType<typeof setInterval> | null = null;
38
+ private readonly logger = new Logger(OutboxRelay.name);
39
+
40
+ constructor(
41
+ @Inject(OUTBOX_MODULE_OPTIONS) rawOptions: OutboxModuleOptions | undefined,
42
+ @Inject(OUTBOX_STORE) store: IOutboxStore,
43
+ @Optional() @Inject(OUTBOX_PUBLISHER) publisher?: IOutboxPublisher,
44
+ @Optional() @Inject(CLOCK) clock?: IClock,
45
+ @Optional() @Inject(ID_GENERATOR) idGenerator?: IIdGenerator,
46
+ ) {
47
+ this.store = store;
48
+ this.publisher = rawOptions?.publisher ?? publisher;
49
+ this.options = resolveOutboxOptions(rawOptions).relay;
50
+ this.clock = clock ?? new SystemClock();
51
+ this.idGenerator = idGenerator ?? new UuidIdGenerator();
52
+ }
53
+
54
+ async dispatchPendingBatch(): Promise<void> {
55
+ if (this.publisher === undefined) {
56
+ throw new OutboxMisconfiguredException();
57
+ }
58
+
59
+ if (this.inFlight !== null) {
60
+ return;
61
+ }
62
+
63
+ const publisher = this.publisher;
64
+ const cycle = this.runCycle(publisher);
65
+ this.inFlight = cycle;
66
+ try {
67
+ await cycle;
68
+ } finally {
69
+ this.inFlight = null;
70
+ }
71
+ }
72
+
73
+ private async runCycle(publisher: IOutboxPublisher): Promise<void> {
74
+ const now = this.clock.now();
75
+ const owner = this.idGenerator.generate();
76
+ const batch = await this.store.claimBatch(
77
+ this.options.batchSize,
78
+ this.options.leaseMs,
79
+ now,
80
+ owner,
81
+ );
82
+
83
+ if (batch.length === 0) {
84
+ return;
85
+ }
86
+
87
+ for (const record of batch) {
88
+ try {
89
+ await publisher.publish(record);
90
+ try {
91
+ await this.store.markPublished(record.id, this.clock.now(), owner);
92
+ } catch {
93
+ continue;
94
+ }
95
+ } catch {
96
+ const attempts = record.attempts + 1;
97
+ try {
98
+ if (attempts >= this.options.maxAttempts) {
99
+ await this.store.markDead(record.id, attempts, owner);
100
+ } else {
101
+ const failureNow = this.clock.now();
102
+ const backoffMs = computeBackoff(
103
+ attempts,
104
+ this.options.backoffBaseMs,
105
+ this.options.backoffCapMs,
106
+ );
107
+ const nextAttemptAt = new Date(failureNow.getTime() + backoffMs);
108
+ await this.store.markRetry(record.id, attempts, nextAttemptAt, owner);
109
+ }
110
+ } catch {
111
+ continue;
112
+ }
113
+ }
114
+ }
115
+ }
116
+
117
+ async onApplicationBootstrap(): Promise<void> {
118
+ if (!this.options.enabled) {
119
+ return;
120
+ }
121
+ if (this.publisher === undefined) {
122
+ throw new OutboxMisconfiguredException();
123
+ }
124
+ this.interval = setInterval(() => {
125
+ this.dispatchPendingBatch().catch((err: unknown) => {
126
+ this.logger.error(
127
+ `Outbox relay cycle failed: ${(err as Error)?.message ?? err}`,
128
+ (err as Error)?.stack,
129
+ );
130
+ });
131
+ }, this.options.pollIntervalMs);
132
+ }
133
+
134
+ async onModuleDestroy(): Promise<void> {
135
+ if (this.interval !== null) {
136
+ clearInterval(this.interval);
137
+ this.interval = null;
138
+ }
139
+ if (this.inFlight !== null) {
140
+ try {
141
+ await this.inFlight;
142
+ } catch {
143
+ // swallow — cycle errors are already logged by the interval tick
144
+ }
145
+ }
146
+ }
147
+ }
@@ -0,0 +1,70 @@
1
+ import { Inject, Injectable, Optional } from '@nestjs/common';
2
+ import {
3
+ CLOCK,
4
+ IClock,
5
+ ID_GENERATOR,
6
+ IIdGenerator,
7
+ SystemClock,
8
+ UuidIdGenerator,
9
+ } from '@nathapp/nestjs-common';
10
+ import {
11
+ ITransactionManager,
12
+ TRANSACTION_MANAGER,
13
+ } from '@nathapp/nestjs-data';
14
+ import { OUTBOX_STORE } from '../constant/outbox.constants';
15
+ import { OutboxInvalidEventException } from '../exception/outbox-invalid-event.exception';
16
+ import {
17
+ OutboxEventInput,
18
+ OutboxRecord,
19
+ } from '../interface/outbox-record.interface';
20
+ import { OutboxStatus } from '../interface/outbox-status.enum';
21
+ import { IOutboxStore } from '../interface/outbox-store.interface';
22
+
23
+ @Injectable()
24
+ export class OutboxService {
25
+ private readonly clock: IClock;
26
+ private readonly idGenerator: IIdGenerator;
27
+
28
+ constructor(
29
+ @Inject(OUTBOX_STORE)
30
+ private readonly store: IOutboxStore,
31
+ @Optional() @Inject(TRANSACTION_MANAGER)
32
+ private readonly txManager?: ITransactionManager,
33
+ @Optional() @Inject(CLOCK) clock?: IClock,
34
+ @Optional() @Inject(ID_GENERATOR) idGenerator?: IIdGenerator,
35
+ ) {
36
+ this.clock = clock ?? new SystemClock();
37
+ this.idGenerator = idGenerator ?? new UuidIdGenerator();
38
+ }
39
+
40
+ async record(event: OutboxEventInput): Promise<OutboxRecord> {
41
+ if (event === null || typeof event !== 'object') {
42
+ throw new OutboxInvalidEventException();
43
+ }
44
+
45
+ const rawType = (event as { type?: unknown }).type;
46
+ if (typeof rawType !== 'string' || rawType.trim().length === 0) {
47
+ throw new OutboxInvalidEventException();
48
+ }
49
+
50
+ const type = rawType.trim();
51
+
52
+ const now = this.clock.now();
53
+ const record: OutboxRecord = {
54
+ id: this.idGenerator.generate(),
55
+ type,
56
+ payload: (event as OutboxEventInput).payload,
57
+ status: OutboxStatus.PENDING,
58
+ attempts: 0,
59
+ createdAt: now,
60
+ nextAttemptAt: now,
61
+ headers: event.headers,
62
+ metadata: event.metadata,
63
+ };
64
+
65
+ const client = this.txManager?.getClient();
66
+ await this.store.save(record, client);
67
+
68
+ return record;
69
+ }
70
+ }
@@ -0,0 +1,51 @@
1
+ import { OutboxModuleOptions } from '../interface/outbox-module-options.interface';
2
+ import { IOutboxStore } from '../interface/outbox-store.interface';
3
+ import { IOutboxPublisher } from '../interface/outbox-publisher.interface';
4
+ import { InMemoryOutboxStore } from './in-memory-outbox-store';
5
+
6
+ export interface ResolvedOutboxRelayOptions {
7
+ enabled: boolean;
8
+ pollIntervalMs: number;
9
+ batchSize: number;
10
+ leaseMs: number;
11
+ maxAttempts: number;
12
+ backoffBaseMs: number;
13
+ backoffCapMs: number;
14
+ }
15
+
16
+ export interface ResolvedOutboxOptions {
17
+ store: IOutboxStore;
18
+ publisher: IOutboxPublisher | undefined;
19
+ relay: ResolvedOutboxRelayOptions;
20
+ }
21
+
22
+ export const OUTBOX_DEFAULTS = {
23
+ relay: {
24
+ enabled: true,
25
+ pollIntervalMs: 5000,
26
+ batchSize: 10,
27
+ leaseMs: 30000,
28
+ maxAttempts: 10,
29
+ backoffBaseMs: 1000,
30
+ backoffCapMs: 300000,
31
+ },
32
+ } as const;
33
+
34
+ export function resolveOutboxOptions(
35
+ options: OutboxModuleOptions | undefined,
36
+ ): ResolvedOutboxOptions {
37
+ const relay = options?.relay ?? {};
38
+ return {
39
+ store: options?.store ?? new InMemoryOutboxStore(),
40
+ publisher: options?.publisher,
41
+ relay: {
42
+ enabled: relay.enabled ?? OUTBOX_DEFAULTS.relay.enabled,
43
+ pollIntervalMs: relay.pollIntervalMs ?? OUTBOX_DEFAULTS.relay.pollIntervalMs,
44
+ batchSize: relay.batchSize ?? OUTBOX_DEFAULTS.relay.batchSize,
45
+ leaseMs: relay.leaseMs ?? OUTBOX_DEFAULTS.relay.leaseMs,
46
+ maxAttempts: relay.maxAttempts ?? OUTBOX_DEFAULTS.relay.maxAttempts,
47
+ backoffBaseMs: relay.backoffBaseMs ?? OUTBOX_DEFAULTS.relay.backoffBaseMs,
48
+ backoffCapMs: relay.backoffCapMs ?? OUTBOX_DEFAULTS.relay.backoffCapMs,
49
+ },
50
+ };
51
+ }
@@ -0,0 +1,4 @@
1
+ export function computeBackoff(attempts: number, baseMs: number, capMs: number): number {
2
+ const raw = baseMs * 2 ** (attempts - 1);
3
+ return Math.min(capMs, raw);
4
+ }
@@ -0,0 +1 @@
1
+ export * from './compute-backoff';
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@nathapp/nestjs-outbox",
3
+ "version": "3.3.0",
4
+ "description": "Transactional outbox for NestJS: record domain events inside the caller's DB transaction, then relay them at-least-once to a broker. Zero runtime dependencies",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "clean": "rimraf dist/ .turbo/",
9
+ "build": "rimraf dist && tsc -p tsconfig.json",
10
+ "precommit": "lint-staged",
11
+ "test": "jest --config ./test/jest-test.json",
12
+ "test:cov": "jest --config ./test/jest-test.json --coverage",
13
+ "test:watch": "jest --config ./test/jest-test.json --watch",
14
+ "type-check": "tsc --noEmit -p tsconfig.json",
15
+ "lint": "eslint -c .eslintrc.js",
16
+ "lint:fix": "eslint -c .eslintrc.js --fix"
17
+ },
18
+ "author": "William Khoo",
19
+ "license": "ISC",
20
+ "engines": {
21
+ "node": ">=16"
22
+ },
23
+ "peerDependencies": {
24
+ "@nathapp/nestjs-common": "^3.0.0",
25
+ "@nathapp/nestjs-data": "^3.0.0",
26
+ "@nestjs/common": "^10.0.0 || ^11.0.0",
27
+ "@nestjs/core": "^10.0.0 || ^11.0.0",
28
+ "reflect-metadata": "^0.1.13 || ^0.2.0"
29
+ },
30
+ "devDependencies": {
31
+ "@nathapp/eslint-config": "1.0.1",
32
+ "@nathapp/nestjs-common": "^3.3.0",
33
+ "@nathapp/nestjs-data": "^3.3.0",
34
+ "@nathapp/typescript-config": "1.0.1",
35
+ "@nestjs/common": "^11.0.0",
36
+ "@nestjs/core": "^11.0.0",
37
+ "@nestjs/testing": "^11.0.0",
38
+ "@types/jest": "^29.5.2",
39
+ "eslint": "^8.57.0",
40
+ "jest": "^29.5.0",
41
+ "reflect-metadata": "^0.1.13",
42
+ "ts-jest": "^29.1.0",
43
+ "typescript": "^5"
44
+ },
45
+ "directories": {
46
+ "lib": "lib"
47
+ }
48
+ }
@@ -0,0 +1,45 @@
1
+ import {
2
+ OutboxModule,
3
+ OutboxService,
4
+ OutboxRelay,
5
+ InMemoryOutboxStore,
6
+ OutboxInvalidEventException,
7
+ OutboxMisconfiguredException,
8
+ OUTBOX_STORE,
9
+ } from '../../lib';
10
+
11
+ describe('Package barrel — lib/index public surface', () => {
12
+ describe('AC5: every public symbol exported from the package barrel has the expected kind', () => {
13
+ it('AC5: OutboxModule is a constructor (function)', () => {
14
+ expect(typeof OutboxModule).toBe('function');
15
+ });
16
+
17
+ it('AC5: OutboxService is a constructor (function)', () => {
18
+ expect(typeof OutboxService).toBe('function');
19
+ });
20
+
21
+ it('AC5: OutboxRelay is a constructor (function)', () => {
22
+ expect(typeof OutboxRelay).toBe('function');
23
+ });
24
+
25
+ it('AC5: new InMemoryOutboxStore() is an InMemoryOutboxStore', () => {
26
+ const store = new InMemoryOutboxStore();
27
+ expect(store).toBeInstanceOf(InMemoryOutboxStore);
28
+ });
29
+
30
+ it('AC5: new OutboxInvalidEventException() is an OutboxInvalidEventException', () => {
31
+ const err = new OutboxInvalidEventException();
32
+ expect(err).toBeInstanceOf(OutboxInvalidEventException);
33
+ });
34
+
35
+ it('AC5: new OutboxMisconfiguredException() is an OutboxMisconfiguredException', () => {
36
+ const err = new OutboxMisconfiguredException();
37
+ expect(err).toBeInstanceOf(OutboxMisconfiguredException);
38
+ });
39
+
40
+ it('AC5: OUTBOX_STORE is a symbol', () => {
41
+ expect(typeof OUTBOX_STORE).toBe('symbol');
42
+ expect(OUTBOX_STORE).toBe(Symbol.for('OUTBOX_STORE'));
43
+ });
44
+ });
45
+ });
@@ -0,0 +1,20 @@
1
+ import {
2
+ OUTBOX_STORE,
3
+ OUTBOX_PUBLISHER,
4
+ OUTBOX_MODULE_OPTIONS,
5
+ } from '../../lib/constant/outbox.constants';
6
+
7
+ describe('Outbox DI tokens (AC #10)', () => {
8
+ it('OUTBOX_STORE equals Symbol.for("OUTBOX_STORE")', () => {
9
+ expect(OUTBOX_STORE).toBe(Symbol.for('OUTBOX_STORE'));
10
+ });
11
+
12
+ it('OUTBOX_PUBLISHER equals Symbol.for("OUTBOX_PUBLISHER")', () => {
13
+ expect(OUTBOX_PUBLISHER).toBe(Symbol.for('OUTBOX_PUBLISHER'));
14
+ });
15
+
16
+ it('OUTBOX_MODULE_OPTIONS is defined (string or symbol token)', () => {
17
+ expect(OUTBOX_MODULE_OPTIONS).toBeDefined();
18
+ expect(typeof OUTBOX_MODULE_OPTIONS === 'string' || typeof OUTBOX_MODULE_OPTIONS === 'symbol').toBe(true);
19
+ });
20
+ });