@ddd-ts-kit/kernel 0.82.0 → 0.83.1
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.
- package/README.fr.md +138 -67
- package/README.md +133 -64
- package/dist/application/ports/read-model-rows.d.ts +1 -1
- package/dist/application/ports/read-model-rows.d.ts.map +1 -1
- package/dist/domain/errors/concept-error.d.ts +2 -13
- package/dist/domain/errors/concept-error.d.ts.map +1 -1
- package/dist/domain/errors/concept-error.js +3 -24
- package/dist/domain/errors/concept-error.js.map +1 -1
- package/dist/domain/errors/concurrency-error.d.ts +3 -1
- package/dist/domain/errors/concurrency-error.d.ts.map +1 -1
- package/dist/domain/errors/concurrency-error.js +2 -2
- package/dist/domain/errors/concurrency-error.js.map +1 -1
- package/dist/domain/errors/domain-refusal.d.ts +36 -0
- package/dist/domain/errors/domain-refusal.d.ts.map +1 -0
- package/dist/domain/errors/domain-refusal.js +33 -0
- package/dist/domain/errors/domain-refusal.js.map +1 -0
- package/dist/domain/errors/factual-refusal.d.ts +23 -0
- package/dist/domain/errors/factual-refusal.d.ts.map +1 -0
- package/dist/domain/errors/factual-refusal.js +34 -0
- package/dist/domain/errors/factual-refusal.js.map +1 -0
- package/dist/domain/errors/index.d.ts +17 -0
- package/dist/domain/errors/index.d.ts.map +1 -0
- package/dist/domain/errors/index.js +17 -0
- package/dist/domain/errors/index.js.map +1 -0
- package/dist/domain/errors/is-refusal.d.ts +4 -3
- package/dist/domain/errors/is-refusal.d.ts.map +1 -1
- package/dist/domain/errors/is-refusal.js +10 -3
- package/dist/domain/errors/is-refusal.js.map +1 -1
- package/dist/domain/errors/transaction-conflict-error.d.ts +10 -0
- package/dist/domain/errors/transaction-conflict-error.d.ts.map +1 -0
- package/dist/domain/errors/transaction-conflict-error.js +13 -0
- package/dist/domain/errors/transaction-conflict-error.js.map +1 -0
- package/dist/index.d.ts +1 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -13
- package/dist/index.js.map +1 -1
- package/dist/infrastructure/domain-events/within-unit-of-work.d.ts +4 -0
- package/dist/infrastructure/domain-events/within-unit-of-work.d.ts.map +1 -1
- package/dist/infrastructure/domain-events/within-unit-of-work.js +7 -1
- package/dist/infrastructure/domain-events/within-unit-of-work.js.map +1 -1
- package/dist/infrastructure/event-sourcing/postgres/is-append-conflict.d.ts +2 -2
- package/dist/infrastructure/event-sourcing/postgres/is-append-conflict.d.ts.map +1 -1
- package/dist/infrastructure/event-sourcing/postgres/is-append-conflict.js +4 -12
- package/dist/infrastructure/event-sourcing/postgres/is-append-conflict.js.map +1 -1
- package/dist/infrastructure/event-sourcing/postgres/postgres-event-store.d.ts +3 -1
- package/dist/infrastructure/event-sourcing/postgres/postgres-event-store.d.ts.map +1 -1
- package/dist/infrastructure/event-sourcing/postgres/postgres-event-store.js +3 -1
- package/dist/infrastructure/event-sourcing/postgres/postgres-event-store.js.map +1 -1
- package/dist/infrastructure/files/self-signed-file-storage.d.ts +1 -0
- package/dist/infrastructure/files/self-signed-file-storage.d.ts.map +1 -1
- package/dist/infrastructure/files/self-signed-file-storage.js +5 -9
- package/dist/infrastructure/files/self-signed-file-storage.js.map +1 -1
- package/dist/infrastructure/in-memory/in-memory-read-model-table.js +1 -1
- package/dist/infrastructure/in-memory/in-memory-read-model-table.js.map +1 -1
- package/dist/infrastructure/persistence/has-driver-code.d.ts +7 -0
- package/dist/infrastructure/persistence/has-driver-code.d.ts.map +1 -0
- package/dist/infrastructure/persistence/has-driver-code.js +20 -0
- package/dist/infrastructure/persistence/has-driver-code.js.map +1 -0
- package/dist/infrastructure/persistence/is-transaction-conflict.d.ts +2 -0
- package/dist/infrastructure/persistence/is-transaction-conflict.d.ts.map +1 -0
- package/dist/infrastructure/persistence/is-transaction-conflict.js +12 -0
- package/dist/infrastructure/persistence/is-transaction-conflict.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ starts, so a business rule never changes with a library.
|
|
|
28
28
|
|
|
29
29
|
| Area | Exports |
|
|
30
30
|
|---|---|
|
|
31
|
-
| Domain | `Entity`, `AggregateRoot` (version, recorded events), `ValueObject`, `DomainEvent`, `DomainError` with a stable `code`, `ConceptError` (the errors of one concept, each built by a static factory: see "Domain errors"), `isRefusal`, `CodeOf`, `RefusalOf`, `Refusal`, `RefusalFacts`, the generic `InvariantViolation`, `NotFoundError`, `ConflictError`, `ConcurrencyError`, `PreconditionFailedError`, `Result` with `ok`, `err`, `unwrap` |
|
|
31
|
+
| Domain | `Entity`, `AggregateRoot` (version, recorded events), `ValueObject`, `DomainEvent`, `DomainError` with a stable `code`, `ConceptError` (the errors of one concept, each built by a static factory: see "Domain errors"), `DomainRefusal` (one class per refusal, built by static factories), `FactualRefusal`, `isRefusal`, `CodeOf`, `RefusalOf`, `Refusal`, `RefusalFacts`, the generic `InvariantViolation`, `NotFoundError`, `ConflictError`, `ConcurrencyError`, `PreconditionFailedError`, `Result` with `ok`, `err`, `unwrap` |
|
|
32
32
|
| Ports | `Clock`, `IdGenerator`, `UnitOfWork`, `Repository` (abstract classes: each one is its own injection token), `UseCase`, `DependencyKind` (the roles a binding declares, read by di, tracing and aspects: `repository`, `gateway`, `external`, `use-case`, `publisher`, `consumer`, `domain-event-handler`, `job`) |
|
|
33
33
|
| Buses | `Command`, `Query`, `CommandBus`, `QueryBus`, `BusMiddleware`, the in-process `InMemoryCommandBus` and `InMemoryQueryBus` (`register(type, handler, { context })` names the bounded context in `MessageInfo`), `unitOfWorkMiddleware` |
|
|
34
34
|
| Events | `IntegrationEvent`, `IntegrationEventPublisher`, `IntegrationEventSubscriber`, `EventBus` (both at once), `EventSubscribers` (the subscriber of each bounded context), `ProcessedEventStore`, `idempotent`, `InMemoryEventBus`, `DomainEventDispatcher`, `IntegrationEventCallback` and `DomainEventCallback` (the functions a subscriber and a dispatcher are given), `executionContextOfEvent`, `inEventScope`, `scopedPublisher`, `EventDelivery` (what a provider says of one delivery: attempt, last attempt, transport, source, consumer), `messagingSystemOf` |
|
|
@@ -57,20 +57,36 @@ file, and `ddd-ts-kit generate` writes them):
|
|
|
57
57
|
|
|
58
58
|
<!-- checked sample -->
|
|
59
59
|
```ts
|
|
60
|
-
import { AggregateRoot,
|
|
60
|
+
import { AggregateRoot, DomainRefusal, Entity, ValueObject, NamedDomainEvent } from '@ddd-ts-kit/kernel';
|
|
61
61
|
|
|
62
|
-
// The
|
|
63
|
-
export
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
// The codes of the context, in domain/errors/codes.ts, and its refusals, one class each (see "Domain errors").
|
|
63
|
+
export const OrderingCodes = {
|
|
64
|
+
InvalidQuantity: 'INVALID_QUANTITY',
|
|
65
|
+
OrderAlreadyPlaced: 'ORDER_ALREADY_PLACED',
|
|
66
|
+
OrderEmpty: 'ORDER_EMPTY',
|
|
67
|
+
} as const;
|
|
68
|
+
|
|
69
|
+
export class InvalidQuantity extends DomainRefusal {
|
|
70
|
+
readonly code = OrderingCodes.InvalidQuantity;
|
|
71
|
+
|
|
72
|
+
static forUnits(units: number) {
|
|
73
|
+
return InvalidQuantity.refusal(`${units} is not a positive whole quantity.`, { units });
|
|
66
74
|
}
|
|
75
|
+
}
|
|
67
76
|
|
|
68
|
-
|
|
69
|
-
|
|
77
|
+
export class OrderAlreadyPlaced extends DomainRefusal {
|
|
78
|
+
readonly code = OrderingCodes.OrderAlreadyPlaced;
|
|
79
|
+
|
|
80
|
+
static forOrder(orderId: string) {
|
|
81
|
+
return OrderAlreadyPlaced.refusal(`Order ${orderId} is already placed.`, { orderId });
|
|
70
82
|
}
|
|
83
|
+
}
|
|
71
84
|
|
|
72
|
-
|
|
73
|
-
|
|
85
|
+
export class OrderEmpty extends DomainRefusal {
|
|
86
|
+
readonly code = OrderingCodes.OrderEmpty;
|
|
87
|
+
|
|
88
|
+
static onPlacement(orderId: string) {
|
|
89
|
+
return OrderEmpty.refusal(`Order ${orderId} has no line to place.`, { orderId });
|
|
74
90
|
}
|
|
75
91
|
}
|
|
76
92
|
|
|
@@ -82,7 +98,7 @@ export class Quantity extends ValueObject<{ readonly units: number }> {
|
|
|
82
98
|
|
|
83
99
|
static create(units: number): Quantity {
|
|
84
100
|
if (!Number.isInteger(units) || units <= 0) {
|
|
85
|
-
throw
|
|
101
|
+
throw InvalidQuantity.forUnits(units);
|
|
86
102
|
}
|
|
87
103
|
|
|
88
104
|
return new Quantity({ units });
|
|
@@ -129,7 +145,7 @@ export class Order extends AggregateRoot {
|
|
|
129
145
|
|
|
130
146
|
add(productId: string, quantity: Quantity): void {
|
|
131
147
|
if (this.placed) {
|
|
132
|
-
throw
|
|
148
|
+
throw OrderAlreadyPlaced.forOrder(this.id);
|
|
133
149
|
}
|
|
134
150
|
|
|
135
151
|
this.lines.push(new OrderLine(productId, quantity));
|
|
@@ -137,11 +153,11 @@ export class Order extends AggregateRoot {
|
|
|
137
153
|
|
|
138
154
|
place(now: Date): void {
|
|
139
155
|
if (this.placed) {
|
|
140
|
-
throw
|
|
156
|
+
throw OrderAlreadyPlaced.forOrder(this.id);
|
|
141
157
|
}
|
|
142
158
|
|
|
143
159
|
if (this.lines.length === 0) {
|
|
144
|
-
throw
|
|
160
|
+
throw OrderEmpty.onPlacement(this.id);
|
|
145
161
|
}
|
|
146
162
|
|
|
147
163
|
this.placed = true;
|
|
@@ -188,45 +204,60 @@ refusal instead of throwing it (`"stack.valueObjects": "result"`). Why each bloc
|
|
|
188
204
|
## Domain errors
|
|
189
205
|
|
|
190
206
|
A business refusal is a `DomainError`: an answer the code planned for, known everywhere by its
|
|
191
|
-
`code`. A context has one
|
|
192
|
-
|
|
207
|
+
`code`. A context has one class per refusal, named after what is refused, which extends
|
|
208
|
+
`DomainRefusal`; its static factories are named after what the refusal is about, and the code
|
|
209
|
+
of every refusal of the context is written once, in the closed list `domain/errors/codes.ts`:
|
|
193
210
|
|
|
194
211
|
<!-- checked sample -->
|
|
195
212
|
```ts
|
|
196
|
-
import {
|
|
213
|
+
import { DomainRefusal } from '@ddd-ts-kit/kernel';
|
|
197
214
|
|
|
198
215
|
declare const calendar: { readonly id: string };
|
|
199
216
|
declare const slot: { readonly startsAt: Date };
|
|
200
217
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
218
|
+
// domain/errors/codes.ts
|
|
219
|
+
export const SchedulingCodes = {
|
|
220
|
+
SlotTaken: 'SLOT_TAKEN',
|
|
221
|
+
UnknownAppointment: 'APPOINTMENT_NOT_FOUND',
|
|
222
|
+
} as const;
|
|
223
|
+
|
|
224
|
+
export type SchedulingCode = (typeof SchedulingCodes)[keyof typeof SchedulingCodes];
|
|
225
|
+
|
|
226
|
+
// domain/errors/slot-taken.error.ts
|
|
227
|
+
export class SlotTaken extends DomainRefusal {
|
|
228
|
+
readonly code = SchedulingCodes.SlotTaken;
|
|
229
|
+
|
|
230
|
+
static forCalendar(calendarId: string, startsAt: Date) {
|
|
231
|
+
return SlotTaken.refusal(
|
|
205
232
|
`The slot starting at ${startsAt.toISOString()} on calendar ${calendarId} is already held.`,
|
|
206
233
|
{ calendarId, startsAt },
|
|
207
234
|
).exposing('startsAt');
|
|
208
235
|
}
|
|
236
|
+
}
|
|
209
237
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
238
|
+
// domain/errors/unknown-appointment.error.ts
|
|
239
|
+
export class UnknownAppointment extends DomainRefusal {
|
|
240
|
+
readonly code = SchedulingCodes.UnknownAppointment;
|
|
241
|
+
|
|
242
|
+
static forAppointment(appointmentId: string) {
|
|
243
|
+
return UnknownAppointment.refusal(`Appointment ${appointmentId} does not exist.`, { appointmentId });
|
|
214
244
|
}
|
|
215
245
|
}
|
|
216
246
|
|
|
217
|
-
throw
|
|
247
|
+
throw SlotTaken.forCalendar(calendar.id, slot.startsAt);
|
|
218
248
|
```
|
|
219
249
|
|
|
220
250
|
| Member | What it does |
|
|
221
251
|
|---|---|
|
|
222
|
-
| `
|
|
252
|
+
| `readonly code` | written by the class, read from the list of its context under its own name: one code per class, whatever the factory |
|
|
253
|
+
| `protected static refusal(message, facts?)` | builds the refusal of a factory: an instance of the class it is called on, typed with its code and those facts. The constructor is protected and a subclass declares none, so outside its class a refusal exists only through a factory |
|
|
223
254
|
| `protected exposing(...names)` | called on the refusal by its factory: these facts may leave the process. None does by default |
|
|
224
255
|
| `protected causedBy(cause)` | called by its factory: the error this refusal translates, as `cause` |
|
|
225
|
-
| `
|
|
256
|
+
| `message`, `name`, `cause` | as on any `DomainError`; `name` is the class |
|
|
226
257
|
| `facts` | every fact the factory was given, as plain data (`RefusalFacts`: text, numbers, booleans, dates, `null`, lists of them), for the code that catches the error and for the tests |
|
|
227
258
|
| `exposedFacts` | the facts a client may read: what `@ddd-ts-kit/http` writes under `facts` in the problem document |
|
|
228
259
|
| `toJSON()` | `{ name, code, message }`, plus `facts` when some are exposed: never the stack, the cause or a private fact |
|
|
229
|
-
| `static is(error
|
|
260
|
+
| `static is(error)` | narrows what was caught to this refusal, with the facts of each of its factories |
|
|
230
261
|
|
|
231
262
|
Instead of a bare `Error`, which nothing downstream can tell from a bug:
|
|
232
263
|
|
|
@@ -234,48 +265,71 @@ Instead of a bare `Error`, which nothing downstream can tell from a bug:
|
|
|
234
265
|
throw new Error(`Slot ${startsAt.toISOString()} is taken`); // reported as unexpected, answered as a 500
|
|
235
266
|
```
|
|
236
267
|
|
|
237
|
-
write the refusal
|
|
268
|
+
write the refusal, which every layer knows by its code:
|
|
238
269
|
|
|
239
270
|
```ts
|
|
240
|
-
throw
|
|
271
|
+
throw SlotTaken.forCalendar(calendar.id, slot.startsAt); // a planned answer: a 4xx with code SLOT_TAKEN
|
|
241
272
|
```
|
|
242
273
|
|
|
243
274
|
The HTTP status of each code is chosen in the presentation layer, in one place; the error
|
|
244
275
|
logs, the spans and the audit trail record the `code`, and the error reporter leaves a
|
|
245
276
|
`DomainError` alone (the two exceptions are under "ErrorReporter").
|
|
246
277
|
|
|
247
|
-
The codes of a
|
|
248
|
-
`switch` is exhaustive and
|
|
278
|
+
The codes of a context are a closed vocabulary, `SchedulingCode`: `isRefusal(error, SchedulingCodes)`
|
|
279
|
+
narrows what was caught to one of them, so a `switch` on its code is exhaustive, and the class of
|
|
280
|
+
one refusal knows its facts:
|
|
249
281
|
|
|
250
282
|
```ts
|
|
251
|
-
import { isRefusal
|
|
252
|
-
|
|
253
|
-
type AppointmentErrorCode = CodeOf<typeof AppointmentError>; // 'SLOT_TAKEN' | 'APPOINTMENT_NOT_FOUND'
|
|
283
|
+
import { isRefusal } from '@ddd-ts-kit/kernel';
|
|
254
284
|
|
|
255
|
-
if (
|
|
285
|
+
if (isRefusal(error, SchedulingCodes)) {
|
|
256
286
|
switch (error.code) {
|
|
257
287
|
case 'SLOT_TAKEN':
|
|
258
|
-
return
|
|
288
|
+
return 'choose another slot';
|
|
259
289
|
case 'APPOINTMENT_NOT_FOUND':
|
|
260
|
-
return
|
|
290
|
+
return 'book again';
|
|
261
291
|
}
|
|
262
292
|
}
|
|
263
293
|
|
|
264
|
-
if (
|
|
265
|
-
error.facts.
|
|
294
|
+
if (SlotTaken.is(error)) {
|
|
295
|
+
retryAfter(error.facts.startsAt); // a Date
|
|
266
296
|
}
|
|
267
297
|
|
|
268
298
|
isRefusal(error, 'SLOT_TAKEN'); // whatever the class: the code is what everything downstream keys on
|
|
269
299
|
```
|
|
270
300
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
would hide its code from `CodeOf`.
|
|
301
|
+
A factory lets its return type be inferred: an annotation `: SlotTaken` would hide its facts
|
|
302
|
+
from `is`.
|
|
274
303
|
|
|
275
304
|
**Facts are private.** A message is written for a human and answered to the client as `detail`;
|
|
276
305
|
the facts are data, and stay in the process unless the factory names the ones that may leave.
|
|
277
306
|
Never expose what identifies a person. The audit trail and the spans record the code alone.
|
|
278
307
|
|
|
308
|
+
**One class per concept** is the form of `ConceptError`, and keeps working: a class gathers the
|
|
309
|
+
refusals of one concept, each a factory named after the refusal that sets its own code, and
|
|
310
|
+
`CodeOf<typeof AppointmentError>` is the union of its codes.
|
|
311
|
+
|
|
312
|
+
<!-- checked sample -->
|
|
313
|
+
```ts
|
|
314
|
+
import { ConceptError, type CodeOf } from '@ddd-ts-kit/kernel';
|
|
315
|
+
|
|
316
|
+
export class AppointmentError extends ConceptError {
|
|
317
|
+
static slotTaken(calendarId: string, startsAt: Date) {
|
|
318
|
+
return AppointmentError.refusal('SLOT_TAKEN', `The slot at ${startsAt.toISOString()} is held.`, {
|
|
319
|
+
calendarId,
|
|
320
|
+
startsAt,
|
|
321
|
+
}).exposing('startsAt');
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export type AppointmentErrorCode = CodeOf<typeof AppointmentError>; // 'SLOT_TAKEN'
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
`ConceptError` has the members above, but `refusal(code, message, facts?)` takes the code, and
|
|
329
|
+
`AppointmentError.is(error, 'SLOT_TAKEN')` narrows to one code of the class. `ConceptError` and
|
|
330
|
+
`DomainRefusal` share `FactualRefusal`, the facts and their exposure, which is what
|
|
331
|
+
`@ddd-ts-kit/http` reads.
|
|
332
|
+
|
|
279
333
|
**One class per error** is the form of 0.1 and 0.2, and keeps working: `DomainError` is still
|
|
280
334
|
abstract over `code`, its constructor still public, and `InvariantViolation`, `NotFoundError`,
|
|
281
335
|
`ConflictError` and `ConcurrencyError`, each one refusal under a fixed code, are still built
|
|
@@ -288,28 +342,39 @@ export class SlotTakenError extends DomainError {
|
|
|
288
342
|
```
|
|
289
343
|
|
|
290
344
|
A project says which form it writes with `"stack.refusals"` in `ddd-ts-kit.config.json`
|
|
291
|
-
(`
|
|
292
|
-
`errors/*` rules hold the first, and `ddd-ts-kit error-factories`
|
|
293
|
-
|
|
345
|
+
(`named`, what `init` writes; `factories`, one class per concept; or `classes`, what a config
|
|
346
|
+
without the key means); the `errors/*` rules hold the first two, and `ddd-ts-kit error-factories`
|
|
347
|
+
moves a project from the others to `named`. The forms can live in one project.
|
|
294
348
|
|
|
295
349
|
## State machines
|
|
296
350
|
|
|
297
351
|
<!-- checked sample -->
|
|
298
352
|
```ts
|
|
299
|
-
import { AggregateRoot,
|
|
353
|
+
import { AggregateRoot, DomainRefusal, defineStateMachine } from '@ddd-ts-kit/kernel';
|
|
300
354
|
|
|
301
355
|
// domain/vocabularies/parcel-status.vocabulary.ts
|
|
302
356
|
export const ParcelStatus = { Registered: 'registered', Shipped: 'shipped', Delivered: 'delivered' } as const;
|
|
303
357
|
export type ParcelStatus = (typeof ParcelStatus)[keyof typeof ParcelStatus];
|
|
304
358
|
|
|
305
|
-
// domain/errors/
|
|
306
|
-
export
|
|
307
|
-
|
|
308
|
-
|
|
359
|
+
// domain/errors/codes.ts, then one class per refusal
|
|
360
|
+
export const ShippingCodes = {
|
|
361
|
+
ParcelUnpaid: 'PARCEL_UNPAID',
|
|
362
|
+
ParcelTransitionRefused: 'PARCEL_TRANSITION_REFUSED',
|
|
363
|
+
} as const;
|
|
364
|
+
|
|
365
|
+
export class ParcelUnpaid extends DomainRefusal {
|
|
366
|
+
readonly code = ShippingCodes.ParcelUnpaid;
|
|
367
|
+
|
|
368
|
+
static forParcel(parcelId: string) {
|
|
369
|
+
return ParcelUnpaid.refusal(`Parcel ${parcelId} is not paid yet.`, { parcelId });
|
|
309
370
|
}
|
|
371
|
+
}
|
|
310
372
|
|
|
311
|
-
|
|
312
|
-
|
|
373
|
+
export class ParcelTransitionRefused extends DomainRefusal {
|
|
374
|
+
readonly code = ShippingCodes.ParcelTransitionRefused;
|
|
375
|
+
|
|
376
|
+
static forParcel(parcelId: string, from: ParcelStatus, transition: string) {
|
|
377
|
+
return ParcelTransitionRefused.refusal(`Parcel ${parcelId} cannot ${transition} once ${from}.`, {
|
|
313
378
|
parcelId,
|
|
314
379
|
from,
|
|
315
380
|
transition,
|
|
@@ -331,12 +396,12 @@ export const parcelLifecycle = defineStateMachine({
|
|
|
331
396
|
ship: {
|
|
332
397
|
from: [ParcelStatus.Registered],
|
|
333
398
|
to: ParcelStatus.Shipped,
|
|
334
|
-
guard: ({ parcelId, paid }: ParcelMoment) => (paid ? undefined :
|
|
399
|
+
guard: ({ parcelId, paid }: ParcelMoment) => (paid ? undefined : ParcelUnpaid.forParcel(parcelId)),
|
|
335
400
|
},
|
|
336
401
|
reroute: { from: [ParcelStatus.Shipped] },
|
|
337
402
|
deliver: { from: [ParcelStatus.Shipped], to: ParcelStatus.Delivered },
|
|
338
403
|
},
|
|
339
|
-
refused: ({ from, transition }, { parcelId }: ParcelMoment) =>
|
|
404
|
+
refused: ({ from, transition }, { parcelId }: ParcelMoment) => ParcelTransitionRefused.forParcel(parcelId, from, transition),
|
|
340
405
|
});
|
|
341
406
|
|
|
342
407
|
// domain/aggregates/parcel.aggregate.ts: the aggregate asks the machine, never tests its status by hand
|
|
@@ -1425,13 +1490,17 @@ the instances as JSON, for tests and one process. The runner is `StoredProcessRu
|
|
|
1425
1490
|
|
|
1426
1491
|
<!-- checked sample -->
|
|
1427
1492
|
```ts
|
|
1428
|
-
import {
|
|
1493
|
+
import { DomainRefusal, type RateLimiter } from '@ddd-ts-kit/kernel';
|
|
1494
|
+
|
|
1495
|
+
export const IdentityCodes = { TooManyResetAttempts: 'TOO_MANY_RESET_ATTEMPTS' } as const;
|
|
1429
1496
|
|
|
1430
|
-
export class
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
}).exposing(
|
|
1497
|
+
export class TooManyResetAttempts extends DomainRefusal {
|
|
1498
|
+
readonly code = IdentityCodes.TooManyResetAttempts;
|
|
1499
|
+
|
|
1500
|
+
static withRetryAfter(retryAfterMs: number) {
|
|
1501
|
+
return TooManyResetAttempts.refusal('Too many password resets: try again later.', { retryAfterMs }).exposing(
|
|
1502
|
+
'retryAfterMs',
|
|
1503
|
+
);
|
|
1435
1504
|
}
|
|
1436
1505
|
}
|
|
1437
1506
|
|
|
@@ -1439,7 +1508,7 @@ export async function allowReset(limiter: RateLimiter, accountId: string): Promi
|
|
|
1439
1508
|
const decision = await limiter.consume(`password-reset:${accountId}`, { limit: 3, windowMs: 3_600_000 });
|
|
1440
1509
|
|
|
1441
1510
|
if (!decision.allowed) {
|
|
1442
|
-
throw
|
|
1511
|
+
throw TooManyResetAttempts.withRetryAfter(decision.retryAfterMs);
|
|
1443
1512
|
}
|
|
1444
1513
|
}
|
|
1445
1514
|
```
|
|
@@ -1970,7 +2039,7 @@ export class AttendanceRecord extends EventSourcedAggregate<AttendanceEvent, Att
|
|
|
1970
2039
|
|
|
1971
2040
|
noteNoShow(appointmentId: string, now: Date): void {
|
|
1972
2041
|
if (this.state.noShows >= 3) {
|
|
1973
|
-
throw
|
|
2042
|
+
throw PatientBarred.forRecord(this.id);
|
|
1974
2043
|
}
|
|
1975
2044
|
|
|
1976
2045
|
this.record(new NoShowNoted(this.id, now, appointmentId));
|
|
@@ -7,7 +7,7 @@ import type { StampedRow } from '../read-model/stamped-row.js';
|
|
|
7
7
|
export declare abstract class ReadModelRows<Row> {
|
|
8
8
|
/** The row of the key with its stamp, a deleted one included (without `row`). */
|
|
9
9
|
abstract find(key: string): Promise<StampedRow<Row> | undefined>;
|
|
10
|
-
/** Inserts unless the key exists
|
|
10
|
+
/** Inserts unless the key exists or the entry has no `row`, and says whether it did. */
|
|
11
11
|
abstract insert(entry: StampedRow<Row>): Promise<boolean>;
|
|
12
12
|
/** Replaces the row of the key if it still holds the stamp `held`, and says whether it did. */
|
|
13
13
|
abstract replace(entry: StampedRow<Row>, held: EventStamp): Promise<boolean>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-model-rows.d.ts","sourceRoot":"","sources":["../../../src/application/ports/read-model-rows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAE/D;;;GAGG;AACH,8BAAsB,aAAa,CAAC,GAAG;IACrC,iFAAiF;IACjF,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,CAAC;IAEjE,
|
|
1
|
+
{"version":3,"file":"read-model-rows.d.ts","sourceRoot":"","sources":["../../../src/application/ports/read-model-rows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAE/D;;;GAGG;AACH,8BAAsB,aAAa,CAAC,GAAG;IACrC,iFAAiF;IACjF,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,CAAC;IAEjE,wFAAwF;IACxF,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAE1D,+FAA+F;IAC/F,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9E"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { CodeOf } from './code-of.js';
|
|
2
|
-
import
|
|
3
|
-
import { DomainError } from './domain-error.js';
|
|
2
|
+
import { FactualRefusal } from './factual-refusal.js';
|
|
4
3
|
import type { RefusalFacts } from './refusal-facts.js';
|
|
5
4
|
import type { RefusalOf } from './refusal-of.js';
|
|
6
5
|
import type { Refusal } from './refusal.js';
|
|
@@ -18,26 +17,16 @@ import type { Refusal } from './refusal.js';
|
|
|
18
17
|
* only through a factory, with the code and the message that factory wrote. The facts are
|
|
19
18
|
* private until the factory exposes some by name (`.exposing('startsAt')`).
|
|
20
19
|
*/
|
|
21
|
-
export declare class ConceptError extends
|
|
20
|
+
export declare class ConceptError extends FactualRefusal {
|
|
22
21
|
/** Narrows what was caught to a refusal of this class, of this code when one is given. */
|
|
23
22
|
static is<C extends {
|
|
24
23
|
readonly prototype: object;
|
|
25
24
|
}, const Code extends CodeOf<C> = CodeOf<C>>(this: C, error: unknown, code?: Code): error is RefusalOf<C, Code>;
|
|
26
25
|
readonly code: string;
|
|
27
|
-
/** Every fact of the refusal, for the code that catches it and for the tests. Private to the process. */
|
|
28
|
-
readonly facts: RefusalFacts;
|
|
29
|
-
private exposedNames;
|
|
30
26
|
protected constructor(code: string, message: string, facts: RefusalFacts);
|
|
31
|
-
/** The facts a client may read: the ones the factory named, none by default. */
|
|
32
|
-
get exposedFacts(): RefusalFacts;
|
|
33
|
-
toJSON(): DomainErrorJson;
|
|
34
27
|
/** Builds the refusal of a factory: its code, the message it wrote and the facts it was given. */
|
|
35
28
|
protected static refusal<E, const Code extends string, const Facts extends RefusalFacts = RefusalFacts>(this: {
|
|
36
29
|
readonly prototype: E;
|
|
37
30
|
}, code: Code, message: string, facts?: Facts): Refusal<E, Code, Facts>;
|
|
38
|
-
/** Called by a factory: these facts may leave the process, in a problem document for instance. */
|
|
39
|
-
protected exposing(...names: readonly (keyof this['facts'] & string)[]): this;
|
|
40
|
-
/** Called by a factory: the error this refusal translates. */
|
|
41
|
-
protected causedBy(cause: unknown): this;
|
|
42
31
|
}
|
|
43
32
|
//# sourceMappingURL=concept-error.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"concept-error.d.ts","sourceRoot":"","sources":["../../../src/domain/errors/concept-error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,
|
|
1
|
+
{"version":3,"file":"concept-error.d.ts","sourceRoot":"","sources":["../../../src/domain/errors/concept-error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C;;;;;;;;;;;;;GAaG;AACH,qBAAa,YAAa,SAAQ,cAAc;IAC9C,0FAA0F;IAC1F,MAAM,CAAC,EAAE,CAAC,CAAC,SAAS;QAAE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,KAAK,CAAC,IAAI,SAAS,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,EAC1F,IAAI,EAAE,CAAC,EACP,KAAK,EAAE,OAAO,EACd,IAAI,CAAC,EAAE,IAAI,GACV,KAAK,IAAI,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,CAQ7B;IAED,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,SAAS,aAAa,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAGvE;IAED,kGAAkG;IAClG,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,EAAE,KAAK,CAAC,IAAI,SAAS,MAAM,EAAE,KAAK,CAAC,KAAK,SAAS,YAAY,GAAG,YAAY,EACpG,IAAI,EAAE;QAAE,QAAQ,CAAC,SAAS,EAAE,CAAC,CAAA;KAAE,EAC/B,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,KAAK,GACZ,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;CAK5B"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FactualRefusal } from './factual-refusal.js';
|
|
2
2
|
/**
|
|
3
3
|
* The errors of one concept of the model, each built by a static factory named in the words of
|
|
4
4
|
* the business:
|
|
@@ -13,7 +13,7 @@ import { DomainError } from './domain-error.js';
|
|
|
13
13
|
* only through a factory, with the code and the message that factory wrote. The facts are
|
|
14
14
|
* private until the factory exposes some by name (`.exposing('startsAt')`).
|
|
15
15
|
*/
|
|
16
|
-
export class ConceptError extends
|
|
16
|
+
export class ConceptError extends FactualRefusal {
|
|
17
17
|
/** Narrows what was caught to a refusal of this class, of this code when one is given. */
|
|
18
18
|
static is(error, code) {
|
|
19
19
|
const expected = code;
|
|
@@ -22,33 +22,12 @@ export class ConceptError extends DomainError {
|
|
|
22
22
|
(expected === undefined || error.code === expected));
|
|
23
23
|
}
|
|
24
24
|
code;
|
|
25
|
-
/** Every fact of the refusal, for the code that catches it and for the tests. Private to the process. */
|
|
26
|
-
facts;
|
|
27
|
-
exposedNames = [];
|
|
28
25
|
constructor(code, message, facts) {
|
|
29
|
-
super(message);
|
|
26
|
+
super(message, facts);
|
|
30
27
|
this.code = code;
|
|
31
|
-
this.facts = facts;
|
|
32
|
-
}
|
|
33
|
-
/** The facts a client may read: the ones the factory named, none by default. */
|
|
34
|
-
get exposedFacts() {
|
|
35
|
-
return Object.fromEntries(this.exposedNames.map((name) => [name, this.facts[name] ?? null]));
|
|
36
|
-
}
|
|
37
|
-
toJSON() {
|
|
38
|
-
return { ...super.toJSON(), ...(this.exposedNames.length === 0 ? {} : { facts: this.exposedFacts }) };
|
|
39
28
|
}
|
|
40
29
|
static refusal(code, message, facts = {}) {
|
|
41
30
|
return new this(code, message, facts);
|
|
42
31
|
}
|
|
43
|
-
/** Called by a factory: these facts may leave the process, in a problem document for instance. */
|
|
44
|
-
exposing(...names) {
|
|
45
|
-
this.exposedNames = names;
|
|
46
|
-
return this;
|
|
47
|
-
}
|
|
48
|
-
/** Called by a factory: the error this refusal translates. */
|
|
49
|
-
causedBy(cause) {
|
|
50
|
-
Object.defineProperty(this, 'cause', { value: cause, writable: true, configurable: true, enumerable: false });
|
|
51
|
-
return this;
|
|
52
|
-
}
|
|
53
32
|
}
|
|
54
33
|
//# sourceMappingURL=concept-error.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"concept-error.js","sourceRoot":"","sources":["../../../src/domain/errors/concept-error.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"concept-error.js","sourceRoot":"","sources":["../../../src/domain/errors/concept-error.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAKtD;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,YAAa,SAAQ,cAAc;IAC9C,0FAA0F;IAC1F,MAAM,CAAC,EAAE,CAEP,KAAc,EACd,IAAW;QAEX,MAAM,QAAQ,GAAuB,IAAI,CAAC;QAE1C,OAAO,CACL,KAAK,YAAY,YAAY;YAC7B,MAAM,CAAC,SAAS,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,CAAC;YAC1D,CAAC,QAAQ,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC,CACpD,CAAC;IACJ,CAAC;IAEQ,IAAI,CAAS;IAEtB,YAAsB,IAAY,EAAE,OAAe,EAAE,KAAmB;QACtE,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAUS,MAAM,CAAC,OAAO,CAAC,IAAY,EAAE,OAAe,EAAE,KAAK,GAAiB,EAAE;QAC9E,OAAO,IAAI,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;CACF"}
|
|
@@ -4,6 +4,8 @@ export declare const CONCURRENCY_CONFLICT = "CONCURRENCY_CONFLICT";
|
|
|
4
4
|
/** Somebody else saved the aggregate since it was loaded. */
|
|
5
5
|
export declare class ConcurrencyError extends DomainError {
|
|
6
6
|
readonly code = "CONCURRENCY_CONFLICT";
|
|
7
|
-
constructor(aggregateId: string | number
|
|
7
|
+
constructor(aggregateId: string | number, options?: {
|
|
8
|
+
readonly cause?: unknown;
|
|
9
|
+
});
|
|
8
10
|
}
|
|
9
11
|
//# sourceMappingURL=concurrency-error.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"concurrency-error.d.ts","sourceRoot":"","sources":["../../../src/domain/errors/concurrency-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,uFAAuF;AACvF,eAAO,MAAM,oBAAoB,yBAAyB,CAAC;AAE3D,6DAA6D;AAC7D,qBAAa,gBAAiB,SAAQ,WAAW;IAC/C,QAAQ,CAAC,IAAI,0BAAwB;IAErC,YAAY,WAAW,EAAE,MAAM,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"concurrency-error.d.ts","sourceRoot":"","sources":["../../../src/domain/errors/concurrency-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,uFAAuF;AACvF,eAAO,MAAM,oBAAoB,yBAAyB,CAAC;AAE3D,6DAA6D;AAC7D,qBAAa,gBAAiB,SAAQ,WAAW;IAC/C,QAAQ,CAAC,IAAI,0BAAwB;IAErC,YAAY,WAAW,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,EAE/E;CACF"}
|
|
@@ -4,8 +4,8 @@ export const CONCURRENCY_CONFLICT = 'CONCURRENCY_CONFLICT';
|
|
|
4
4
|
/** Somebody else saved the aggregate since it was loaded. */
|
|
5
5
|
export class ConcurrencyError extends DomainError {
|
|
6
6
|
code = CONCURRENCY_CONFLICT;
|
|
7
|
-
constructor(aggregateId) {
|
|
8
|
-
super(`Aggregate ${aggregateId} was modified concurrently
|
|
7
|
+
constructor(aggregateId, options) {
|
|
8
|
+
super(`Aggregate ${aggregateId} was modified concurrently`, options);
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
//# sourceMappingURL=concurrency-error.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"concurrency-error.js","sourceRoot":"","sources":["../../../src/domain/errors/concurrency-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,uFAAuF;AACvF,MAAM,CAAC,MAAM,oBAAoB,GAAG,sBAAsB,CAAC;AAE3D,6DAA6D;AAC7D,MAAM,OAAO,gBAAiB,SAAQ,WAAW;IACtC,IAAI,GAAG,oBAAoB,CAAC;IAErC,YAAY,WAA4B;
|
|
1
|
+
{"version":3,"file":"concurrency-error.js","sourceRoot":"","sources":["../../../src/domain/errors/concurrency-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,uFAAuF;AACvF,MAAM,CAAC,MAAM,oBAAoB,GAAG,sBAAsB,CAAC;AAE3D,6DAA6D;AAC7D,MAAM,OAAO,gBAAiB,SAAQ,WAAW;IACtC,IAAI,GAAG,oBAAoB,CAAC;IAErC,YAAY,WAA4B,EAAE,OAAsC;QAC9E,KAAK,CAAC,aAAa,WAAW,4BAA4B,EAAE,OAAO,CAAC,CAAC;IACvE,CAAC;CACF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { FactualRefusal } from './factual-refusal.js';
|
|
2
|
+
import type { RefusalFacts } from './refusal-facts.js';
|
|
3
|
+
import type { RefusalOf } from './refusal-of.js';
|
|
4
|
+
import type { Refusal } from './refusal.js';
|
|
5
|
+
/**
|
|
6
|
+
* One refusal of the model, named after what is refused, with its code, and built by static
|
|
7
|
+
* factories named after what the refusal is about:
|
|
8
|
+
*
|
|
9
|
+
* export class InsufficientFunds extends DomainRefusal {
|
|
10
|
+
* readonly code = 'INSUFFICIENT_FUNDS';
|
|
11
|
+
*
|
|
12
|
+
* static forAccountId(accountId: string, missing: number) {
|
|
13
|
+
* return InsufficientFunds.refusal(`Account ${accountId} lacks ${missing}.`, { accountId, missing });
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* throw InsufficientFunds.forAccountId(account.id, missing);
|
|
18
|
+
*
|
|
19
|
+
* The constructor is protected and a subclass declares none: outside its class a refusal exists
|
|
20
|
+
* only through a factory, with the message that factory wrote. The facts are private until the
|
|
21
|
+
* factory exposes some by name (`.exposing('missing')`).
|
|
22
|
+
*/
|
|
23
|
+
export declare abstract class DomainRefusal extends FactualRefusal {
|
|
24
|
+
/** Narrows what was caught to this refusal, with the facts of its factories. */
|
|
25
|
+
static is<C extends {
|
|
26
|
+
readonly prototype: object;
|
|
27
|
+
}>(this: C, error: unknown): error is RefusalOf<C>;
|
|
28
|
+
protected constructor(message: string, facts?: RefusalFacts);
|
|
29
|
+
/** Builds the refusal of a factory: the message it wrote and the facts it was given. */
|
|
30
|
+
protected static refusal<E extends {
|
|
31
|
+
readonly code: string;
|
|
32
|
+
}, const Facts extends RefusalFacts = RefusalFacts>(this: {
|
|
33
|
+
readonly prototype: E;
|
|
34
|
+
}, message: string, facts?: Facts): Refusal<E, E['code'], Facts>;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=domain-refusal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"domain-refusal.d.ts","sourceRoot":"","sources":["../../../src/domain/errors/domain-refusal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C;;;;;;;;;;;;;;;;;GAiBG;AACH,8BAAsB,aAAc,SAAQ,cAAc;IACxD,gFAAgF;IAChF,MAAM,CAAC,EAAE,CAAC,CAAC,SAAS;QAAE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAAC,CAAC,CAAC,CAElG;IAGD,SAAS,aAAa,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE,YAAiB,EAE9D;IAED,wFAAwF;IACxF,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,KAAK,CAAC,KAAK,SAAS,YAAY,GAAG,YAAY,EAC3G,IAAI,EAAE;QAAE,QAAQ,CAAC,SAAS,EAAE,CAAC,CAAA;KAAE,EAC/B,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,KAAK,GACZ,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,CAAC;CAYjC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { FactualRefusal } from './factual-refusal.js';
|
|
2
|
+
/**
|
|
3
|
+
* One refusal of the model, named after what is refused, with its code, and built by static
|
|
4
|
+
* factories named after what the refusal is about:
|
|
5
|
+
*
|
|
6
|
+
* export class InsufficientFunds extends DomainRefusal {
|
|
7
|
+
* readonly code = 'INSUFFICIENT_FUNDS';
|
|
8
|
+
*
|
|
9
|
+
* static forAccountId(accountId: string, missing: number) {
|
|
10
|
+
* return InsufficientFunds.refusal(`Account ${accountId} lacks ${missing}.`, { accountId, missing });
|
|
11
|
+
* }
|
|
12
|
+
* }
|
|
13
|
+
*
|
|
14
|
+
* throw InsufficientFunds.forAccountId(account.id, missing);
|
|
15
|
+
*
|
|
16
|
+
* The constructor is protected and a subclass declares none: outside its class a refusal exists
|
|
17
|
+
* only through a factory, with the message that factory wrote. The facts are private until the
|
|
18
|
+
* factory exposes some by name (`.exposing('missing')`).
|
|
19
|
+
*/
|
|
20
|
+
export class DomainRefusal extends FactualRefusal {
|
|
21
|
+
/** Narrows what was caught to this refusal, with the facts of its factories. */
|
|
22
|
+
static is(error) {
|
|
23
|
+
return error instanceof DomainRefusal && Object.prototype.isPrototypeOf.call(this.prototype, error);
|
|
24
|
+
}
|
|
25
|
+
// A factory calls `refusal`; the facts a subclass is built with are the ones `refusal` was given.
|
|
26
|
+
constructor(message, facts = {}) {
|
|
27
|
+
super(message, facts);
|
|
28
|
+
}
|
|
29
|
+
static refusal(message, facts) {
|
|
30
|
+
return new this(message, facts);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=domain-refusal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"domain-refusal.js","sourceRoot":"","sources":["../../../src/domain/errors/domain-refusal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAKtD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAgB,aAAc,SAAQ,cAAc;IACxD,gFAAgF;IAChF,MAAM,CAAC,EAAE,CAAoD,KAAc;QACzE,OAAO,KAAK,YAAY,aAAa,IAAI,MAAM,CAAC,SAAS,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;IACtG,CAAC;IAED,kGAAkG;IAClG,YAAsB,OAAe,EAAE,KAAK,GAAiB,EAAE;QAC7D,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IACxB,CAAC;IASS,MAAM,CAAC,OAAO,CAKtB,OAAe,EACf,KAAoB;QAEpB,OAAO,IAAI,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAClC,CAAC;CACF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { DomainErrorJson } from './domain-error-json.js';
|
|
2
|
+
import { DomainError } from './domain-error.js';
|
|
3
|
+
import type { RefusalFacts } from './refusal-facts.js';
|
|
4
|
+
/**
|
|
5
|
+
* A refusal built by a static factory, whichever form the project writes (`DomainRefusal`, one
|
|
6
|
+
* class per refusal, or `ConceptError`, one class per concept): it carries the facts the factory
|
|
7
|
+
* was given, keeps them private until the factory exposes some by name (`.exposing('startsAt')`),
|
|
8
|
+
* and may keep the error it translates (`.causedBy(cause)`).
|
|
9
|
+
*/
|
|
10
|
+
export declare abstract class FactualRefusal extends DomainError {
|
|
11
|
+
/** Every fact of the refusal, for the code that catches it and for the tests. Private to the process. */
|
|
12
|
+
readonly facts: RefusalFacts;
|
|
13
|
+
private exposedNames;
|
|
14
|
+
protected constructor(message: string, facts: RefusalFacts);
|
|
15
|
+
/** The facts a client may read: the ones the factory named, none by default. */
|
|
16
|
+
get exposedFacts(): RefusalFacts;
|
|
17
|
+
toJSON(): DomainErrorJson;
|
|
18
|
+
/** Called by a factory: these facts may leave the process, in a problem document for instance. */
|
|
19
|
+
protected exposing(...names: readonly (keyof this['facts'] & string)[]): this;
|
|
20
|
+
/** Called by a factory: the error this refusal translates. */
|
|
21
|
+
protected causedBy(cause: unknown): this;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=factual-refusal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factual-refusal.d.ts","sourceRoot":"","sources":["../../../src/domain/errors/factual-refusal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD;;;;;GAKG;AACH,8BAAsB,cAAe,SAAQ,WAAW;IACtD,yGAAyG;IACzG,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;IAE7B,OAAO,CAAC,YAAY,CAAyB;IAE7C,SAAS,aAAa,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAGzD;IAED,gFAAgF;IAChF,IAAI,YAAY,IAAI,YAAY,CAE/B;IAEQ,MAAM,IAAI,eAAe,CAEjC;IAED,kGAAkG;IAClG,SAAS,CAAC,QAAQ,CAAC,GAAG,KAAK,EAAE,SAAS,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,EAAE,GAAG,IAAI,CAI5E;IAED,8DAA8D;IAC9D,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAIvC;CACF"}
|