@arts-n-crafts/ts 3.19.0 → 5.0.0-rc.2

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 (79) hide show
  1. package/README.md +221 -6
  2. package/package.json +109 -22
  3. package/packages/v3/dist/index.cjs +63 -72
  4. package/packages/v3/dist/index.cjs.map +1 -1
  5. package/packages/v3/dist/index.d.cts +22 -22
  6. package/packages/v3/dist/index.d.ts +22 -22
  7. package/packages/v3/dist/index.js +63 -72
  8. package/packages/v3/dist/index.js.map +1 -1
  9. package/packages/v3/package.json +49 -50
  10. package/packages/v4/dist/index.cjs +71 -126
  11. package/packages/v4/dist/index.cjs.map +1 -1
  12. package/packages/v4/dist/index.d.cts +36 -47
  13. package/packages/v4/dist/index.d.ts +36 -47
  14. package/packages/v4/dist/index.js +72 -125
  15. package/packages/v4/dist/index.js.map +1 -1
  16. package/packages/v4/package.json +49 -50
  17. package/packages/v5/dist/DomainEvent-CSzUMw46.d.cts +23 -0
  18. package/packages/v5/dist/DomainEvent-w1jyCjai.d.ts +23 -0
  19. package/packages/v5/dist/Intent-B6l0bHZU.d.cts +13 -0
  20. package/packages/v5/dist/Intent-BLNY6RoG.d.ts +13 -0
  21. package/packages/v5/dist/Message-c9XdBoh4.d.cts +36 -0
  22. package/packages/v5/dist/Message-c9XdBoh4.d.ts +36 -0
  23. package/packages/v5/dist/Rejection-D9PlzquK.d.cts +19 -0
  24. package/packages/v5/dist/Rejection-D9PlzquK.d.ts +19 -0
  25. package/packages/v5/dist/adapters/outbound/capabilities/index.cjs +4 -0
  26. package/packages/v5/dist/adapters/outbound/capabilities/index.cjs.map +1 -0
  27. package/packages/v5/dist/adapters/outbound/capabilities/index.d.cts +77 -0
  28. package/packages/v5/dist/adapters/outbound/capabilities/index.d.ts +77 -0
  29. package/packages/v5/dist/adapters/outbound/capabilities/index.js +3 -0
  30. package/packages/v5/dist/adapters/outbound/capabilities/index.js.map +1 -0
  31. package/packages/v5/dist/adapters/outbound/shapes/index.cjs +4 -0
  32. package/packages/v5/dist/adapters/outbound/shapes/index.cjs.map +1 -0
  33. package/packages/v5/dist/adapters/outbound/shapes/index.d.cts +68 -0
  34. package/packages/v5/dist/adapters/outbound/shapes/index.d.ts +68 -0
  35. package/packages/v5/dist/adapters/outbound/shapes/index.js +3 -0
  36. package/packages/v5/dist/adapters/outbound/shapes/index.js.map +1 -0
  37. package/packages/v5/dist/core/capabilities/index.cjs +4 -0
  38. package/packages/v5/dist/core/capabilities/index.cjs.map +1 -0
  39. package/packages/v5/dist/core/capabilities/index.d.cts +33 -0
  40. package/packages/v5/dist/core/capabilities/index.d.ts +33 -0
  41. package/packages/v5/dist/core/capabilities/index.js +3 -0
  42. package/packages/v5/dist/core/capabilities/index.js.map +1 -0
  43. package/packages/v5/dist/core/shapes/index.cjs +4 -0
  44. package/packages/v5/dist/core/shapes/index.cjs.map +1 -0
  45. package/packages/v5/dist/core/shapes/index.d.cts +27 -0
  46. package/packages/v5/dist/core/shapes/index.d.ts +27 -0
  47. package/packages/v5/dist/core/shapes/index.js +3 -0
  48. package/packages/v5/dist/core/shapes/index.js.map +1 -0
  49. package/packages/v5/dist/useCases/command/capabilities/index.cjs +4 -0
  50. package/packages/v5/dist/useCases/command/capabilities/index.cjs.map +1 -0
  51. package/packages/v5/dist/useCases/command/capabilities/index.d.cts +27 -0
  52. package/packages/v5/dist/useCases/command/capabilities/index.d.ts +27 -0
  53. package/packages/v5/dist/useCases/command/capabilities/index.js +3 -0
  54. package/packages/v5/dist/useCases/command/capabilities/index.js.map +1 -0
  55. package/packages/v5/dist/useCases/command/shapes/index.cjs +4 -0
  56. package/packages/v5/dist/useCases/command/shapes/index.cjs.map +1 -0
  57. package/packages/v5/dist/useCases/command/shapes/index.d.cts +78 -0
  58. package/packages/v5/dist/useCases/command/shapes/index.d.ts +78 -0
  59. package/packages/v5/dist/useCases/command/shapes/index.js +3 -0
  60. package/packages/v5/dist/useCases/command/shapes/index.js.map +1 -0
  61. package/packages/v5/dist/useCases/policy/capabilities/index.cjs +4 -0
  62. package/packages/v5/dist/useCases/policy/capabilities/index.cjs.map +1 -0
  63. package/packages/v5/dist/useCases/policy/capabilities/index.d.cts +12 -0
  64. package/packages/v5/dist/useCases/policy/capabilities/index.d.ts +12 -0
  65. package/packages/v5/dist/useCases/policy/capabilities/index.js +3 -0
  66. package/packages/v5/dist/useCases/policy/capabilities/index.js.map +1 -0
  67. package/packages/v5/dist/useCases/query/capabilities/index.cjs +4 -0
  68. package/packages/v5/dist/useCases/query/capabilities/index.cjs.map +1 -0
  69. package/packages/v5/dist/useCases/query/capabilities/index.d.cts +8 -0
  70. package/packages/v5/dist/useCases/query/capabilities/index.d.ts +8 -0
  71. package/packages/v5/dist/useCases/query/capabilities/index.js +3 -0
  72. package/packages/v5/dist/useCases/query/capabilities/index.js.map +1 -0
  73. package/packages/v5/dist/useCases/query/shapes/index.cjs +4 -0
  74. package/packages/v5/dist/useCases/query/shapes/index.cjs.map +1 -0
  75. package/packages/v5/dist/useCases/query/shapes/index.d.cts +7 -0
  76. package/packages/v5/dist/useCases/query/shapes/index.d.ts +7 -0
  77. package/packages/v5/dist/useCases/query/shapes/index.js +3 -0
  78. package/packages/v5/dist/useCases/query/shapes/index.js.map +1 -0
  79. package/packages/v5/package.json +59 -0
@@ -33,23 +33,17 @@ function createQuery(type, payload, metadata = {}) {
33
33
 
34
34
  // src/core/utils/isCommand.ts
35
35
  function isCommand(candidate) {
36
- if (candidate === null)
37
- return false;
38
- if (typeof candidate !== "object")
39
- return false;
40
- if (!("type" in candidate))
41
- return false;
36
+ if (candidate === null) return false;
37
+ if (typeof candidate !== "object") return false;
38
+ if (!("type" in candidate)) return false;
42
39
  return "kind" in candidate && candidate.kind === "command";
43
40
  }
44
41
 
45
42
  // src/core/utils/isQuery.ts
46
43
  function isQuery(candidate) {
47
- if (candidate === null)
48
- return false;
49
- if (typeof candidate !== "object")
50
- return false;
51
- if (!("type" in candidate))
52
- return false;
44
+ if (candidate === null) return false;
45
+ if (typeof candidate !== "object") return false;
46
+ if (!("type" in candidate)) return false;
53
47
  return "kind" in candidate && candidate.kind === "query";
54
48
  }
55
49
 
@@ -152,8 +146,7 @@ function fail(anExpression) {
152
146
 
153
147
  // src/utils/invariant/invariant.ts
154
148
  function invariant(condition, onInvalid) {
155
- if (!condition)
156
- onInvalid();
149
+ if (!condition) onInvalid();
157
150
  }
158
151
 
159
152
  // src/domain/Specification/implementations/FieldGreaterThan.specification.ts
@@ -170,9 +163,7 @@ var FieldGreaterThan = class extends CompositeSpecification {
170
163
  const field = entity[this.field];
171
164
  invariant(
172
165
  this.isNumber(field),
173
- fail(
174
- new TypeError(`Field ${String(this.field)} is not a number`)
175
- )
166
+ fail(new TypeError(`Field ${String(this.field)} is not a number`))
176
167
  );
177
168
  return field > this.value;
178
169
  }
@@ -180,12 +171,12 @@ var FieldGreaterThan = class extends CompositeSpecification {
180
171
  return createQueryNode("gt", this.field, this.value);
181
172
  }
182
173
  };
183
- function createIntegrationEvent(type, payload, metadata) {
174
+ function createIntegrationEvent(type, payload, metadata = {}, timestamp = (/* @__PURE__ */ new Date()).toISOString()) {
184
175
  return Object.freeze({
185
176
  id: crypto.randomUUID(),
186
177
  type,
187
178
  payload,
188
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
179
+ timestamp,
189
180
  metadata: {
190
181
  ...metadata
191
182
  },
@@ -195,11 +186,16 @@ function createIntegrationEvent(type, payload, metadata) {
195
186
 
196
187
  // src/domain/utils/convertDomainEventToIntegrationEvent.ts
197
188
  function convertDomainEventToIntegrationEvent(event) {
198
- return createIntegrationEvent(event.type, event.payload, {
199
- outcome: "accepted",
200
- aggregateType: event.aggregateType,
201
- aggregateId: event.aggregateId
202
- });
189
+ return createIntegrationEvent(
190
+ event.type,
191
+ event.payload,
192
+ {
193
+ outcome: "accepted",
194
+ aggregateType: event.aggregateType,
195
+ aggregateId: event.aggregateId
196
+ },
197
+ new Date(event.timestamp).toISOString()
198
+ );
203
199
  }
204
200
 
205
201
  // src/domain/utils/convertRejectionToIntegrationEvent.ts
@@ -213,7 +209,8 @@ function convertRejectionToIntegrationEvent(rejection) {
213
209
  aggregateId: rejection.metadata?.aggregateId,
214
210
  commandType: rejection.commandType,
215
211
  commandId: rejection.commandId
216
- }
212
+ },
213
+ new Date(rejection.timestamp).toISOString()
217
214
  );
218
215
  }
219
216
  function createDomainEvent(type, aggregateId, aggregateType, payload, metadata = {}) {
@@ -248,12 +245,9 @@ function createRejection(rejectionSpecifics, metadata = {}) {
248
245
 
249
246
  // src/domain/utils/isEvent.ts
250
247
  function isEvent(event) {
251
- if (typeof event !== "object")
252
- return false;
253
- if (event === null)
254
- return false;
255
- if (!("type" in event))
256
- return false;
248
+ if (typeof event !== "object") return false;
249
+ if (event === null) return false;
250
+ if (!("type" in event)) return false;
257
251
  return "kind" in event;
258
252
  }
259
253
 
@@ -264,8 +258,7 @@ function isDomainEvent(event) {
264
258
 
265
259
  // src/domain/utils/isRejection.ts
266
260
  function isRejection(candidate) {
267
- if (candidate === null || typeof candidate !== "object")
268
- return false;
261
+ if (candidate === null || typeof candidate !== "object") return false;
269
262
  return "commandId" in candidate && "commandType" in candidate && "reasonCode" in candidate && "timestamp" in candidate;
270
263
  }
271
264
 
@@ -318,43 +311,36 @@ var SimpleDatabase = class {
318
311
  datasource = /* @__PURE__ */ new Map();
319
312
  simulateOffline = false;
320
313
  async query(tableName, specification) {
321
- if (this.simulateOffline)
322
- throw new DatabaseOfflineException();
314
+ if (this.simulateOffline) throw new DatabaseOfflineException();
323
315
  const tableRecords = this.datasource.get(tableName) || [];
324
316
  return tableRecords.filter((record) => specification.isSatisfiedBy(record));
325
317
  }
326
318
  async execute(tableName, statement) {
327
- if (this.simulateOffline)
328
- throw new DatabaseOfflineException();
329
- if (!this.datasource.has(tableName))
330
- this.datasource.set(tableName, []);
319
+ if (this.simulateOffline) throw new DatabaseOfflineException();
320
+ if (!this.datasource.has(tableName)) this.datasource.set(tableName, []);
331
321
  const table = this.datasource.get(tableName);
332
322
  switch (statement.operation) {
333
323
  case "CREATE" /* CREATE */: {
334
324
  const isDuplicate = table.some((item) => item.id === statement.payload.id);
335
- if (isDuplicate)
336
- throw new DuplicateRecordException(statement.payload.id);
325
+ if (isDuplicate) throw new DuplicateRecordException(statement.payload.id);
337
326
  table.push(statement.payload);
338
327
  break;
339
328
  }
340
329
  case "PUT" /* PUT */: {
341
330
  const index = table.findIndex((item) => item.id === statement.payload.id);
342
- if (index === -1)
343
- throw new RecordNotFoundException(statement.payload.id);
331
+ if (index === -1) throw new RecordNotFoundException(statement.payload.id);
344
332
  table[index] = statement.payload;
345
333
  break;
346
334
  }
347
335
  case "PATCH" /* PATCH */: {
348
336
  const index = table.findIndex((item) => item.id === statement.payload.id);
349
- if (index === -1)
350
- throw new RecordNotFoundException(statement.payload.id);
337
+ if (index === -1) throw new RecordNotFoundException(statement.payload.id);
351
338
  table[index] = { ...table[index], ...statement.payload };
352
339
  break;
353
340
  }
354
341
  case "DELETE" /* DELETE */: {
355
342
  const index = table.findIndex((item) => item.id === statement.payload.id);
356
- if (index === -1)
357
- throw new RecordNotFoundException(statement.payload.id);
343
+ if (index === -1) throw new RecordNotFoundException(statement.payload.id);
358
344
  table.splice(index, 1);
359
345
  break;
360
346
  }
@@ -374,12 +360,13 @@ var SimpleEventBus = class {
374
360
  this.handlers.set(stream, handlersForType);
375
361
  }
376
362
  async consume(stream, anEvent) {
377
- if (!this.handlers.has(stream))
378
- return;
363
+ if (!this.handlers.has(stream)) return;
379
364
  const handlers = this.handlers.get(stream);
380
- await Promise.all(handlers.map(async (handler) => {
381
- return handler.handle(anEvent);
382
- }));
365
+ await Promise.all(
366
+ handlers.map(async (handler) => {
367
+ return handler.handle(anEvent);
368
+ })
369
+ );
383
370
  }
384
371
  async publish(stream, anEvent) {
385
372
  await this.consume(stream, anEvent);
@@ -408,49 +395,6 @@ function isIntegrationEvent(event) {
408
395
  return isEvent(event) && event.kind === "integration";
409
396
  }
410
397
 
411
- // src/infrastructure/EventBus/utils/mapDomainEventToIntegrationEvent.ts
412
- function mapDomainEventToIntegrationEvent(domainEvent) {
413
- const metadata = {
414
- ...domainEvent.metadata,
415
- aggregateType: domainEvent.aggregateType,
416
- aggregateId: domainEvent.aggregateId,
417
- outcome: "accepted"
418
- };
419
- return {
420
- id: domainEvent.id,
421
- type: domainEvent.type,
422
- payload: domainEvent.payload,
423
- timestamp: new Date(domainEvent.timestamp).toISOString(),
424
- metadata,
425
- kind: "integration"
426
- };
427
- }
428
-
429
- // src/infrastructure/EventBus/utils/mapRejectionToIntegrationEvent.ts
430
- function mapRejectionToIntegrationEvent(rejection) {
431
- const metadata = {
432
- ...rejection.metadata,
433
- outcome: "rejected",
434
- commandId: rejection.commandId,
435
- commandType: rejection.commandType
436
- };
437
- return {
438
- id: rejection.id,
439
- type: rejection.type,
440
- payload: {
441
- reasonCode: rejection.reasonCode,
442
- reason: rejection.reason,
443
- classification: rejection.classification,
444
- retryable: rejection.retryable,
445
- details: rejection.details,
446
- validationErrors: rejection.validationErrors
447
- },
448
- timestamp: new Date(rejection.timestamp).toISOString(),
449
- metadata,
450
- kind: "integration"
451
- };
452
- }
453
-
454
398
  // src/utils/streamKey/makeStreamKey.ts
455
399
  function makeStreamKey(streamName, aggregateId) {
456
400
  return `${streamName}#${aggregateId}`;
@@ -469,10 +413,8 @@ function isEqual(a, b) {
469
413
 
470
414
  // src/utils/parseAsError/parseAsError.ts
471
415
  function parseAsError(value) {
472
- if (value instanceof Error)
473
- return value;
474
- if (typeof value === "string")
475
- return new Error(value);
416
+ if (value instanceof Error) return value;
417
+ if (typeof value === "string") return new Error(value);
476
418
  try {
477
419
  const json = JSON.stringify(value);
478
420
  return new Error(json ?? String(value));
@@ -514,11 +456,12 @@ var SimpleEventStore = class {
514
456
  }
515
457
  async append(streamName, events) {
516
458
  const uniqueAggregateIds = new Set(events.map((event2) => event2.aggregateId));
517
- if (uniqueAggregateIds.size > 1)
518
- throw new MultipleAggregatesException();
459
+ if (uniqueAggregateIds.size > 1) throw new MultipleAggregatesException();
519
460
  const event = events[0];
520
461
  const currentStream = await this.load(streamName, event.aggregateId);
521
- const eventsToStore = events.map((event2) => createStoredEvent(streamName, currentStream.length + 1, event2));
462
+ const eventsToStore = events.map(
463
+ (event2) => createStoredEvent(streamName, currentStream.length + 1, event2)
464
+ );
522
465
  await Promise.all(
523
466
  eventsToStore.map(
524
467
  async (payload) => this.database.execute(this.tableName, { operation: "CREATE" /* CREATE */, payload })
@@ -537,14 +480,16 @@ var GenericOutboxWorker = class {
537
480
  }
538
481
  async runOnce() {
539
482
  const pending = await this.outbox.getPending();
540
- await Promise.all(pending.map(async (entry) => {
541
- try {
542
- await this.eventBus.publish(this.stream, entry.event);
543
- await this.outbox.markAsPublished(entry.id);
544
- } catch {
545
- await this.outbox.markAsFailed(entry.id);
546
- }
547
- }));
483
+ await Promise.all(
484
+ pending.map(async (entry) => {
485
+ try {
486
+ await this.eventBus.publish(this.stream, entry.event);
487
+ await this.outbox.markAsPublished(entry.id);
488
+ } catch {
489
+ await this.outbox.markAsFailed(entry.id);
490
+ }
491
+ })
492
+ );
548
493
  }
549
494
  async tick() {
550
495
  await this.runOnce();
@@ -623,12 +568,7 @@ var SimpleRepository = class {
623
568
  }
624
569
  async store(events) {
625
570
  await Promise.all(
626
- events.map(
627
- async (event) => this.eventStore.append(
628
- this.streamName,
629
- [event]
630
- )
631
- )
571
+ events.map(async (event) => this.eventStore.append(this.streamName, [event]))
632
572
  );
633
573
  }
634
574
  };
@@ -647,20 +587,20 @@ var ScenarioTest = class {
647
587
  }
648
588
  givenInput = [];
649
589
  whenInput;
650
- given(...events) {
590
+ Given(...events) {
651
591
  this.givenInput = events;
652
592
  return {
653
- when: this.when.bind(this),
654
- then: this.then.bind(this)
593
+ When: this.When.bind(this),
594
+ Then: this.Then.bind(this)
655
595
  };
656
596
  }
657
- when(action) {
597
+ When(action) {
658
598
  this.whenInput = action;
659
599
  return {
660
- then: this.then.bind(this)
600
+ Then: this.Then.bind(this)
661
601
  };
662
602
  }
663
- async then(thenInput) {
603
+ async Then(thenInput) {
664
604
  await Promise.all([
665
605
  this.repository.store(this.givenInput),
666
606
  ...this.givenInput.map(async (event) => this.eventBus.consume(this.streamName, event))
@@ -686,7 +626,11 @@ var ScenarioTest = class {
686
626
  }
687
627
  invariant(
688
628
  isDomainEvent(thenInput),
689
- fail(new TypeError('When "domain event" or "integration event" expects a domain event in the then-step'))
629
+ fail(
630
+ new TypeError(
631
+ 'When "domain event" or "integration event" expects a domain event in the then-step'
632
+ )
633
+ )
690
634
  );
691
635
  await this.handleEvent(this.whenInput, thenInput);
692
636
  }
@@ -697,7 +641,10 @@ var ScenarioTest = class {
697
641
  const foundRejection = pending.find(
698
642
  (entry) => entry.event.metadata.outcome === "rejected" && entry.event.metadata.commandType === outcome.commandType && entry.event.payload.reasonCode === outcome.reasonCode
699
643
  );
700
- invariant(!!foundRejection, fail(new Error("ScenarioTest: rejection was not found in outbox")));
644
+ invariant(
645
+ !!foundRejection,
646
+ fail(new Error("ScenarioTest: rejection was not found in outbox"))
647
+ );
701
648
  return;
702
649
  }
703
650
  const actualEvents = await this.eventStore.load(this.streamName, outcome.aggregateId);
@@ -788,8 +735,6 @@ exports.isIntegrationEvent = isIntegrationEvent;
788
735
  exports.isQuery = isQuery;
789
736
  exports.isRejection = isRejection;
790
737
  exports.makeStreamKey = makeStreamKey;
791
- exports.mapDomainEventToIntegrationEvent = mapDomainEventToIntegrationEvent;
792
- exports.mapRejectionToIntegrationEvent = mapRejectionToIntegrationEvent;
793
738
  exports.parseAsError = parseAsError;
794
739
  //# sourceMappingURL=index.cjs.map
795
740
  //# sourceMappingURL=index.cjs.map