@cullet/erp-core 1.1.0 → 1.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 (196) hide show
  1. package/KIT_CONTEXT.md +1 -67
  2. package/dist/app-error.d.cts +108 -0
  3. package/dist/application/index.cjs +26 -0
  4. package/dist/application/index.d.cts +2 -0
  5. package/dist/application/index.d.ts +1 -1
  6. package/dist/application/index.js +2 -1
  7. package/dist/domain/index.cjs +16 -0
  8. package/dist/domain/index.d.cts +3 -0
  9. package/dist/domain/index.d.ts +3 -0
  10. package/dist/domain/index.js +3 -0
  11. package/dist/domain-event-contracts.cjs +33 -0
  12. package/dist/domain-event-contracts.cjs.map +1 -0
  13. package/dist/domain-event-contracts.d.cts +27 -0
  14. package/dist/domain-event-contracts.d.ts +27 -0
  15. package/dist/domain-event-contracts.js +22 -0
  16. package/dist/domain-event-contracts.js.map +1 -0
  17. package/dist/domain-exception.cjs +17 -0
  18. package/dist/domain-exception.cjs.map +1 -0
  19. package/dist/domain-exception.d.cts +7 -0
  20. package/dist/domain-exception.d.ts +7 -0
  21. package/dist/domain-exception.js +12 -0
  22. package/dist/domain-exception.js.map +1 -0
  23. package/dist/errors/index.cjs +41 -0
  24. package/dist/errors/index.d.cts +3 -0
  25. package/dist/errors/index.js +1 -1
  26. package/dist/exceptions/index.cjs +17 -0
  27. package/dist/exceptions/index.d.cts +5 -0
  28. package/dist/exceptions/index.d.ts +5 -0
  29. package/dist/exceptions/index.js +7 -0
  30. package/dist/exceptions/validation-field.cjs +3 -0
  31. package/dist/exceptions/validation-field.d.cts +2 -0
  32. package/dist/gate-engine-registry.cjs +308 -0
  33. package/dist/gate-engine-registry.cjs.map +1 -0
  34. package/dist/gate-engine-registry.d.cts +81 -0
  35. package/dist/gate-engine-registry.d.ts +2 -1
  36. package/dist/gate-engine-registry.js +2 -1
  37. package/dist/gate-engine-registry.js.map +1 -1
  38. package/dist/gate-types.d.cts +171 -0
  39. package/dist/gate-types.d.ts +3 -139
  40. package/dist/gate-v1-payload.schema.cjs +638 -0
  41. package/dist/gate-v1-payload.schema.cjs.map +1 -0
  42. package/dist/gate-v1-payload.schema.js +2 -116
  43. package/dist/gate-v1-payload.schema.js.map +1 -1
  44. package/dist/hashing.cjs +66 -0
  45. package/dist/hashing.cjs.map +1 -0
  46. package/dist/immutable.cjs +77 -0
  47. package/dist/immutable.cjs.map +1 -0
  48. package/dist/immutable.d.cts +6 -0
  49. package/dist/immutable.d.ts +6 -0
  50. package/dist/immutable.js +54 -0
  51. package/dist/immutable.js.map +1 -0
  52. package/dist/index.cjs +163 -0
  53. package/dist/index.cjs.map +1 -0
  54. package/dist/index.d.cts +30 -0
  55. package/dist/index.d.ts +15 -160
  56. package/dist/index.js +19 -176
  57. package/dist/index.js.map +1 -1
  58. package/dist/invalid-state-transition-exception.cjs +47 -0
  59. package/dist/invalid-state-transition-exception.cjs.map +1 -0
  60. package/dist/invalid-state-transition-exception.js +30 -0
  61. package/dist/invalid-state-transition-exception.js.map +1 -0
  62. package/dist/invariant-violation-exception.cjs +16 -0
  63. package/dist/invariant-violation-exception.cjs.map +1 -0
  64. package/dist/invariant-violation-exception.js +11 -0
  65. package/dist/invariant-violation-exception.js.map +1 -0
  66. package/dist/not-found-error.cjs +65 -0
  67. package/dist/not-found-error.cjs.map +1 -0
  68. package/dist/not-found-error.js +1 -1
  69. package/dist/outcome.cjs +97 -0
  70. package/dist/outcome.cjs.map +1 -0
  71. package/dist/outcome.d.cts +140 -0
  72. package/dist/outcome.d.ts +140 -0
  73. package/dist/outcome.js +1 -1
  74. package/dist/parse-gate-payload.d.cts +62 -0
  75. package/dist/parse-gate-payload.d.ts +2 -1
  76. package/dist/path.d.cts +90 -0
  77. package/dist/path.d.ts +2 -1
  78. package/dist/plugin.cjs +79 -0
  79. package/dist/plugin.cjs.map +1 -0
  80. package/dist/plugin.d.cts +85 -0
  81. package/dist/plugin.d.ts +85 -0
  82. package/dist/plugin.js +74 -0
  83. package/dist/plugin.js.map +1 -0
  84. package/dist/plugins/index.cjs +3 -0
  85. package/dist/plugins/index.d.cts +2 -0
  86. package/dist/plugins/index.d.ts +2 -0
  87. package/dist/plugins/index.js +2 -0
  88. package/dist/policies/engines/index.cjs +10 -0
  89. package/dist/policies/engines/index.d.cts +4 -0
  90. package/dist/policies/engines/index.d.ts +1 -1
  91. package/dist/policies/engines/v1/gate/index.cjs +91 -0
  92. package/dist/policies/engines/v1/gate/index.cjs.map +1 -0
  93. package/dist/policies/engines/v1/gate/index.d.cts +121 -0
  94. package/dist/policies/engines/v1/gate/index.d.ts +2 -1
  95. package/dist/policies/engines/v1/gate/index.js +2 -1
  96. package/dist/policies/engines/v1/gate/index.js.map +1 -1
  97. package/dist/policies/index.cjs +41 -0
  98. package/dist/policies/index.d.cts +7 -0
  99. package/dist/policies/index.d.ts +2 -1
  100. package/dist/policies/index.js +2 -1
  101. package/dist/policy-service.cjs +1190 -0
  102. package/dist/policy-service.cjs.map +1 -0
  103. package/dist/policy-service.d.cts +559 -0
  104. package/dist/policy-service.d.ts +2 -1
  105. package/dist/policy-service.js +6 -3
  106. package/dist/policy-service.js.map +1 -1
  107. package/dist/result/index.cjs +7 -0
  108. package/dist/result/index.d.cts +2 -0
  109. package/dist/result/index.d.ts +2 -0
  110. package/dist/result/index.js +3 -0
  111. package/dist/result.cjs +135 -0
  112. package/dist/result.cjs.map +1 -0
  113. package/dist/result.js +118 -0
  114. package/dist/result.js.map +1 -0
  115. package/dist/ruleset-registry.cjs +47 -0
  116. package/dist/ruleset-registry.cjs.map +1 -0
  117. package/dist/ruleset-registry.js +42 -0
  118. package/dist/ruleset-registry.js.map +1 -0
  119. package/dist/rulesets/index.cjs +3 -0
  120. package/dist/rulesets/index.d.cts +2 -0
  121. package/dist/rulesets/index.d.ts +2 -0
  122. package/dist/rulesets/index.js +2 -0
  123. package/dist/temporal-guards.cjs +32 -0
  124. package/dist/temporal-guards.cjs.map +1 -0
  125. package/dist/temporal-guards.js +2 -17
  126. package/dist/temporal-guards.js.map +1 -1
  127. package/dist/temporal-use-case.cjs +191 -0
  128. package/dist/temporal-use-case.cjs.map +1 -0
  129. package/dist/temporal-use-case.d.cts +304 -0
  130. package/dist/temporal-use-case.d.ts +4 -9
  131. package/dist/temporal-use-case.js +6 -140
  132. package/dist/temporal-use-case.js.map +1 -1
  133. package/dist/unexpected-error.cjs +208 -0
  134. package/dist/unexpected-error.cjs.map +1 -0
  135. package/dist/unexpected-error.js +179 -0
  136. package/dist/unexpected-error.js.map +1 -0
  137. package/dist/use-case.cjs +96 -0
  138. package/dist/use-case.cjs.map +1 -0
  139. package/dist/use-case.js +91 -0
  140. package/dist/use-case.js.map +1 -0
  141. package/dist/uuid-identifier.cjs +65 -0
  142. package/dist/uuid-identifier.cjs.map +1 -0
  143. package/dist/uuid-identifier.d.cts +230 -0
  144. package/dist/uuid-identifier.d.ts +230 -0
  145. package/dist/uuid-identifier.js +60 -0
  146. package/dist/uuid-identifier.js.map +1 -0
  147. package/dist/validation-code.cjs +60 -0
  148. package/dist/validation-code.cjs.map +1 -0
  149. package/dist/validation-code.d.cts +23 -0
  150. package/dist/validation-code.d.ts +23 -0
  151. package/dist/validation-code.js +1 -177
  152. package/dist/validation-code.js.map +1 -1
  153. package/dist/validation-error.cjs +1020 -0
  154. package/dist/validation-error.cjs.map +1 -0
  155. package/dist/validation-error.d.cts +777 -0
  156. package/dist/validation-error.d.ts +1 -21
  157. package/dist/validation-error.js +1 -1
  158. package/dist/validation-exception.cjs +41 -0
  159. package/dist/validation-exception.cjs.map +1 -0
  160. package/dist/validation-exception.d.cts +50 -0
  161. package/dist/validation-exception.d.ts +50 -0
  162. package/dist/validation-exception.js +8 -2
  163. package/dist/validation-exception.js.map +1 -1
  164. package/dist/validation-field.cjs +28 -0
  165. package/dist/validation-field.cjs.map +1 -0
  166. package/dist/validation-field.d.cts +12 -0
  167. package/dist/value-object-ruleset.contracts.d.cts +36 -0
  168. package/dist/value-object-ruleset.contracts.d.ts +36 -0
  169. package/dist/value-object.cjs +208 -0
  170. package/dist/value-object.cjs.map +1 -0
  171. package/dist/value-object.js +191 -0
  172. package/dist/value-object.js.map +1 -0
  173. package/dist/version.d.cts +10 -0
  174. package/dist/version.d.ts +10 -0
  175. package/dist/versioning/index.cjs +7 -0
  176. package/dist/versioning/index.d.cts +3 -0
  177. package/dist/versioning/index.d.ts +3 -0
  178. package/dist/versioning/index.js +3 -0
  179. package/meta.json +18 -3
  180. package/package.json +165 -17
  181. package/src/core/domain/rulesets/index.ts +7 -0
  182. package/src/core/domain/uuid-identifier.ts +72 -0
  183. package/src/core/domain/value-object.ts +37 -5
  184. package/src/core/exceptions/index.ts +13 -0
  185. package/src/core/index.ts +14 -2
  186. package/src/core/plugins/index.ts +7 -0
  187. package/src/core/plugins/plugin.ts +99 -0
  188. package/src/core/plugins/types.ts +53 -0
  189. package/src/core/versioning/index.ts +14 -0
  190. package/src/domain/index.ts +7 -0
  191. package/src/exceptions/index.ts +1 -0
  192. package/src/plugins/index.ts +1 -0
  193. package/src/result/index.ts +2 -0
  194. package/src/rulesets/index.ts +1 -0
  195. package/src/version.ts +1 -1
  196. package/src/versioning/index.ts +1 -0
@@ -0,0 +1,230 @@
1
+ import { n as ContractVersion } from "./version.cjs";
2
+ import { t as DeepReadonly } from "./immutable.cjs";
3
+ import { t as PluginManager } from "./plugin.cjs";
4
+
5
+ //#region src/core/domain/entity.d.ts
6
+
7
+ /**
8
+ * The minimal persisted shape needed to reconstitute an {@link Entity}.
9
+ *
10
+ * This is the contract between storage and the domain: a repository maps a row
11
+ * (or document) onto these four fields and hands them to a subclass constructor.
12
+ * `aggregateVersion` travels with the state so optimistic-concurrency checks
13
+ * survive a round-trip through the database.
14
+ */
15
+ interface EntityState<TIdentifier> {
16
+ readonly id: TIdentifier;
17
+ readonly createdAt: Date;
18
+ readonly updatedAt: Date;
19
+ readonly aggregateVersion: number;
20
+ }
21
+ /**
22
+ * Base class for domain entities — objects defined by a stable identity rather
23
+ * than by their attributes. Two entities are "the same" when their `id`
24
+ * matches, even if every other field differs; this is the opposite of a
25
+ * {@link ValueObject}, which is defined entirely by its contents.
26
+ *
27
+ * The class owns the bookkeeping common to every aggregate root: a creation
28
+ * timestamp that never changes, a last-modified timestamp, and an
29
+ * `aggregateVersion` counter used for optimistic concurrency control. All three
30
+ * are validated on construction and the dates are defensively cloned, so an
31
+ * entity can never be built into — or leak — an inconsistent temporal state.
32
+ *
33
+ * @typeParam TIdentifier - The identity type (e.g. a branded string id or a VO).
34
+ */
35
+ declare abstract class Entity<TIdentifier> {
36
+ static readonly CONTRACT_VERSION: ContractVersion;
37
+ private readonly _id;
38
+ private readonly _createdAt;
39
+ private _updatedAt;
40
+ private _aggregateVersion;
41
+ /**
42
+ * Reconstitutes an entity from its persisted {@link EntityState}.
43
+ *
44
+ * Validates the temporal invariants up front so an invalid entity is
45
+ * impossible to construct: both dates must be valid, the aggregate version
46
+ * must be a non-negative integer, and `updatedAt` may not predate
47
+ * `createdAt`. Both dates are cloned on the way in so a later mutation of
48
+ * the caller's `Date` objects cannot reach into the entity's internal state.
49
+ *
50
+ * Declared `protected` because entities are reconstituted through a
51
+ * subclass factory, never instantiated directly by application code.
52
+ *
53
+ * @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.
54
+ */
55
+ protected constructor(state: EntityState<TIdentifier>);
56
+ /** The entity's stable identity — the basis for equality between entities. */
57
+ get id(): TIdentifier;
58
+ /**
59
+ * When the entity was first created.
60
+ *
61
+ * Returns a clone so callers cannot mutate the entity's internal `Date`;
62
+ * the value is fixed at construction and never changes thereafter.
63
+ */
64
+ get createdAt(): Date;
65
+ /**
66
+ * When the entity was last modified.
67
+ *
68
+ * Returns a clone for the same encapsulation reason as {@link createdAt};
69
+ * the underlying value advances only through {@link markAsModified}.
70
+ */
71
+ get updatedAt(): Date;
72
+ /**
73
+ * Monotonic counter incremented on every mutation, used for optimistic
74
+ * concurrency control: a writer reads this value, and the persistence layer
75
+ * rejects the write if the stored version has moved on in the meantime.
76
+ */
77
+ get aggregateVersion(): number;
78
+ /**
79
+ * The schema/contract version stamped on this entity type by the
80
+ * `@version` decorator — used to detect and migrate state persisted under
81
+ * an older shape.
82
+ */
83
+ get contractVersion(): ContractVersion;
84
+ /**
85
+ * The single seam through which an entity records a mutation: it advances
86
+ * `updatedAt` and bumps {@link aggregateVersion} by one. Subclasses must
87
+ * call this from every state-changing method so the version counter stays
88
+ * an accurate optimistic-lock token.
89
+ *
90
+ * @param updatedAt - The modification instant; defaults to now. Validated
91
+ * and required not to predate `createdAt`, preserving the same invariant
92
+ * the constructor enforces.
93
+ * @returns The new aggregate version after the increment.
94
+ * @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.
95
+ */
96
+ protected markAsModified(updatedAt?: Date): number;
97
+ }
98
+ //#endregion
99
+ //#region src/core/domain/value-object.d.ts
100
+ /**
101
+ * Extension point for value-object equality. A plugin implementing this
102
+ * contract overrides how any two value objects are compared, letting the host
103
+ * application swap in a structural comparator (e.g. `lodash.isEqual` over the
104
+ * wrapped `value`) without every value object having to implement `equals` by
105
+ * hand.
106
+ */
107
+ type ValueObjectPluginContract = {
108
+ equals: (a: ValueObject<unknown, unknown>, b: ValueObject<unknown, unknown>) => boolean;
109
+ };
110
+ /**
111
+ * Base class for value objects — domain concepts defined entirely by their
112
+ * contents, with no identity of their own. Two value objects are
113
+ * interchangeable when they hold equal data, which is the opposite of an
114
+ * {@link Entity} (compared by id). Money, a CPF, a date range: replacing one
115
+ * instance with an equal one changes nothing about the model.
116
+ *
117
+ * Immutability is the defining guarantee here. The wrapped `value` is
118
+ * deep-frozen on construction, so a value object can be shared freely without
119
+ * any risk of a consumer mutating shared state. Subclasses seal the instance
120
+ * itself with {@link finalize} once their own fields are set.
121
+ *
122
+ * @typeParam T - The shape of the wrapped data.
123
+ * @typeParam P - The primitive form produced by {@link toPrimitive} / {@link toJSON}.
124
+ */
125
+ declare abstract class ValueObject<T, P> {
126
+ static readonly CONTRACT_VERSION: ContractVersion;
127
+ /**
128
+ * Registry of equality plugins shared by every value object. Empty by
129
+ * default — when nothing is registered, {@link equals} falls back to a
130
+ * structural comparison of the wrapped `value`. Hosts register a plugin
131
+ * (e.g. `lodash.isEqual`) once at startup to customise equality globally.
132
+ */
133
+ static readonly plugins: PluginManager<ValueObjectPluginContract>;
134
+ /** The wrapped data, deep-frozen so it can never be mutated after construction. */
135
+ readonly value: DeepReadonly<T>;
136
+ /**
137
+ * Wraps `value`, deep-freezing it so the value object is immutable from the
138
+ * moment it exists. Declared `protected` because value objects are built
139
+ * through a validating subclass factory, never instantiated directly.
140
+ */
141
+ protected constructor(value: T);
142
+ /**
143
+ * The schema/contract version stamped on this value-object type by the
144
+ * `@version` decorator — used to detect state persisted under an older shape.
145
+ */
146
+ get contractVersion(): ContractVersion;
147
+ /**
148
+ * Freezes the instance shell, blocking reassignment of any own field.
149
+ *
150
+ * `value` is already deep-frozen by the constructor, so the wrapped data is
151
+ * immutable regardless. The instance itself is NOT frozen automatically
152
+ * because a subclass may still need to assign its own fields after
153
+ * `super(value)` runs. Call `finalize()` at the very end of the subclass
154
+ * constructor (after all fields are set) to make the whole value object
155
+ * immutable.
156
+ */
157
+ protected finalize(): void;
158
+ /**
159
+ * Hook for `JSON.stringify`. Delegates to {@link toPrimitive} so a value
160
+ * object serializes to its primitive form rather than exposing the internal
161
+ * `value` wrapper.
162
+ */
163
+ toJSON(): P;
164
+ /**
165
+ * Compares this value object with another by content. Because value objects
166
+ * carry no identity, two independently constructed instances holding the
167
+ * same data are considered equal.
168
+ *
169
+ * Delegates to the registered equality {@link plugins}; with no plugin
170
+ * registered it falls back to comparing the serialized wrapped `value`.
171
+ * Subclasses may still override for a faster or domain-specific comparison.
172
+ */
173
+ equals(other: this): boolean;
174
+ /**
175
+ * Projects the value object down to a plain, serializable primitive form —
176
+ * the representation suitable for persistence, transport, or comparison.
177
+ */
178
+ abstract toPrimitive(): P;
179
+ }
180
+ //#endregion
181
+ //#region src/core/domain/uuid-identifier.d.ts
182
+ /**
183
+ * Base class for UUID-backed identity value objects.
184
+ *
185
+ * A domain typically has many of these — `OrderId`, `CustomerId`, `InvoiceId` —
186
+ * all wrapping the same `string` shape. Two problems follow: the UUID format
187
+ * check gets copy-pasted into every one, and because the wrapped shape is
188
+ * identical, TypeScript's structural typing would happily accept an `OrderId`
189
+ * where a `CustomerId` is expected. `UuidIdentifier` solves both: the format
190
+ * lives here once ({@link isValid}), and the `TBrand` phantom tag makes each
191
+ * subtype nominally distinct so the ids cannot be swapped for one another.
192
+ *
193
+ * It deliberately does not impose a factory. Identifiers vary in how they
194
+ * report an invalid value (their own exception type, their own message), so a
195
+ * concrete id adds a validating `create` that calls {@link isValid} plus a
196
+ * `reconstitute` that trusts an already-persisted value:
197
+ *
198
+ * ```ts
199
+ * class OrderId extends UuidIdentifier<"OrderId"> {
200
+ * private constructor(value: string) { super(value); }
201
+ * static create(raw: string): OrderId {
202
+ * if (!UuidIdentifier.isValid(raw)) throw new InvalidOrderIdError(raw);
203
+ * return new OrderId(raw);
204
+ * }
205
+ * static reconstitute(value: string): OrderId { return new OrderId(value); }
206
+ * }
207
+ * ```
208
+ *
209
+ * @typeParam TBrand - A unique string literal that nominally tags the subtype.
210
+ */
211
+ declare abstract class UuidIdentifier<TBrand extends string = string> extends ValueObject<string, string> {
212
+ /**
213
+ * Phantom brand. Never assigned at runtime (`declare`), it exists only so
214
+ * two identifiers with different brands are not interchangeable at the type
215
+ * level despite sharing the same `string` value.
216
+ */
217
+ protected readonly __brand: TBrand;
218
+ protected constructor(value: string);
219
+ toPrimitive(): string;
220
+ /** The wrapped UUID string — convenient for logging and interpolation. */
221
+ toString(): string;
222
+ /**
223
+ * Whether `candidate` is a canonical UUID (versions 1–5). The single source
224
+ * of truth for the format; concrete identifiers call this from `create`.
225
+ */
226
+ static isValid(candidate: string): boolean;
227
+ }
228
+ //#endregion
229
+ export { EntityState as a, Entity as i, ValueObject as n, ValueObjectPluginContract as r, UuidIdentifier as t };
230
+ //# sourceMappingURL=uuid-identifier.d.cts.map
@@ -0,0 +1,230 @@
1
+ import { n as ContractVersion } from "./version.js";
2
+ import { t as DeepReadonly } from "./immutable.js";
3
+ import { t as PluginManager } from "./plugin.js";
4
+
5
+ //#region src/core/domain/entity.d.ts
6
+
7
+ /**
8
+ * The minimal persisted shape needed to reconstitute an {@link Entity}.
9
+ *
10
+ * This is the contract between storage and the domain: a repository maps a row
11
+ * (or document) onto these four fields and hands them to a subclass constructor.
12
+ * `aggregateVersion` travels with the state so optimistic-concurrency checks
13
+ * survive a round-trip through the database.
14
+ */
15
+ interface EntityState<TIdentifier> {
16
+ readonly id: TIdentifier;
17
+ readonly createdAt: Date;
18
+ readonly updatedAt: Date;
19
+ readonly aggregateVersion: number;
20
+ }
21
+ /**
22
+ * Base class for domain entities — objects defined by a stable identity rather
23
+ * than by their attributes. Two entities are "the same" when their `id`
24
+ * matches, even if every other field differs; this is the opposite of a
25
+ * {@link ValueObject}, which is defined entirely by its contents.
26
+ *
27
+ * The class owns the bookkeeping common to every aggregate root: a creation
28
+ * timestamp that never changes, a last-modified timestamp, and an
29
+ * `aggregateVersion` counter used for optimistic concurrency control. All three
30
+ * are validated on construction and the dates are defensively cloned, so an
31
+ * entity can never be built into — or leak — an inconsistent temporal state.
32
+ *
33
+ * @typeParam TIdentifier - The identity type (e.g. a branded string id or a VO).
34
+ */
35
+ declare abstract class Entity<TIdentifier> {
36
+ static readonly CONTRACT_VERSION: ContractVersion;
37
+ private readonly _id;
38
+ private readonly _createdAt;
39
+ private _updatedAt;
40
+ private _aggregateVersion;
41
+ /**
42
+ * Reconstitutes an entity from its persisted {@link EntityState}.
43
+ *
44
+ * Validates the temporal invariants up front so an invalid entity is
45
+ * impossible to construct: both dates must be valid, the aggregate version
46
+ * must be a non-negative integer, and `updatedAt` may not predate
47
+ * `createdAt`. Both dates are cloned on the way in so a later mutation of
48
+ * the caller's `Date` objects cannot reach into the entity's internal state.
49
+ *
50
+ * Declared `protected` because entities are reconstituted through a
51
+ * subclass factory, never instantiated directly by application code.
52
+ *
53
+ * @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.
54
+ */
55
+ protected constructor(state: EntityState<TIdentifier>);
56
+ /** The entity's stable identity — the basis for equality between entities. */
57
+ get id(): TIdentifier;
58
+ /**
59
+ * When the entity was first created.
60
+ *
61
+ * Returns a clone so callers cannot mutate the entity's internal `Date`;
62
+ * the value is fixed at construction and never changes thereafter.
63
+ */
64
+ get createdAt(): Date;
65
+ /**
66
+ * When the entity was last modified.
67
+ *
68
+ * Returns a clone for the same encapsulation reason as {@link createdAt};
69
+ * the underlying value advances only through {@link markAsModified}.
70
+ */
71
+ get updatedAt(): Date;
72
+ /**
73
+ * Monotonic counter incremented on every mutation, used for optimistic
74
+ * concurrency control: a writer reads this value, and the persistence layer
75
+ * rejects the write if the stored version has moved on in the meantime.
76
+ */
77
+ get aggregateVersion(): number;
78
+ /**
79
+ * The schema/contract version stamped on this entity type by the
80
+ * `@version` decorator — used to detect and migrate state persisted under
81
+ * an older shape.
82
+ */
83
+ get contractVersion(): ContractVersion;
84
+ /**
85
+ * The single seam through which an entity records a mutation: it advances
86
+ * `updatedAt` and bumps {@link aggregateVersion} by one. Subclasses must
87
+ * call this from every state-changing method so the version counter stays
88
+ * an accurate optimistic-lock token.
89
+ *
90
+ * @param updatedAt - The modification instant; defaults to now. Validated
91
+ * and required not to predate `createdAt`, preserving the same invariant
92
+ * the constructor enforces.
93
+ * @returns The new aggregate version after the increment.
94
+ * @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.
95
+ */
96
+ protected markAsModified(updatedAt?: Date): number;
97
+ }
98
+ //#endregion
99
+ //#region src/core/domain/value-object.d.ts
100
+ /**
101
+ * Extension point for value-object equality. A plugin implementing this
102
+ * contract overrides how any two value objects are compared, letting the host
103
+ * application swap in a structural comparator (e.g. `lodash.isEqual` over the
104
+ * wrapped `value`) without every value object having to implement `equals` by
105
+ * hand.
106
+ */
107
+ type ValueObjectPluginContract = {
108
+ equals: (a: ValueObject<unknown, unknown>, b: ValueObject<unknown, unknown>) => boolean;
109
+ };
110
+ /**
111
+ * Base class for value objects — domain concepts defined entirely by their
112
+ * contents, with no identity of their own. Two value objects are
113
+ * interchangeable when they hold equal data, which is the opposite of an
114
+ * {@link Entity} (compared by id). Money, a CPF, a date range: replacing one
115
+ * instance with an equal one changes nothing about the model.
116
+ *
117
+ * Immutability is the defining guarantee here. The wrapped `value` is
118
+ * deep-frozen on construction, so a value object can be shared freely without
119
+ * any risk of a consumer mutating shared state. Subclasses seal the instance
120
+ * itself with {@link finalize} once their own fields are set.
121
+ *
122
+ * @typeParam T - The shape of the wrapped data.
123
+ * @typeParam P - The primitive form produced by {@link toPrimitive} / {@link toJSON}.
124
+ */
125
+ declare abstract class ValueObject<T, P> {
126
+ static readonly CONTRACT_VERSION: ContractVersion;
127
+ /**
128
+ * Registry of equality plugins shared by every value object. Empty by
129
+ * default — when nothing is registered, {@link equals} falls back to a
130
+ * structural comparison of the wrapped `value`. Hosts register a plugin
131
+ * (e.g. `lodash.isEqual`) once at startup to customise equality globally.
132
+ */
133
+ static readonly plugins: PluginManager<ValueObjectPluginContract>;
134
+ /** The wrapped data, deep-frozen so it can never be mutated after construction. */
135
+ readonly value: DeepReadonly<T>;
136
+ /**
137
+ * Wraps `value`, deep-freezing it so the value object is immutable from the
138
+ * moment it exists. Declared `protected` because value objects are built
139
+ * through a validating subclass factory, never instantiated directly.
140
+ */
141
+ protected constructor(value: T);
142
+ /**
143
+ * The schema/contract version stamped on this value-object type by the
144
+ * `@version` decorator — used to detect state persisted under an older shape.
145
+ */
146
+ get contractVersion(): ContractVersion;
147
+ /**
148
+ * Freezes the instance shell, blocking reassignment of any own field.
149
+ *
150
+ * `value` is already deep-frozen by the constructor, so the wrapped data is
151
+ * immutable regardless. The instance itself is NOT frozen automatically
152
+ * because a subclass may still need to assign its own fields after
153
+ * `super(value)` runs. Call `finalize()` at the very end of the subclass
154
+ * constructor (after all fields are set) to make the whole value object
155
+ * immutable.
156
+ */
157
+ protected finalize(): void;
158
+ /**
159
+ * Hook for `JSON.stringify`. Delegates to {@link toPrimitive} so a value
160
+ * object serializes to its primitive form rather than exposing the internal
161
+ * `value` wrapper.
162
+ */
163
+ toJSON(): P;
164
+ /**
165
+ * Compares this value object with another by content. Because value objects
166
+ * carry no identity, two independently constructed instances holding the
167
+ * same data are considered equal.
168
+ *
169
+ * Delegates to the registered equality {@link plugins}; with no plugin
170
+ * registered it falls back to comparing the serialized wrapped `value`.
171
+ * Subclasses may still override for a faster or domain-specific comparison.
172
+ */
173
+ equals(other: this): boolean;
174
+ /**
175
+ * Projects the value object down to a plain, serializable primitive form —
176
+ * the representation suitable for persistence, transport, or comparison.
177
+ */
178
+ abstract toPrimitive(): P;
179
+ }
180
+ //#endregion
181
+ //#region src/core/domain/uuid-identifier.d.ts
182
+ /**
183
+ * Base class for UUID-backed identity value objects.
184
+ *
185
+ * A domain typically has many of these — `OrderId`, `CustomerId`, `InvoiceId` —
186
+ * all wrapping the same `string` shape. Two problems follow: the UUID format
187
+ * check gets copy-pasted into every one, and because the wrapped shape is
188
+ * identical, TypeScript's structural typing would happily accept an `OrderId`
189
+ * where a `CustomerId` is expected. `UuidIdentifier` solves both: the format
190
+ * lives here once ({@link isValid}), and the `TBrand` phantom tag makes each
191
+ * subtype nominally distinct so the ids cannot be swapped for one another.
192
+ *
193
+ * It deliberately does not impose a factory. Identifiers vary in how they
194
+ * report an invalid value (their own exception type, their own message), so a
195
+ * concrete id adds a validating `create` that calls {@link isValid} plus a
196
+ * `reconstitute` that trusts an already-persisted value:
197
+ *
198
+ * ```ts
199
+ * class OrderId extends UuidIdentifier<"OrderId"> {
200
+ * private constructor(value: string) { super(value); }
201
+ * static create(raw: string): OrderId {
202
+ * if (!UuidIdentifier.isValid(raw)) throw new InvalidOrderIdError(raw);
203
+ * return new OrderId(raw);
204
+ * }
205
+ * static reconstitute(value: string): OrderId { return new OrderId(value); }
206
+ * }
207
+ * ```
208
+ *
209
+ * @typeParam TBrand - A unique string literal that nominally tags the subtype.
210
+ */
211
+ declare abstract class UuidIdentifier<TBrand extends string = string> extends ValueObject<string, string> {
212
+ /**
213
+ * Phantom brand. Never assigned at runtime (`declare`), it exists only so
214
+ * two identifiers with different brands are not interchangeable at the type
215
+ * level despite sharing the same `string` value.
216
+ */
217
+ protected readonly __brand: TBrand;
218
+ protected constructor(value: string);
219
+ toPrimitive(): string;
220
+ /** The wrapped UUID string — convenient for logging and interpolation. */
221
+ toString(): string;
222
+ /**
223
+ * Whether `candidate` is a canonical UUID (versions 1–5). The single source
224
+ * of truth for the format; concrete identifiers call this from `create`.
225
+ */
226
+ static isValid(candidate: string): boolean;
227
+ }
228
+ //#endregion
229
+ export { EntityState as a, Entity as i, ValueObject as n, ValueObjectPluginContract as r, UuidIdentifier as t };
230
+ //# sourceMappingURL=uuid-identifier.d.ts.map
@@ -0,0 +1,60 @@
1
+ import { t as ValueObject } from "./value-object.js";
2
+ //#region src/core/domain/uuid-identifier.ts
3
+ /**
4
+ * Canonical RFC-4122 UUID (versions 1–5), matched case-insensitively. Declared
5
+ * once here so the dozens of typed identifiers a domain accumulates never have
6
+ * to re-state the pattern.
7
+ */
8
+ const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
9
+ /**
10
+ * Base class for UUID-backed identity value objects.
11
+ *
12
+ * A domain typically has many of these — `OrderId`, `CustomerId`, `InvoiceId` —
13
+ * all wrapping the same `string` shape. Two problems follow: the UUID format
14
+ * check gets copy-pasted into every one, and because the wrapped shape is
15
+ * identical, TypeScript's structural typing would happily accept an `OrderId`
16
+ * where a `CustomerId` is expected. `UuidIdentifier` solves both: the format
17
+ * lives here once ({@link isValid}), and the `TBrand` phantom tag makes each
18
+ * subtype nominally distinct so the ids cannot be swapped for one another.
19
+ *
20
+ * It deliberately does not impose a factory. Identifiers vary in how they
21
+ * report an invalid value (their own exception type, their own message), so a
22
+ * concrete id adds a validating `create` that calls {@link isValid} plus a
23
+ * `reconstitute` that trusts an already-persisted value:
24
+ *
25
+ * ```ts
26
+ * class OrderId extends UuidIdentifier<"OrderId"> {
27
+ * private constructor(value: string) { super(value); }
28
+ * static create(raw: string): OrderId {
29
+ * if (!UuidIdentifier.isValid(raw)) throw new InvalidOrderIdError(raw);
30
+ * return new OrderId(raw);
31
+ * }
32
+ * static reconstitute(value: string): OrderId { return new OrderId(value); }
33
+ * }
34
+ * ```
35
+ *
36
+ * @typeParam TBrand - A unique string literal that nominally tags the subtype.
37
+ */
38
+ var UuidIdentifier = class extends ValueObject {
39
+ constructor(value) {
40
+ super(value);
41
+ }
42
+ toPrimitive() {
43
+ return this.value;
44
+ }
45
+ /** The wrapped UUID string — convenient for logging and interpolation. */
46
+ toString() {
47
+ return this.value;
48
+ }
49
+ /**
50
+ * Whether `candidate` is a canonical UUID (versions 1–5). The single source
51
+ * of truth for the format; concrete identifiers call this from `create`.
52
+ */
53
+ static isValid(candidate) {
54
+ return UUID_PATTERN.test(candidate);
55
+ }
56
+ };
57
+ //#endregion
58
+ export { UuidIdentifier as t };
59
+
60
+ //# sourceMappingURL=uuid-identifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid-identifier.js","names":[],"sources":["../src/core/domain/uuid-identifier.ts"],"sourcesContent":["import { ValueObject } from \"./value-object.js\";\n\n/**\n * Canonical RFC-4122 UUID (versions 1–5), matched case-insensitively. Declared\n * once here so the dozens of typed identifiers a domain accumulates never have\n * to re-state the pattern.\n */\nconst UUID_PATTERN =\n /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\n\n/**\n * Base class for UUID-backed identity value objects.\n *\n * A domain typically has many of these — `OrderId`, `CustomerId`, `InvoiceId` —\n * all wrapping the same `string` shape. Two problems follow: the UUID format\n * check gets copy-pasted into every one, and because the wrapped shape is\n * identical, TypeScript's structural typing would happily accept an `OrderId`\n * where a `CustomerId` is expected. `UuidIdentifier` solves both: the format\n * lives here once ({@link isValid}), and the `TBrand` phantom tag makes each\n * subtype nominally distinct so the ids cannot be swapped for one another.\n *\n * It deliberately does not impose a factory. Identifiers vary in how they\n * report an invalid value (their own exception type, their own message), so a\n * concrete id adds a validating `create` that calls {@link isValid} plus a\n * `reconstitute` that trusts an already-persisted value:\n *\n * ```ts\n * class OrderId extends UuidIdentifier<\"OrderId\"> {\n * private constructor(value: string) { super(value); }\n * static create(raw: string): OrderId {\n * if (!UuidIdentifier.isValid(raw)) throw new InvalidOrderIdError(raw);\n * return new OrderId(raw);\n * }\n * static reconstitute(value: string): OrderId { return new OrderId(value); }\n * }\n * ```\n *\n * @typeParam TBrand - A unique string literal that nominally tags the subtype.\n */\nabstract class UuidIdentifier<\n TBrand extends string = string,\n> extends ValueObject<string, string> {\n /**\n * Phantom brand. Never assigned at runtime (`declare`), it exists only so\n * two identifiers with different brands are not interchangeable at the type\n * level despite sharing the same `string` value.\n */\n protected declare readonly __brand: TBrand;\n\n protected constructor(value: string) {\n super(value);\n }\n\n public toPrimitive(): string {\n return this.value;\n }\n\n /** The wrapped UUID string — convenient for logging and interpolation. */\n public toString(): string {\n return this.value;\n }\n\n /**\n * Whether `candidate` is a canonical UUID (versions 1–5). The single source\n * of truth for the format; concrete identifiers call this from `create`.\n */\n public static isValid(candidate: string): boolean {\n return UUID_PATTERN.test(candidate);\n }\n}\n\nexport { UuidIdentifier };\n"],"mappings":";;;;;;;AAOA,MAAM,eACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BJ,IAAe,iBAAf,cAEU,YAA4B;CAQlC,YAAsB,OAAe;EACjC,MAAM,KAAK;CACf;CAEA,cAA6B;EACzB,OAAO,KAAK;CAChB;;CAGA,WAA0B;EACtB,OAAO,KAAK;CAChB;;;;;CAMA,OAAc,QAAQ,WAA4B;EAC9C,OAAO,aAAa,KAAK,SAAS;CACtC;AACJ"}
@@ -0,0 +1,60 @@
1
+ //#region src/core/exceptions/validation-code.ts
2
+ var ValidationCode = class ValidationCode {
3
+ constructor(value) {
4
+ this.value = value;
5
+ }
6
+ static of(value) {
7
+ return new ValidationCode(value);
8
+ }
9
+ static {
10
+ this.BLANK = new ValidationCode("blank");
11
+ }
12
+ static {
13
+ this.REQUIRED = new ValidationCode("required");
14
+ }
15
+ static {
16
+ this.INVALID_FORMAT = new ValidationCode("invalid_format");
17
+ }
18
+ static {
19
+ this.INVALID_CHARACTERS = new ValidationCode("invalid_characters");
20
+ }
21
+ static {
22
+ this.INVALID_LENGTH = new ValidationCode("invalid_length");
23
+ }
24
+ static {
25
+ this.TOO_SHORT = new ValidationCode("too_short");
26
+ }
27
+ static {
28
+ this.TOO_LONG = new ValidationCode("too_long");
29
+ }
30
+ static {
31
+ this.OUT_OF_RANGE = new ValidationCode("out_of_range");
32
+ }
33
+ static {
34
+ this.INVALID_CHECKSUM = new ValidationCode("invalid_checksum");
35
+ }
36
+ static {
37
+ this.INVALID_CHECKING_DIGIT = new ValidationCode("invalid_checking_digit");
38
+ }
39
+ static {
40
+ this.ALREADY_EXISTS = new ValidationCode("already_exists");
41
+ }
42
+ static {
43
+ this.NOT_FOUND = new ValidationCode("not_found");
44
+ }
45
+ static {
46
+ this.NOT_ALLOWED = new ValidationCode("not_allowed");
47
+ }
48
+ static {
49
+ this.UNEXPECTED = new ValidationCode("unexpected");
50
+ }
51
+ };
52
+ //#endregion
53
+ Object.defineProperty(exports, "ValidationCode", {
54
+ enumerable: true,
55
+ get: function() {
56
+ return ValidationCode;
57
+ }
58
+ });
59
+
60
+ //# sourceMappingURL=validation-code.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation-code.cjs","names":[],"sources":["../src/core/exceptions/validation-code.ts"],"sourcesContent":["// Object-oriented representation of validation codes (similar to Kotlin inline class).\n// Provides common reusable codes while allowing custom codes via `of`.\n\nclass ValidationCode {\n public readonly value: string;\n\n private constructor(value: string) {\n this.value = value;\n }\n\n // Factory for custom codes\n public static of(value: string): ValidationCode {\n return new ValidationCode(value);\n }\n\n // Common, domain-friendly codes\n public static readonly BLANK = new ValidationCode(\"blank\");\n public static readonly REQUIRED = new ValidationCode(\"required\");\n public static readonly INVALID_FORMAT = new ValidationCode(\n \"invalid_format\",\n );\n public static readonly INVALID_CHARACTERS = new ValidationCode(\n \"invalid_characters\",\n );\n public static readonly INVALID_LENGTH = new ValidationCode(\n \"invalid_length\",\n );\n public static readonly TOO_SHORT = new ValidationCode(\"too_short\");\n public static readonly TOO_LONG = new ValidationCode(\"too_long\");\n public static readonly OUT_OF_RANGE = new ValidationCode(\"out_of_range\");\n public static readonly INVALID_CHECKSUM = new ValidationCode(\n \"invalid_checksum\",\n );\n public static readonly INVALID_CHECKING_DIGIT = new ValidationCode(\n \"invalid_checking_digit\",\n );\n public static readonly ALREADY_EXISTS = new ValidationCode(\n \"already_exists\",\n );\n public static readonly NOT_FOUND = new ValidationCode(\"not_found\");\n public static readonly NOT_ALLOWED = new ValidationCode(\"not_allowed\");\n public static readonly UNEXPECTED = new ValidationCode(\"unexpected\");\n}\n\nexport { ValidationCode };\n"],"mappings":";AAGA,IAAM,iBAAN,MAAM,eAAe;CAGjB,YAAoB,OAAe;EAC/B,KAAK,QAAQ;CACjB;CAGA,OAAc,GAAG,OAA+B;EAC5C,OAAO,IAAI,eAAe,KAAK;CACnC;;eAG+B,IAAI,eAAe,OAAO;;;kBACvB,IAAI,eAAe,UAAU;;;wBACvB,IAAI,eACxC,gBACJ;;;4BAC4C,IAAI,eAC5C,oBACJ;;;wBACwC,IAAI,eACxC,gBACJ;;;mBACmC,IAAI,eAAe,WAAW;;;kBAC/B,IAAI,eAAe,UAAU;;;sBACzB,IAAI,eAAe,cAAc;;;0BAC7B,IAAI,eAC1C,kBACJ;;;gCACgD,IAAI,eAChD,wBACJ;;;wBACwC,IAAI,eACxC,gBACJ;;;mBACmC,IAAI,eAAe,WAAW;;;qBAC5B,IAAI,eAAe,aAAa;;;oBACjC,IAAI,eAAe,YAAY;;AACvE"}
@@ -0,0 +1,23 @@
1
+ //#region src/core/exceptions/validation-code.d.ts
2
+ declare class ValidationCode {
3
+ readonly value: string;
4
+ private constructor();
5
+ static of(value: string): ValidationCode;
6
+ static readonly BLANK: ValidationCode;
7
+ static readonly REQUIRED: ValidationCode;
8
+ static readonly INVALID_FORMAT: ValidationCode;
9
+ static readonly INVALID_CHARACTERS: ValidationCode;
10
+ static readonly INVALID_LENGTH: ValidationCode;
11
+ static readonly TOO_SHORT: ValidationCode;
12
+ static readonly TOO_LONG: ValidationCode;
13
+ static readonly OUT_OF_RANGE: ValidationCode;
14
+ static readonly INVALID_CHECKSUM: ValidationCode;
15
+ static readonly INVALID_CHECKING_DIGIT: ValidationCode;
16
+ static readonly ALREADY_EXISTS: ValidationCode;
17
+ static readonly NOT_FOUND: ValidationCode;
18
+ static readonly NOT_ALLOWED: ValidationCode;
19
+ static readonly UNEXPECTED: ValidationCode;
20
+ }
21
+ //#endregion
22
+ export { ValidationCode as t };
23
+ //# sourceMappingURL=validation-code.d.cts.map
@@ -0,0 +1,23 @@
1
+ //#region src/core/exceptions/validation-code.d.ts
2
+ declare class ValidationCode {
3
+ readonly value: string;
4
+ private constructor();
5
+ static of(value: string): ValidationCode;
6
+ static readonly BLANK: ValidationCode;
7
+ static readonly REQUIRED: ValidationCode;
8
+ static readonly INVALID_FORMAT: ValidationCode;
9
+ static readonly INVALID_CHARACTERS: ValidationCode;
10
+ static readonly INVALID_LENGTH: ValidationCode;
11
+ static readonly TOO_SHORT: ValidationCode;
12
+ static readonly TOO_LONG: ValidationCode;
13
+ static readonly OUT_OF_RANGE: ValidationCode;
14
+ static readonly INVALID_CHECKSUM: ValidationCode;
15
+ static readonly INVALID_CHECKING_DIGIT: ValidationCode;
16
+ static readonly ALREADY_EXISTS: ValidationCode;
17
+ static readonly NOT_FOUND: ValidationCode;
18
+ static readonly NOT_ALLOWED: ValidationCode;
19
+ static readonly UNEXPECTED: ValidationCode;
20
+ }
21
+ //#endregion
22
+ export { ValidationCode as t };
23
+ //# sourceMappingURL=validation-code.d.ts.map