@rineex/ddd 1.6.0 → 2.0.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.
- package/README.md +549 -79
- package/dist/index.d.mts +28 -163
- package/dist/index.d.ts +28 -163
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -58,10 +58,10 @@ interface EntityProps<ID extends EntityId, Props> {
|
|
|
58
58
|
* @template ID - The specific Identity Value Object type.
|
|
59
59
|
*/
|
|
60
60
|
declare abstract class Entity<ID extends EntityId, Props> {
|
|
61
|
-
/** The immutable unique identifier for this entity */
|
|
62
|
-
readonly id: ID;
|
|
63
61
|
/** The timestamp when this entity was first instantiated/created */
|
|
64
62
|
readonly createdAt: Date;
|
|
63
|
+
/** The immutable unique identifier for this entity */
|
|
64
|
+
readonly id: ID;
|
|
65
65
|
/**
|
|
66
66
|
* Protected constructor to be called by subclasses.
|
|
67
67
|
* @param props - Initial identity and metadata.
|
|
@@ -75,18 +75,18 @@ declare abstract class Entity<ID extends EntityId, Props> {
|
|
|
75
75
|
* @returns True if IDs are equal.
|
|
76
76
|
*/
|
|
77
77
|
equals(other?: Entity<ID, Props>): boolean;
|
|
78
|
-
/**
|
|
79
|
-
* Validates the current state of the entity against domain invariants.
|
|
80
|
-
* This method should be called after construction and any mutation.
|
|
81
|
-
* @throws {Error} Should throw a specific DomainError if validation fails.
|
|
82
|
-
*/
|
|
83
|
-
abstract validate(): void;
|
|
84
78
|
/**
|
|
85
79
|
* Converts the Entity into a plain Javascript object.
|
|
86
80
|
* Subclasses must implement this to explicitly control serialization,
|
|
87
81
|
* @returns A plain object representation of the entity.
|
|
88
82
|
*/
|
|
89
83
|
abstract toObject(): Record<string, unknown>;
|
|
84
|
+
/**
|
|
85
|
+
* Validates the current state of the entity against domain invariants.
|
|
86
|
+
* This method should be called after construction and any mutation.
|
|
87
|
+
* @throws {Error} Should throw a specific DomainError if validation fails.
|
|
88
|
+
*/
|
|
89
|
+
abstract validate(): void;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
type Primitive$1 = boolean | number | string | null;
|
|
@@ -103,12 +103,12 @@ type DomainEventProps<AggregateId extends EntityId, Payload> = {
|
|
|
103
103
|
payload: Payload;
|
|
104
104
|
};
|
|
105
105
|
declare abstract class DomainEvent<AggregateId extends EntityId = EntityId, T extends DomainEventPayload = DomainEventPayload> {
|
|
106
|
+
readonly aggregateId: AggregateId;
|
|
106
107
|
abstract readonly eventName: string;
|
|
107
108
|
readonly id: string;
|
|
108
|
-
readonly aggregateId: AggregateId;
|
|
109
|
-
readonly schemaVersion: number;
|
|
110
109
|
readonly occurredAt: number;
|
|
111
110
|
readonly payload: Readonly<T>;
|
|
111
|
+
readonly schemaVersion: number;
|
|
112
112
|
protected constructor(props: DomainEventProps<AggregateId, T>);
|
|
113
113
|
toPrimitives(): Readonly<{
|
|
114
114
|
id: string;
|
|
@@ -242,11 +242,6 @@ declare abstract class PrimitiveValueObject<T extends Primitive> implements Enti
|
|
|
242
242
|
* @throws Error if validation fails
|
|
243
243
|
*/
|
|
244
244
|
protected constructor(value: T);
|
|
245
|
-
/**
|
|
246
|
-
* Returns the primitive value.
|
|
247
|
-
* Prefer explicit access over implicit coercion.
|
|
248
|
-
*/
|
|
249
|
-
getValue(): T;
|
|
250
245
|
/**
|
|
251
246
|
* Compares two Value Objects for equality.
|
|
252
247
|
*
|
|
@@ -257,6 +252,11 @@ declare abstract class PrimitiveValueObject<T extends Primitive> implements Enti
|
|
|
257
252
|
* @param other - Another Value Object
|
|
258
253
|
*/
|
|
259
254
|
equals(other?: PrimitiveValueObject<T> | null | undefined): boolean;
|
|
255
|
+
/**
|
|
256
|
+
* Returns the primitive value.
|
|
257
|
+
* Prefer explicit access over implicit coercion.
|
|
258
|
+
*/
|
|
259
|
+
getValue(): T;
|
|
260
260
|
/**
|
|
261
261
|
* JSON serialization hook.
|
|
262
262
|
* Produces the raw primitive value.
|
|
@@ -280,14 +280,6 @@ declare abstract class ValueObject<T> {
|
|
|
280
280
|
get value(): T;
|
|
281
281
|
protected readonly props: Readonly<T>;
|
|
282
282
|
protected constructor(props: T);
|
|
283
|
-
/**
|
|
284
|
-
* Standard for clean API integration and logging.
|
|
285
|
-
*/
|
|
286
|
-
toJSON(): T;
|
|
287
|
-
/**
|
|
288
|
-
* Useful for debugging and string-based indexing.
|
|
289
|
-
*/
|
|
290
|
-
toString(): string;
|
|
291
283
|
/**
|
|
292
284
|
* Type guard to check if an unknown object is an instance of ValueObject.
|
|
293
285
|
* This is useful for runtime type checking.
|
|
@@ -300,6 +292,14 @@ declare abstract class ValueObject<T> {
|
|
|
300
292
|
* Deep equality comparison of ValueObjects
|
|
301
293
|
*/
|
|
302
294
|
equals(other?: ValueObject<T>): boolean;
|
|
295
|
+
/**
|
|
296
|
+
* Standard for clean API integration and logging.
|
|
297
|
+
*/
|
|
298
|
+
toJSON(): T;
|
|
299
|
+
/**
|
|
300
|
+
* Useful for debugging and string-based indexing.
|
|
301
|
+
*/
|
|
302
|
+
toString(): string;
|
|
303
303
|
/**
|
|
304
304
|
* Validates the value object props
|
|
305
305
|
* @throws InvalidValueObjectError if validation fails
|
|
@@ -338,10 +338,6 @@ declare class InvalidValueObjectError extends DomainError {
|
|
|
338
338
|
declare class UUID extends PrimitiveValueObject<string> {
|
|
339
339
|
private static readonly schema;
|
|
340
340
|
constructor(value: string);
|
|
341
|
-
/**
|
|
342
|
-
* Generates a new AggregateId.
|
|
343
|
-
*/
|
|
344
|
-
static generate<T extends typeof UUID>(this: T): InstanceType<T>;
|
|
345
341
|
/**
|
|
346
342
|
* Creates an UUID from an external string.
|
|
347
343
|
* Use only for untrusted input.
|
|
@@ -349,6 +345,10 @@ declare class UUID extends PrimitiveValueObject<string> {
|
|
|
349
345
|
* @param value - UUID string
|
|
350
346
|
*/
|
|
351
347
|
static fromString(value: string): UUID;
|
|
348
|
+
/**
|
|
349
|
+
* Generates a new AggregateId.
|
|
350
|
+
*/
|
|
351
|
+
static generate<T extends typeof UUID>(this: T): InstanceType<T>;
|
|
352
352
|
protected validate(value: string): void;
|
|
353
353
|
}
|
|
354
354
|
|
|
@@ -521,141 +521,6 @@ declare const HttpStatusMessage: {
|
|
|
521
521
|
};
|
|
522
522
|
type HttpStatusMessage = (typeof HttpStatusMessage)[keyof typeof HttpStatusMessage];
|
|
523
523
|
|
|
524
|
-
/**
|
|
525
|
-
* Base class for Domain violations.
|
|
526
|
-
* Purposely does NOT extend native Error to avoid stack trace overhead in the domain.
|
|
527
|
-
*/
|
|
528
|
-
declare abstract class DomainViolation {
|
|
529
|
-
abstract readonly code: string;
|
|
530
|
-
abstract readonly message: string;
|
|
531
|
-
readonly metadata: Readonly<Record<string, unknown>>;
|
|
532
|
-
protected constructor(metadata?: Record<string, unknown>);
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* Represents the outcome of an operation that can either succeed or fail,
|
|
537
|
-
* without relying on exceptions for control flow.
|
|
538
|
-
*
|
|
539
|
-
* This pattern is commonly used in domain and application layers to make
|
|
540
|
-
* success and failure states explicit, predictable, and type-safe.
|
|
541
|
-
*
|
|
542
|
-
* ## Design guarantees
|
|
543
|
-
* - A `Result` is **immutable** once created.
|
|
544
|
-
* - A `Result` is **exactly one of**:
|
|
545
|
-
* - Success → contains a value
|
|
546
|
-
* - Failure → contains an error
|
|
547
|
-
* - Accessing the wrong side throws immediately (fail-fast).
|
|
548
|
-
*
|
|
549
|
-
* @typeParam T - Type of the success value
|
|
550
|
-
* @typeParam E - Type of the failure error
|
|
551
|
-
*
|
|
552
|
-
* @example
|
|
553
|
-
* ```ts
|
|
554
|
-
* function parseNumber(input: string): Result<number, string> {
|
|
555
|
-
* const value = Number(input);
|
|
556
|
-
*
|
|
557
|
-
* if (Number.isNaN(value)) {
|
|
558
|
-
* return Result.fail('Invalid number');
|
|
559
|
-
* }
|
|
560
|
-
*
|
|
561
|
-
* return Result.ok(value);
|
|
562
|
-
* }
|
|
563
|
-
*
|
|
564
|
-
* const result = parseNumber('42');
|
|
565
|
-
*
|
|
566
|
-
* if (result.isSuccess) {
|
|
567
|
-
* console.log(result.getValue()); // 42
|
|
568
|
-
* } else {
|
|
569
|
-
* console.error(result.getError());
|
|
570
|
-
* }
|
|
571
|
-
* ```
|
|
572
|
-
*/
|
|
573
|
-
declare class Result<T, E> {
|
|
574
|
-
private readonly _value?;
|
|
575
|
-
private readonly _error?;
|
|
576
|
-
/**
|
|
577
|
-
* Indicates whether the result represents a successful outcome.
|
|
578
|
-
*
|
|
579
|
-
* This flag is mutually exclusive with {@link isFailure}.
|
|
580
|
-
*/
|
|
581
|
-
readonly isSuccess: boolean;
|
|
582
|
-
/**
|
|
583
|
-
* Indicates whether the result represents a failed outcome.
|
|
584
|
-
*
|
|
585
|
-
* This flag is mutually exclusive with {@link isSuccess}.
|
|
586
|
-
*/
|
|
587
|
-
readonly isFailure: boolean;
|
|
588
|
-
/**
|
|
589
|
-
* Creates a new {@link Result} instance.
|
|
590
|
-
*
|
|
591
|
-
* This constructor is private to enforce the use of
|
|
592
|
-
* {@link Result.ok} and {@link Result.fail} factory methods,
|
|
593
|
-
* preserving the success/failure invariants.
|
|
594
|
-
*
|
|
595
|
-
* @param _value - Success value (defined only for success results)
|
|
596
|
-
* @param _error - Failure error (defined only for failure results)
|
|
597
|
-
*/
|
|
598
|
-
private constructor();
|
|
599
|
-
/**
|
|
600
|
-
* Creates a successful {@link Result}.
|
|
601
|
-
*
|
|
602
|
-
* @param value - Value representing a successful outcome
|
|
603
|
-
* @returns A success {@link Result} containing the provided value
|
|
604
|
-
*
|
|
605
|
-
* @example
|
|
606
|
-
* ```ts
|
|
607
|
-
* return Result.ok(user);
|
|
608
|
-
* ```
|
|
609
|
-
*/
|
|
610
|
-
static ok<T, E>(value: T): Result<T, E>;
|
|
611
|
-
/**
|
|
612
|
-
* Creates a failed {@link Result}.
|
|
613
|
-
*
|
|
614
|
-
* @param error - Error describing the failure
|
|
615
|
-
* @returns A failure {@link Result} containing the provided error
|
|
616
|
-
*
|
|
617
|
-
* @example
|
|
618
|
-
* ```ts
|
|
619
|
-
* return Result.fail(new ValidationError('Email is invalid'));
|
|
620
|
-
* ```
|
|
621
|
-
*/
|
|
622
|
-
static fail<T, E>(error: E): Result<T, E>;
|
|
623
|
-
/**
|
|
624
|
-
* Returns the success value.
|
|
625
|
-
*
|
|
626
|
-
* @returns The value associated with a successful result
|
|
627
|
-
*
|
|
628
|
-
* @throws {Error}
|
|
629
|
-
* Thrown if this result represents a failure.
|
|
630
|
-
* This is a fail-fast guard against incorrect usage.
|
|
631
|
-
*
|
|
632
|
-
* @example
|
|
633
|
-
* ```ts
|
|
634
|
-
* if (result.isSuccess) {
|
|
635
|
-
* const value = result.getValue();
|
|
636
|
-
* }
|
|
637
|
-
* ```
|
|
638
|
-
*/
|
|
639
|
-
getValue(): T;
|
|
640
|
-
/**
|
|
641
|
-
* Returns the failure error.
|
|
642
|
-
*
|
|
643
|
-
* @returns The error associated with a failed result
|
|
644
|
-
*
|
|
645
|
-
* @throws {Error}
|
|
646
|
-
* Thrown if this result represents a success.
|
|
647
|
-
* This is a fail-fast guard against incorrect usage.
|
|
648
|
-
*
|
|
649
|
-
* @example
|
|
650
|
-
* ```ts
|
|
651
|
-
* if (result.isFailure) {
|
|
652
|
-
* const error = result.getError();
|
|
653
|
-
* }
|
|
654
|
-
* ```
|
|
655
|
-
*/
|
|
656
|
-
getError(): E;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
524
|
/**
|
|
660
525
|
* Utility to deeply freeze objects to ensure immutability - handles nested objects and arrays.
|
|
661
526
|
*
|
|
@@ -717,4 +582,4 @@ type UnwrapValueObject<T> = T extends ValueObject<infer V> ? UnwrapValueObject<V
|
|
|
717
582
|
declare function unwrapValueObject<T>(input: T, seen?: WeakSet<object>): UnwrapValueObject<T>;
|
|
718
583
|
declare function ensureObject<T>(input: UnwrapValueObject<T>): object;
|
|
719
584
|
|
|
720
|
-
export { AggregateId, AggregateRoot, ApplicationError, type ApplicationServicePort, DomainError, type DomainErrorMetadata, DomainEvent, type DomainEventPayload,
|
|
585
|
+
export { AggregateId, AggregateRoot, ApplicationError, type ApplicationServicePort, DomainError, type DomainErrorMetadata, DomainEvent, type DomainEventPayload, Entity, type EntityId, type EntityProps, EntityValidationError, HttpStatus, type HttpStatusCode, HttpStatusMessage, InvalidValueObjectError, PrimitiveValueObject, type Props, UUID, type UnixTimestampMillis, type UnwrapValueObject, ValueObject, deepFreeze, ensureObject, unwrapValueObject };
|
package/dist/index.d.ts
CHANGED
|
@@ -58,10 +58,10 @@ interface EntityProps<ID extends EntityId, Props> {
|
|
|
58
58
|
* @template ID - The specific Identity Value Object type.
|
|
59
59
|
*/
|
|
60
60
|
declare abstract class Entity<ID extends EntityId, Props> {
|
|
61
|
-
/** The immutable unique identifier for this entity */
|
|
62
|
-
readonly id: ID;
|
|
63
61
|
/** The timestamp when this entity was first instantiated/created */
|
|
64
62
|
readonly createdAt: Date;
|
|
63
|
+
/** The immutable unique identifier for this entity */
|
|
64
|
+
readonly id: ID;
|
|
65
65
|
/**
|
|
66
66
|
* Protected constructor to be called by subclasses.
|
|
67
67
|
* @param props - Initial identity and metadata.
|
|
@@ -75,18 +75,18 @@ declare abstract class Entity<ID extends EntityId, Props> {
|
|
|
75
75
|
* @returns True if IDs are equal.
|
|
76
76
|
*/
|
|
77
77
|
equals(other?: Entity<ID, Props>): boolean;
|
|
78
|
-
/**
|
|
79
|
-
* Validates the current state of the entity against domain invariants.
|
|
80
|
-
* This method should be called after construction and any mutation.
|
|
81
|
-
* @throws {Error} Should throw a specific DomainError if validation fails.
|
|
82
|
-
*/
|
|
83
|
-
abstract validate(): void;
|
|
84
78
|
/**
|
|
85
79
|
* Converts the Entity into a plain Javascript object.
|
|
86
80
|
* Subclasses must implement this to explicitly control serialization,
|
|
87
81
|
* @returns A plain object representation of the entity.
|
|
88
82
|
*/
|
|
89
83
|
abstract toObject(): Record<string, unknown>;
|
|
84
|
+
/**
|
|
85
|
+
* Validates the current state of the entity against domain invariants.
|
|
86
|
+
* This method should be called after construction and any mutation.
|
|
87
|
+
* @throws {Error} Should throw a specific DomainError if validation fails.
|
|
88
|
+
*/
|
|
89
|
+
abstract validate(): void;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
type Primitive$1 = boolean | number | string | null;
|
|
@@ -103,12 +103,12 @@ type DomainEventProps<AggregateId extends EntityId, Payload> = {
|
|
|
103
103
|
payload: Payload;
|
|
104
104
|
};
|
|
105
105
|
declare abstract class DomainEvent<AggregateId extends EntityId = EntityId, T extends DomainEventPayload = DomainEventPayload> {
|
|
106
|
+
readonly aggregateId: AggregateId;
|
|
106
107
|
abstract readonly eventName: string;
|
|
107
108
|
readonly id: string;
|
|
108
|
-
readonly aggregateId: AggregateId;
|
|
109
|
-
readonly schemaVersion: number;
|
|
110
109
|
readonly occurredAt: number;
|
|
111
110
|
readonly payload: Readonly<T>;
|
|
111
|
+
readonly schemaVersion: number;
|
|
112
112
|
protected constructor(props: DomainEventProps<AggregateId, T>);
|
|
113
113
|
toPrimitives(): Readonly<{
|
|
114
114
|
id: string;
|
|
@@ -242,11 +242,6 @@ declare abstract class PrimitiveValueObject<T extends Primitive> implements Enti
|
|
|
242
242
|
* @throws Error if validation fails
|
|
243
243
|
*/
|
|
244
244
|
protected constructor(value: T);
|
|
245
|
-
/**
|
|
246
|
-
* Returns the primitive value.
|
|
247
|
-
* Prefer explicit access over implicit coercion.
|
|
248
|
-
*/
|
|
249
|
-
getValue(): T;
|
|
250
245
|
/**
|
|
251
246
|
* Compares two Value Objects for equality.
|
|
252
247
|
*
|
|
@@ -257,6 +252,11 @@ declare abstract class PrimitiveValueObject<T extends Primitive> implements Enti
|
|
|
257
252
|
* @param other - Another Value Object
|
|
258
253
|
*/
|
|
259
254
|
equals(other?: PrimitiveValueObject<T> | null | undefined): boolean;
|
|
255
|
+
/**
|
|
256
|
+
* Returns the primitive value.
|
|
257
|
+
* Prefer explicit access over implicit coercion.
|
|
258
|
+
*/
|
|
259
|
+
getValue(): T;
|
|
260
260
|
/**
|
|
261
261
|
* JSON serialization hook.
|
|
262
262
|
* Produces the raw primitive value.
|
|
@@ -280,14 +280,6 @@ declare abstract class ValueObject<T> {
|
|
|
280
280
|
get value(): T;
|
|
281
281
|
protected readonly props: Readonly<T>;
|
|
282
282
|
protected constructor(props: T);
|
|
283
|
-
/**
|
|
284
|
-
* Standard for clean API integration and logging.
|
|
285
|
-
*/
|
|
286
|
-
toJSON(): T;
|
|
287
|
-
/**
|
|
288
|
-
* Useful for debugging and string-based indexing.
|
|
289
|
-
*/
|
|
290
|
-
toString(): string;
|
|
291
283
|
/**
|
|
292
284
|
* Type guard to check if an unknown object is an instance of ValueObject.
|
|
293
285
|
* This is useful for runtime type checking.
|
|
@@ -300,6 +292,14 @@ declare abstract class ValueObject<T> {
|
|
|
300
292
|
* Deep equality comparison of ValueObjects
|
|
301
293
|
*/
|
|
302
294
|
equals(other?: ValueObject<T>): boolean;
|
|
295
|
+
/**
|
|
296
|
+
* Standard for clean API integration and logging.
|
|
297
|
+
*/
|
|
298
|
+
toJSON(): T;
|
|
299
|
+
/**
|
|
300
|
+
* Useful for debugging and string-based indexing.
|
|
301
|
+
*/
|
|
302
|
+
toString(): string;
|
|
303
303
|
/**
|
|
304
304
|
* Validates the value object props
|
|
305
305
|
* @throws InvalidValueObjectError if validation fails
|
|
@@ -338,10 +338,6 @@ declare class InvalidValueObjectError extends DomainError {
|
|
|
338
338
|
declare class UUID extends PrimitiveValueObject<string> {
|
|
339
339
|
private static readonly schema;
|
|
340
340
|
constructor(value: string);
|
|
341
|
-
/**
|
|
342
|
-
* Generates a new AggregateId.
|
|
343
|
-
*/
|
|
344
|
-
static generate<T extends typeof UUID>(this: T): InstanceType<T>;
|
|
345
341
|
/**
|
|
346
342
|
* Creates an UUID from an external string.
|
|
347
343
|
* Use only for untrusted input.
|
|
@@ -349,6 +345,10 @@ declare class UUID extends PrimitiveValueObject<string> {
|
|
|
349
345
|
* @param value - UUID string
|
|
350
346
|
*/
|
|
351
347
|
static fromString(value: string): UUID;
|
|
348
|
+
/**
|
|
349
|
+
* Generates a new AggregateId.
|
|
350
|
+
*/
|
|
351
|
+
static generate<T extends typeof UUID>(this: T): InstanceType<T>;
|
|
352
352
|
protected validate(value: string): void;
|
|
353
353
|
}
|
|
354
354
|
|
|
@@ -521,141 +521,6 @@ declare const HttpStatusMessage: {
|
|
|
521
521
|
};
|
|
522
522
|
type HttpStatusMessage = (typeof HttpStatusMessage)[keyof typeof HttpStatusMessage];
|
|
523
523
|
|
|
524
|
-
/**
|
|
525
|
-
* Base class for Domain violations.
|
|
526
|
-
* Purposely does NOT extend native Error to avoid stack trace overhead in the domain.
|
|
527
|
-
*/
|
|
528
|
-
declare abstract class DomainViolation {
|
|
529
|
-
abstract readonly code: string;
|
|
530
|
-
abstract readonly message: string;
|
|
531
|
-
readonly metadata: Readonly<Record<string, unknown>>;
|
|
532
|
-
protected constructor(metadata?: Record<string, unknown>);
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* Represents the outcome of an operation that can either succeed or fail,
|
|
537
|
-
* without relying on exceptions for control flow.
|
|
538
|
-
*
|
|
539
|
-
* This pattern is commonly used in domain and application layers to make
|
|
540
|
-
* success and failure states explicit, predictable, and type-safe.
|
|
541
|
-
*
|
|
542
|
-
* ## Design guarantees
|
|
543
|
-
* - A `Result` is **immutable** once created.
|
|
544
|
-
* - A `Result` is **exactly one of**:
|
|
545
|
-
* - Success → contains a value
|
|
546
|
-
* - Failure → contains an error
|
|
547
|
-
* - Accessing the wrong side throws immediately (fail-fast).
|
|
548
|
-
*
|
|
549
|
-
* @typeParam T - Type of the success value
|
|
550
|
-
* @typeParam E - Type of the failure error
|
|
551
|
-
*
|
|
552
|
-
* @example
|
|
553
|
-
* ```ts
|
|
554
|
-
* function parseNumber(input: string): Result<number, string> {
|
|
555
|
-
* const value = Number(input);
|
|
556
|
-
*
|
|
557
|
-
* if (Number.isNaN(value)) {
|
|
558
|
-
* return Result.fail('Invalid number');
|
|
559
|
-
* }
|
|
560
|
-
*
|
|
561
|
-
* return Result.ok(value);
|
|
562
|
-
* }
|
|
563
|
-
*
|
|
564
|
-
* const result = parseNumber('42');
|
|
565
|
-
*
|
|
566
|
-
* if (result.isSuccess) {
|
|
567
|
-
* console.log(result.getValue()); // 42
|
|
568
|
-
* } else {
|
|
569
|
-
* console.error(result.getError());
|
|
570
|
-
* }
|
|
571
|
-
* ```
|
|
572
|
-
*/
|
|
573
|
-
declare class Result<T, E> {
|
|
574
|
-
private readonly _value?;
|
|
575
|
-
private readonly _error?;
|
|
576
|
-
/**
|
|
577
|
-
* Indicates whether the result represents a successful outcome.
|
|
578
|
-
*
|
|
579
|
-
* This flag is mutually exclusive with {@link isFailure}.
|
|
580
|
-
*/
|
|
581
|
-
readonly isSuccess: boolean;
|
|
582
|
-
/**
|
|
583
|
-
* Indicates whether the result represents a failed outcome.
|
|
584
|
-
*
|
|
585
|
-
* This flag is mutually exclusive with {@link isSuccess}.
|
|
586
|
-
*/
|
|
587
|
-
readonly isFailure: boolean;
|
|
588
|
-
/**
|
|
589
|
-
* Creates a new {@link Result} instance.
|
|
590
|
-
*
|
|
591
|
-
* This constructor is private to enforce the use of
|
|
592
|
-
* {@link Result.ok} and {@link Result.fail} factory methods,
|
|
593
|
-
* preserving the success/failure invariants.
|
|
594
|
-
*
|
|
595
|
-
* @param _value - Success value (defined only for success results)
|
|
596
|
-
* @param _error - Failure error (defined only for failure results)
|
|
597
|
-
*/
|
|
598
|
-
private constructor();
|
|
599
|
-
/**
|
|
600
|
-
* Creates a successful {@link Result}.
|
|
601
|
-
*
|
|
602
|
-
* @param value - Value representing a successful outcome
|
|
603
|
-
* @returns A success {@link Result} containing the provided value
|
|
604
|
-
*
|
|
605
|
-
* @example
|
|
606
|
-
* ```ts
|
|
607
|
-
* return Result.ok(user);
|
|
608
|
-
* ```
|
|
609
|
-
*/
|
|
610
|
-
static ok<T, E>(value: T): Result<T, E>;
|
|
611
|
-
/**
|
|
612
|
-
* Creates a failed {@link Result}.
|
|
613
|
-
*
|
|
614
|
-
* @param error - Error describing the failure
|
|
615
|
-
* @returns A failure {@link Result} containing the provided error
|
|
616
|
-
*
|
|
617
|
-
* @example
|
|
618
|
-
* ```ts
|
|
619
|
-
* return Result.fail(new ValidationError('Email is invalid'));
|
|
620
|
-
* ```
|
|
621
|
-
*/
|
|
622
|
-
static fail<T, E>(error: E): Result<T, E>;
|
|
623
|
-
/**
|
|
624
|
-
* Returns the success value.
|
|
625
|
-
*
|
|
626
|
-
* @returns The value associated with a successful result
|
|
627
|
-
*
|
|
628
|
-
* @throws {Error}
|
|
629
|
-
* Thrown if this result represents a failure.
|
|
630
|
-
* This is a fail-fast guard against incorrect usage.
|
|
631
|
-
*
|
|
632
|
-
* @example
|
|
633
|
-
* ```ts
|
|
634
|
-
* if (result.isSuccess) {
|
|
635
|
-
* const value = result.getValue();
|
|
636
|
-
* }
|
|
637
|
-
* ```
|
|
638
|
-
*/
|
|
639
|
-
getValue(): T;
|
|
640
|
-
/**
|
|
641
|
-
* Returns the failure error.
|
|
642
|
-
*
|
|
643
|
-
* @returns The error associated with a failed result
|
|
644
|
-
*
|
|
645
|
-
* @throws {Error}
|
|
646
|
-
* Thrown if this result represents a success.
|
|
647
|
-
* This is a fail-fast guard against incorrect usage.
|
|
648
|
-
*
|
|
649
|
-
* @example
|
|
650
|
-
* ```ts
|
|
651
|
-
* if (result.isFailure) {
|
|
652
|
-
* const error = result.getError();
|
|
653
|
-
* }
|
|
654
|
-
* ```
|
|
655
|
-
*/
|
|
656
|
-
getError(): E;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
524
|
/**
|
|
660
525
|
* Utility to deeply freeze objects to ensure immutability - handles nested objects and arrays.
|
|
661
526
|
*
|
|
@@ -717,4 +582,4 @@ type UnwrapValueObject<T> = T extends ValueObject<infer V> ? UnwrapValueObject<V
|
|
|
717
582
|
declare function unwrapValueObject<T>(input: T, seen?: WeakSet<object>): UnwrapValueObject<T>;
|
|
718
583
|
declare function ensureObject<T>(input: UnwrapValueObject<T>): object;
|
|
719
584
|
|
|
720
|
-
export { AggregateId, AggregateRoot, ApplicationError, type ApplicationServicePort, DomainError, type DomainErrorMetadata, DomainEvent, type DomainEventPayload,
|
|
585
|
+
export { AggregateId, AggregateRoot, ApplicationError, type ApplicationServicePort, DomainError, type DomainErrorMetadata, DomainEvent, type DomainEventPayload, Entity, type EntityId, type EntityProps, EntityValidationError, HttpStatus, type HttpStatusCode, HttpStatusMessage, InvalidValueObjectError, PrimitiveValueObject, type Props, UUID, type UnixTimestampMillis, type UnwrapValueObject, ValueObject, deepFreeze, ensureObject, unwrapValueObject };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var w=Object.create;var s=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames,A=Object.getOwnPropertySymbols,M=Object.getPrototypeOf,N=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable;var D=(e,t,r)=>t in e?s(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,O=(e,t)=>{for(var r in t||(t={}))N.call(t,r)&&D(e,r,t[r]);if(A)for(var r of A(t))C.call(t,r)&&D(e,r,t[r]);return e};var j=(e,t)=>{for(var r in t)s(e,r,{get:t[r],enumerable:!0})},_=(e,t,r,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of L(t))!N.call(e,n)&&n!==r&&s(e,n,{get:()=>t[n],enumerable:!(a=V(t,n))||a.enumerable});return e};var S=(e,t,r)=>(r=e!=null?w(M(e)):{},_(t||!e||!e.__esModule?s(r,"default",{value:e,enumerable:!0}):r,e)),H=e=>_(s({},"__esModule",{value:!0}),e);var q={};j(q,{AggregateId:()=>g,AggregateRoot:()=>f,ApplicationError:()=>b,DomainError:()=>o,DomainEvent:()=>I,Entity:()=>d,EntityValidationError:()=>y,HttpStatus:()=>k,HttpStatusMessage:()=>R,InvalidValueObjectError:()=>E,PrimitiveValueObject:()=>c,UUID:()=>p,ValueObject:()=>u,deepFreeze:()=>l,ensureObject:()=>G,unwrapValueObject:()=>i});module.exports=H(q);var d=class{constructor(t){var r;this.id=t.id,this.createdAt=(r=t.createdAt)!=null?r:new Date}equals(t){return t==null?!1:this===t?!0:this.id.equals(t.id)}};var f=class extends d{constructor(){super(...arguments);this._domainEvents=[]}get domainEvents(){return[...this._domainEvents]}addEvent(r){this._domainEvents.push(r)}pullDomainEvents(){let r=[...this._domainEvents];return this._domainEvents.length=0,r}};var F=e=>e instanceof Error?{cause:{message:e.message,stack:e.stack,name:e.name}}:e?{cause:e}:{},o=class extends Error{constructor(t,r,a={}){super(t,O({},F(a.cause))),Object.setPrototypeOf(this,new.target.prototype),this.name=new.target.name,this.code=r,this.metadata=Object.freeze(O({},a))}};var c=class{constructor(t){this.validate(t),this.value=t}equals(t){return t==null||Object.getPrototypeOf(this)!==Object.getPrototypeOf(t)?!1:this.value===t.value}getValue(){return this.value}toJSON(){return this.value}toString(){return String(this.value)}};var P=S(require("fast-deep-equal/es6"));function l(e,t=new WeakSet){if(e==null||typeof e!="object"&&!Array.isArray(e)||Object.isFrozen(e)||t.has(e))return e;if(t.add(e),Array.isArray(e))e.forEach(r=>l(r,t));else for(let r in e)Object.hasOwn(e,r)&&l(e[r],t);return Object.freeze(e)}var u=class e{get value(){return this.props}constructor(t){this.validate(t),this.props=l(t)}static is(t){return t instanceof e}equals(t){return t==null||Object.getPrototypeOf(this)!==Object.getPrototypeOf(t)?!1:(0,P.default)(this.props,t.props)}toJSON(){return this.props}toString(){return JSON.stringify(this.props)}};var y=class extends o{constructor(t,r){super(t,"ENTITY_VALIDATION_ERROR",{cause:r})}};var E=class extends o{constructor(t){super(t,"INVALID_VALUE_OBJECT")}};var U=require("crypto"),I=class{constructor(t){var r;this.id=(r=t.id)!=null?r:(0,U.randomUUID)(),this.aggregateId=t.aggregateId,this.schemaVersion=t.schemaVersion,this.occurredAt=t.occurredAt,this.payload=Object.freeze(t.payload)}toPrimitives(){return{aggregateId:this.aggregateId.toString(),schemaVersion:this.schemaVersion,occurredAt:this.occurredAt,eventName:this.eventName,payload:this.payload,id:this.id}}};var h=require("uuid"),v=S(require("zod"));var T=class T extends c{constructor(t){super(t),this.validate(t)}static fromString(t){return new this(t)}static generate(){return new this((0,h.v4)())}validate(t){let r=T.schema.safeParse(t);if(!r.success)throw new E(`Invalid UUID: ${r.error.message}`)}};T.schema=v.default.uuid();var p=T;var g=class extends p{};var k=Object.freeze({REQUEST_HEADER_FIELDS_TOO_LARGE:431,NETWORK_AUTHENTICATION_REQUIRED:511,NON_AUTHORITATIVE_INFORMATION:203,PROXY_AUTHENTICATION_REQUIRED:407,UNAVAILABLE_FOR_LEGAL_REASONS:451,HTTP_VERSION_NOT_SUPPORTED:505,BANDWIDTH_LIMIT_EXCEEDED:509,VARIANT_ALSO_NEGOTIATES:506,UNSUPPORTED_MEDIA_TYPE:415,RANGE_NOT_SATISFIABLE:416,PRECONDITION_REQUIRED:428,INTERNAL_SERVER_ERROR:500,UNPROCESSABLE_ENTITY:422,INSUFFICIENT_STORAGE:507,SWITCHING_PROTOCOLS:101,PRECONDITION_FAILED:412,MISDIRECTED_REQUEST:421,SERVICE_UNAVAILABLE:503,TEMPORARY_REDIRECT:307,PERMANENT_REDIRECT:308,METHOD_NOT_ALLOWED:405,EXPECTATION_FAILED:417,MOVED_PERMANENTLY:301,PAYLOAD_TOO_LARGE:413,FAILED_DEPENDENCY:424,TOO_MANY_REQUESTS:429,ALREADY_REPORTED:208,MULTIPLE_CHOICES:300,PAYMENT_REQUIRED:402,UPGRADE_REQUIRED:426,PARTIAL_CONTENT:206,REQUEST_TIMEOUT:408,LENGTH_REQUIRED:411,NOT_IMPLEMENTED:501,GATEWAY_TIMEOUT:504,NOT_ACCEPTABLE:406,RESET_CONTENT:205,LOOP_DETECTED:508,MULTI_STATUS:207,NOT_MODIFIED:304,UNAUTHORIZED:401,URI_TOO_LONG:414,NOT_EXTENDED:510,EARLY_HINTS:103,BAD_REQUEST:400,IM_A_TEAPOT:418,BAD_GATEWAY:502,PROCESSING:102,NO_CONTENT:204,SEE_OTHER:303,USE_PROXY:305,FORBIDDEN:403,NOT_FOUND:404,TOO_EARLY:425,CONTINUE:100,ACCEPTED:202,CONFLICT:409,CREATED:201,IM_USED:226,LOCKED:423,FOUND:302,GONE:410,OK:200}),R={431:"Request Header Fields Too Large",511:"Network Authentication Required",203:"Non-Authoritative Information",407:"Proxy Authentication Required",451:"Unavailable For Legal Reasons",505:"HTTP Version Not Supported",509:"Bandwidth Limit Exceeded",506:"Variant Also Negotiates",415:"Unsupported Media Type",416:"Range Not Satisfiable",428:"Precondition Required",500:"Internal Server Error",422:"Unprocessable Entity",507:"Insufficient Storage",101:"Switching Protocols",412:"Precondition Failed",421:"Misdirected Request",503:"Service Unavailable",307:"Temporary Redirect",308:"Permanent Redirect",405:"Method Not Allowed",417:"Expectation Failed",301:"Moved Permanently",413:"Payload Too Large",424:"Failed Dependency",429:"Too Many Requests",208:"Already Reported",300:"Multiple Choices",402:"Payment Required",426:"Upgrade Required",206:"Partial Content",408:"Request Timeout",411:"Length Required",501:"Not Implemented",504:"Gateway Timeout",406:"Not Acceptable",205:"Reset Content",508:"Loop Detected",207:"Multi-Status",304:"Not Modified",401:"Unauthorized",414:"URI Too Long",418:"I'm a Teapot",510:"Not Extended",103:"Early Hints",400:"Bad Request",502:"Bad Gateway",102:"Processing",204:"No Content",303:"See Other",305:"Use Proxy",403:"Forbidden",404:"Not Found",425:"Too Early",100:"Continue",202:"Accepted",226:"I'm Used",409:"Conflict",201:"Created",423:"Locked",302:"Found",410:"Gone",200:"OK"};var b=class extends Error{constructor({code:t=R[500],isOperational:r=!1,status:a=500,metadata:n,message:m,cause:x}){super(m),this.name=new.target.name,this.code=t,this.status=a,this.isOperational=r,this.metadata=n,this.cause=x,Error.captureStackTrace(this,new.target)}};function i(e,t=new WeakSet){if(e==null||typeof e!="object")return e;if(t.has(e))throw new Error("Circular reference detected in ValueObject unwrap");if(t.add(e),Array.isArray(e)){let a=e.map(n=>i(n,t));return t.delete(e),a}if(e instanceof u){let a=i(e.value,t);return t.delete(e),a}if(e instanceof Date)return t.delete(e),e.toISOString();if(e instanceof Map){let a=new Map;return e.forEach((n,m)=>{a.set(m,i(n,t))}),t.delete(e),a}if(e instanceof Set){let a=new Set(Array.from(e.values()).map(n=>i(n,t)));return t.delete(e),a}let r={};for(let a in e)Object.hasOwn(e,a)&&(r[a]=i(e[a],t));return t.delete(e),r}function G(e){return e==null?{}:typeof e=="object"?e:{value:e}}0&&(module.exports={AggregateId,AggregateRoot,ApplicationError,DomainError,DomainEvent,Entity,EntityValidationError,HttpStatus,HttpStatusMessage,InvalidValueObjectError,PrimitiveValueObject,UUID,ValueObject,deepFreeze,ensureObject,unwrapValueObject});
|
|
2
2
|
//# sourceMappingURL=index.js.map
|