archstone 1.2.0 → 1.2.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.md CHANGED
@@ -211,11 +211,14 @@ export interface AuditRepository extends Creatable<AuditLog> {}
211
211
 
212
212
  | Import | Contents |
213
213
  |---|---|
214
+ | `archstone` | Everything |
214
215
  | `archstone/core` | `Either`, `ValueObject`, `UniqueEntityId`, `WatchedList`, `Optional` |
215
216
  | `archstone/domain` | All domain exports |
216
217
  | `archstone/domain/enterprise` | `Entity`, `AggregateRoot`, `DomainEvent`, `DomainEvents`, `EventHandler` |
217
218
  | `archstone/domain/application` | `UseCase`, `UseCaseError`, repository contracts |
218
219
 
220
+ All sub-paths share type declarations via a common chunk — mixing imports from multiple sub-paths is fully type-safe with no duplicate declaration conflicts.
221
+
219
222
  ---
220
223
 
221
224
  ## Architecture
@@ -1,270 +1,2 @@
1
- /**
2
- * Represents the left side of an {@link Either} — conventionally used for
3
- * failure or error values.
4
- *
5
- * @template L - The type of the failure value
6
- * @template R - The type of the success value
7
- */
8
- declare class Left<
9
- L,
10
- R
11
- > {
12
- readonly value: L;
13
- constructor(value: L);
14
- isRight(): this is Right<L, R>;
15
- isLeft(): this is Left<L, R>;
16
- }
17
- /**
18
- * Represents the right side of an {@link Either} — conventionally used for
19
- * success values.
20
- *
21
- * @template L - The type of the failure value
22
- * @template R - The type of the success value
23
- */
24
- declare class Right<
25
- L,
26
- R
27
- > {
28
- readonly value: R;
29
- constructor(value: R);
30
- isRight(): this is Right<L, R>;
31
- isLeft(): this is Left<L, R>;
32
- }
33
- /**
34
- * A discriminated union that represents a value of one of two possible types.
35
- *
36
- * Commonly used as a type-safe alternative to throwing exceptions — the left
37
- * side carries an error, and the right side carries a success value.
38
- *
39
- * Use the {@link left} and {@link right} helper functions to construct values,
40
- * and `isLeft()` / `isRight()` to narrow the type.
41
- *
42
- * @template L - The type of the failure value
43
- * @template R - The type of the success value
44
- *
45
- * @example
46
- * ```ts
47
- * type FindUserResult = Either<NotFoundError, User>
48
- *
49
- * function findUser(id: string): FindUserResult {
50
- * const user = db.find(id)
51
- * if (!user) return left(new NotFoundError("User", id))
52
- * return right(user)
53
- * }
54
- *
55
- * const result = findUser("123")
56
- *
57
- * if (result.isLeft()) {
58
- * console.error(result.value) // NotFoundError
59
- * } else {
60
- * console.log(result.value) // User
61
- * }
62
- * ```
63
- */
64
- type Either<
65
- L,
66
- R
67
- > = Left<L, R> | Right<L, R>;
68
- /**
69
- * Constructs a {@link Left} value, representing a failure.
70
- *
71
- * @param value - The error or failure value
72
- */
73
- declare const left: <
74
- L,
75
- R
76
- >(value: L) => Either<L, R>;
77
- /**
78
- * Constructs a {@link Right} value, representing a success.
79
- *
80
- * @param value - The success value
81
- */
82
- declare const right: <
83
- L,
84
- R
85
- >(value: R) => Either<L, R>;
86
- /**
87
- * Make some property optional on type
88
- *
89
- * @example
90
- * ```typescript
91
- * type Post {
92
- * id: string;
93
- * name: string;
94
- * email: string;
95
- * }
96
- *
97
- * Optional<Post, 'id' | 'email'>
98
- * ```
99
- */
100
- type Optional<
101
- T,
102
- K extends keyof T
103
- > = Pick<Partial<T>, K> & Omit<T, K>;
104
- /**
105
- * Represents a unique identifier for a domain entity.
106
- *
107
- * Wraps a UUID v7 string, which is time-sortable and ideal for database
108
- * indexing. A new identifier is generated automatically if no value is provided.
109
- *
110
- * @example
111
- * ```ts
112
- * // auto-generated
113
- * const id = new UniqueEntityId()
114
- *
115
- * // from existing value (e.g. reconstructing from database)
116
- * const id = new UniqueEntityId("0195d810-5b3e-7000-8e3e-1a2b3c4d5e6f")
117
- * ```
118
- */
119
- declare class UniqueEntityId {
120
- private readonly value;
121
- constructor(value?: string);
122
- /**
123
- * Returns the identifier as a primitive string.
124
- * Useful for serialization and database persistence.
125
- */
126
- toValue(): string;
127
- /**
128
- * Returns the string representation of the identifier.
129
- */
130
- toString(): string;
131
- /**
132
- * Compares this identifier with another by value equality.
133
- *
134
- * @param id - The identifier to compare against
135
- * @returns `true` if both identifiers have the same value
136
- */
137
- equals(id: UniqueEntityId): boolean;
138
- }
139
- /**
140
- * Base class for all value objects in the domain.
141
- *
142
- * A value object is an object defined entirely by its attributes — it has no
143
- * identity. Two value objects are considered equal if all their properties
144
- * are deeply equal, regardless of reference.
145
- *
146
- * Value objects should be immutable. Avoid mutating `props` directly;
147
- * instead, create a new instance with the updated values.
148
- *
149
- * All value objects must implement a static `create` factory method to
150
- * encapsulate construction and validation logic, keeping the constructor
151
- * protected from external instantiation.
152
- *
153
- * @template Props - The shape of the value object's properties
154
- *
155
- * @example
156
- * ```ts
157
- * interface EmailProps {
158
- * value: string
159
- * }
160
- *
161
- * class Email extends ValueObject<EmailProps> {
162
- * get value() { return this.props.value }
163
- *
164
- * static create(email: string): Email {
165
- * if (!email.includes("@")) {
166
- * throw new ValidationError("Invalid email address.")
167
- * }
168
- *
169
- * return new Email({ value: email })
170
- * }
171
- * }
172
- * ```
173
- */
174
- declare abstract class ValueObject<Props> {
175
- protected props: Props;
176
- protected constructor(props: Props);
177
- /**
178
- * Compares this value object with another by deep equality of their properties.
179
- *
180
- * @param other - The value object to compare against
181
- * @returns `true` if both value objects have deeply equal properties
182
- */
183
- equals(other: ValueObject<Props>): boolean;
184
- }
185
- /**
186
- * Tracks additions and removals of items in a collection, enabling
187
- * efficient persistence of only what changed.
188
- *
189
- * Commonly used inside aggregate roots to manage one-to-many relationships
190
- * without rewriting the entire collection on every save — only new and
191
- * removed items are persisted.
192
- *
193
- * Subclasses must implement {@link compareItems} to define equality between items.
194
- *
195
- * @template T - The type of items in the list
196
- *
197
- * @example
198
- * ```ts
199
- * class TagList extends WatchedList<Tag> {
200
- * compareItems(a: Tag, b: Tag): boolean {
201
- * return a.id.equals(b.id)
202
- * }
203
- *
204
- * static create(tags: Tag[]): TagList {
205
- * return new TagList(tags)
206
- * }
207
- * }
208
- *
209
- * const tags = TagList.create([existingTag])
210
- *
211
- * tags.add(newTag) // tracked as new
212
- * tags.remove(oldTag) // tracked as removed
213
- *
214
- * tags.getNewItems() // [newTag]
215
- * tags.getRemovedItems() // [oldTag]
216
- * ```
217
- */
218
- declare abstract class WatchedList<T> {
219
- protected currentItems: T[];
220
- protected initial: T[];
221
- protected new: T[];
222
- protected removed: T[];
223
- constructor(initialItems?: T[]);
224
- /**
225
- * Returns all current items in the list.
226
- */
227
- getItems(): T[];
228
- /**
229
- * Returns items that were added since the list was created.
230
- */
231
- getNewItems(): T[];
232
- /**
233
- * Returns items that were removed since the list was created.
234
- */
235
- getRemovedItems(): T[];
236
- /**
237
- * Returns whether the given item exists in the current list.
238
- *
239
- * @param item - The item to check
240
- */
241
- exists(item: T): boolean;
242
- /**
243
- * Adds an item to the list, tracking it as new if it wasn't in the initial set.
244
- * If the item was previously removed, it is restored.
245
- *
246
- * @param item - The item to add
247
- */
248
- add(item: T): void;
249
- /**
250
- * Removes an item from the list, tracking it as removed if it was in the initial set.
251
- *
252
- * @param item - The item to remove
253
- */
254
- remove(item: T): void;
255
- /**
256
- * Replaces the entire list with a new set of items, automatically
257
- * computing what was added and what was removed.
258
- *
259
- * @param items - The new set of items
260
- */
261
- update(items: T[]): void;
262
- private isCurrentItem;
263
- private isNewItem;
264
- private isRemovedItem;
265
- private removeFromNew;
266
- private removeFromCurrent;
267
- private removeFromRemoved;
268
- private wasAddedInitially;
269
- }
1
+ import { Either, Optional, UniqueEntityId, ValueObject, WatchedList, left, right } from "../shared/chunk-t21213sm.js";
270
2
  export { right, left, WatchedList, ValueObject, UniqueEntityId, Optional, Either };
@@ -1,2 +1,2 @@
1
1
  // @bun
2
- import{a,b,c as d,d as e}from"../shared/chunk-kybhb7j0.js";import{h as c}from"../shared/chunk-27pwj1j1.js";export{b as right,a as left,e as WatchedList,d as ValueObject,c as UniqueEntityId};
2
+ import{d as a,e as b,f as c,g as d,h as e}from"../shared/chunk-r63mxet1.js";export{b as right,a as left,e as WatchedList,d as ValueObject,c as UniqueEntityId};
@@ -1,204 +1,3 @@
1
- /**
2
- * Contract for entities that can be created.
3
- *
4
- * @template T - The entity type
5
- */
6
- interface Creatable<T> {
7
- /**
8
- * Persists a new entity for the first time.
9
- *
10
- * @param entity - The entity to create
11
- */
12
- create(entity: T): Promise<void>;
13
- }
14
- /**
15
- * Contract for entities that can be deleted.
16
- *
17
- * @template T - The entity type
18
- */
19
- interface Deletable<T> {
20
- /**
21
- * Deletes an entity from the repository.
22
- *
23
- * @param entity - The entity to delete
24
- */
25
- delete(entity: T): Promise<void>;
26
- }
27
- /**
28
- * Contract for entities that can be retrieved by their unique identifier.
29
- *
30
- * @template T - The entity type
31
- */
32
- interface Findable<T> {
33
- /**
34
- * Finds an entity by its unique identifier.
35
- *
36
- * @param id - The unique identifier of the entity
37
- * @returns The entity if found, `null` otherwise
38
- */
39
- findById(id: string): Promise<T | null>;
40
- }
41
- /**
42
- * Contract for entities that can be persisted.
43
- *
44
- * @template T - The entity type
45
- */
46
- interface Saveable<T> {
47
- /**
48
- * Persists an existing entity.
49
- *
50
- * @param entity - The entity to save
51
- */
52
- save(entity: T): Promise<void>;
53
- }
54
- /**
55
- * Full CRUD repository contract, composing all segregated interfaces.
56
- *
57
- * Use this when the repository needs to support all operations. For more
58
- * constrained repositories, prefer composing only the interfaces you need.
59
- *
60
- * @template T - The entity type
61
- *
62
- * @example
63
- * ```ts
64
- * // full CRUD
65
- * interface UserRepository extends Repository<User> {
66
- * findByEmail(email: string): Promise<User | null>
67
- * }
68
- *
69
- * // read-only
70
- * interface ReportRepository extends Findable<Report> {
71
- * findByPeriod(start: Date, end: Date): Promise<Report[]>
72
- * }
73
- *
74
- * // append-only
75
- * interface AuditRepository extends Creatable<AuditLog> {}
76
- * ```
77
- */
78
- interface Repository<T> extends Findable<T>, Saveable<T>, Creatable<T>, Deletable<T> {}
79
- /**
80
- * Base contract for all use case errors.
81
- *
82
- * Implement this interface to define semantic, domain-aware errors
83
- * that can be returned as the left side of an {@link Either}.
84
- *
85
- * @example
86
- * ```ts
87
- * class UserNotFoundError implements UseCaseError {
88
- * message = "User not found."
89
- * }
90
- *
91
- * type FindUserResult = Either<UserNotFoundError, User>
92
- * ```
93
- */
94
- interface UseCaseError {
95
- message: string;
96
- }
97
- /**
98
- * Represents the left side of an {@link Either} — conventionally used for
99
- * failure or error values.
100
- *
101
- * @template L - The type of the failure value
102
- * @template R - The type of the success value
103
- */
104
- declare class Left<
105
- L,
106
- R
107
- > {
108
- readonly value: L;
109
- constructor(value: L);
110
- isRight(): this is Right<L, R>;
111
- isLeft(): this is Left<L, R>;
112
- }
113
- /**
114
- * Represents the right side of an {@link Either} — conventionally used for
115
- * success values.
116
- *
117
- * @template L - The type of the failure value
118
- * @template R - The type of the success value
119
- */
120
- declare class Right<
121
- L,
122
- R
123
- > {
124
- readonly value: R;
125
- constructor(value: R);
126
- isRight(): this is Right<L, R>;
127
- isLeft(): this is Left<L, R>;
128
- }
129
- /**
130
- * A discriminated union that represents a value of one of two possible types.
131
- *
132
- * Commonly used as a type-safe alternative to throwing exceptions — the left
133
- * side carries an error, and the right side carries a success value.
134
- *
135
- * Use the {@link left} and {@link right} helper functions to construct values,
136
- * and `isLeft()` / `isRight()` to narrow the type.
137
- *
138
- * @template L - The type of the failure value
139
- * @template R - The type of the success value
140
- *
141
- * @example
142
- * ```ts
143
- * type FindUserResult = Either<NotFoundError, User>
144
- *
145
- * function findUser(id: string): FindUserResult {
146
- * const user = db.find(id)
147
- * if (!user) return left(new NotFoundError("User", id))
148
- * return right(user)
149
- * }
150
- *
151
- * const result = findUser("123")
152
- *
153
- * if (result.isLeft()) {
154
- * console.error(result.value) // NotFoundError
155
- * } else {
156
- * console.log(result.value) // User
157
- * }
158
- * ```
159
- */
160
- type Either<
161
- L,
162
- R
163
- > = Left<L, R> | Right<L, R>;
164
- /**
165
- * Represents the expected output shape of any use case.
166
- * Always an {@link Either} — left for errors, right for success.
167
- */
168
- type UseCaseOutput = Either<UseCaseError | never, unknown | null>;
169
- /**
170
- * Base contract for all application use cases.
171
- *
172
- * A use case orchestrates domain logic for a single application operation.
173
- * It receives a typed input, interacts with the domain, and returns an
174
- * {@link Either} — never throwing exceptions directly.
175
- *
176
- * The left side carries a {@link UseCaseError} on failure.
177
- * The right side carries the success value.
178
- *
179
- * @template Input - The shape of the data required to execute the use case
180
- * @template Output - The expected {@link Either} output, constrained to {@link UseCaseOutput}
181
- *
182
- * @example
183
- * ```ts
184
- * type Input = { userId: string }
185
- * type Output = Either<UserNotFoundError, User>
186
- *
187
- * class GetUserUseCase implements UseCase<Input, Output> {
188
- * constructor(private userRepository: UserRepository) {}
189
- *
190
- * async execute({ userId }: Input): Promise<Output> {
191
- * const user = await this.userRepository.findById(userId)
192
- * if (!user) return left(new UserNotFoundError(userId))
193
- * return right(user)
194
- * }
195
- * }
196
- * ```
197
- */
198
- interface UseCase<
199
- Input,
200
- Output extends UseCaseOutput
201
- > {
202
- execute(input: Input): Promise<Output>;
203
- }
1
+ import { Creatable, Deletable, Findable, Repository, Saveable, UseCase, UseCaseError } from "../../shared/chunk-txbqwwkc.js";
2
+ import "../../shared/chunk-t21213sm.js";
204
3
  export { UseCaseError, UseCase, Saveable, Repository, Findable, Deletable, Creatable };