@vytches/ddd-value-objects 0.26.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/LICENSE +21 -0
- package/LLMGUIDE.md +233 -0
- package/README.md +1391 -0
- package/dist/base-value-object.d.ts +35 -0
- package/dist/branded-id.d.ts +58 -0
- package/dist/id.value-object.d.ts +106 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +248 -0
- package/package.json +68 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface ValueObjectValidator<T> {
|
|
2
|
+
validate(value: T): boolean;
|
|
3
|
+
}
|
|
4
|
+
export declare abstract class BaseValueObject<T> implements ValueObjectValidator<T> {
|
|
5
|
+
protected readonly value: T;
|
|
6
|
+
constructor(value: T);
|
|
7
|
+
/**
|
|
8
|
+
* Compares this value object with another for equality.
|
|
9
|
+
* Uses deep structural comparison for object values, === for primitives.
|
|
10
|
+
* @param valueObject - The value object to compare with
|
|
11
|
+
* @returns True if they are equal, false otherwise
|
|
12
|
+
*/
|
|
13
|
+
equals(valueObject: BaseValueObject<T>): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Returns a string representation of the value object
|
|
16
|
+
* @returns String representation
|
|
17
|
+
*/
|
|
18
|
+
toString(): string;
|
|
19
|
+
/**
|
|
20
|
+
* Returns the value for JSON serialization.
|
|
21
|
+
* Returns T (not string) so JSON.stringify works correctly without double-encoding.
|
|
22
|
+
*/
|
|
23
|
+
toJSON(): unknown;
|
|
24
|
+
/**
|
|
25
|
+
* Gets the underlying value as a readonly reference.
|
|
26
|
+
* @returns The underlying value (frozen for object types)
|
|
27
|
+
*/
|
|
28
|
+
getValue(): Readonly<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Abstract method to validate the value
|
|
31
|
+
* @param value The value to validate
|
|
32
|
+
* @returns True if valid, false otherwise
|
|
33
|
+
*/
|
|
34
|
+
abstract validate(value: unknown): boolean;
|
|
35
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { EntityId } from './id.value-object';
|
|
2
|
+
declare const __brand: unique symbol;
|
|
3
|
+
/**
|
|
4
|
+
* A branded EntityId that carries a compile-time tag.
|
|
5
|
+
* Prevents mixing IDs from different aggregate types.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
* @stable
|
|
9
|
+
* @since 0.24.0
|
|
10
|
+
*/
|
|
11
|
+
export type BrandedId<Tag extends string> = EntityId<string> & {
|
|
12
|
+
readonly [__brand]: Tag;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Create a branded ID from an existing EntityId.
|
|
16
|
+
*
|
|
17
|
+
* @public
|
|
18
|
+
* @stable
|
|
19
|
+
* @since 0.24.0
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* type OrderId = BrandedId<'Order'>;
|
|
24
|
+
* const orderId = createBrandedId<'Order'>(EntityId.create());
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare function createBrandedId<Tag extends string>(id: EntityId<string>): BrandedId<Tag>;
|
|
28
|
+
/**
|
|
29
|
+
* Create a new branded ID with a random UUID.
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
* @stable
|
|
33
|
+
* @since 0.24.0
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* type OrderId = BrandedId<'Order'>;
|
|
38
|
+
* const orderId = newBrandedId<'Order'>();
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function newBrandedId<Tag extends string>(): BrandedId<Tag>;
|
|
42
|
+
/**
|
|
43
|
+
* Create a branded ID from a UUID string.
|
|
44
|
+
*
|
|
45
|
+
* @public
|
|
46
|
+
* @stable
|
|
47
|
+
* @since 0.24.0
|
|
48
|
+
*/
|
|
49
|
+
export declare function brandedIdFromUUID<Tag extends string>(uuid: string): BrandedId<Tag>;
|
|
50
|
+
/**
|
|
51
|
+
* Create a branded ID from a text value.
|
|
52
|
+
*
|
|
53
|
+
* @public
|
|
54
|
+
* @stable
|
|
55
|
+
* @since 0.24.0
|
|
56
|
+
*/
|
|
57
|
+
export declare function brandedIdFromText<Tag extends string>(text: string): BrandedId<Tag>;
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { EntityId as BaseEntityId, IEntityIdFactory, IdType } from '@vytches/ddd-contracts';
|
|
2
|
+
import { Result } from '@vytches/ddd-utils';
|
|
3
|
+
export declare class EntityId<T = string> extends BaseEntityId<T> {
|
|
4
|
+
constructor(value: T, type: IdType);
|
|
5
|
+
validate(value: T): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Primary factory method - creates a new EntityId with a randomly generated UUID
|
|
8
|
+
* @returns New EntityId with random UUID
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const id = EntityId.create();
|
|
12
|
+
* console.log(id.getValue()); // Returns: '550e8400-e29b-41d4-a716-446655440000'
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
static create(): EntityId<string>;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated Use `create()` instead. Will be removed in next major version.
|
|
18
|
+
*/
|
|
19
|
+
static createWithRandomUUID(): EntityId<string>;
|
|
20
|
+
/**
|
|
21
|
+
* @returns EntityId instance
|
|
22
|
+
* @throws {MissingValueError} if value is empty
|
|
23
|
+
* @throws {InvalidParameterError} if value is not a valid UUID
|
|
24
|
+
*/
|
|
25
|
+
static fromUUID(value: string): EntityId<string>;
|
|
26
|
+
static fromInteger(value: number): EntityId<string>;
|
|
27
|
+
static fromBigInt(value: string | bigint): EntityId<string>;
|
|
28
|
+
static fromText(value: string): EntityId<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Create EntityId from UUID string, returning Result instead of throwing.
|
|
31
|
+
* @public
|
|
32
|
+
* @stable
|
|
33
|
+
* @since 0.24.0
|
|
34
|
+
*/
|
|
35
|
+
static tryFromUUID(value: string): Result<EntityId<string>, Error>;
|
|
36
|
+
/**
|
|
37
|
+
* Create EntityId from integer, returning Result instead of throwing.
|
|
38
|
+
* @public
|
|
39
|
+
* @stable
|
|
40
|
+
* @since 0.24.0
|
|
41
|
+
*/
|
|
42
|
+
static tryFromInteger(value: number): Result<EntityId<string>, Error>;
|
|
43
|
+
/**
|
|
44
|
+
* Create EntityId from bigint, returning Result instead of throwing.
|
|
45
|
+
* @public
|
|
46
|
+
* @stable
|
|
47
|
+
* @since 0.24.0
|
|
48
|
+
*/
|
|
49
|
+
static tryFromBigInt(value: string | bigint): Result<EntityId<string>, Error>;
|
|
50
|
+
/**
|
|
51
|
+
* Create EntityId from text, returning Result instead of throwing.
|
|
52
|
+
* @public
|
|
53
|
+
* @stable
|
|
54
|
+
* @since 0.24.0
|
|
55
|
+
*/
|
|
56
|
+
static tryFromText(value: string): Result<EntityId<string>, Error>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* @deprecated Use EntityId static methods directly instead. This class will be removed in the next major version.
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* // Before (deprecated):
|
|
63
|
+
* const id = EntityIdFactory.createWithRandomUUID();
|
|
64
|
+
*
|
|
65
|
+
* // After (recommended):
|
|
66
|
+
* const id = EntityId.create();
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare class EntityIdFactory implements IEntityIdFactory {
|
|
70
|
+
/**
|
|
71
|
+
* @deprecated Use EntityId.create() instead. Will be removed in v1.0.0.
|
|
72
|
+
*/
|
|
73
|
+
static createWithRandomUUID(): EntityId<string>;
|
|
74
|
+
/**
|
|
75
|
+
* @deprecated Use EntityId.fromUUID() instead. Will be removed in v1.0.0.
|
|
76
|
+
* @throws {MissingValueError} if value is empty
|
|
77
|
+
* @throws {InvalidParameterError} if value is empty
|
|
78
|
+
*/
|
|
79
|
+
static fromUUID(value: string): EntityId<string>;
|
|
80
|
+
/**
|
|
81
|
+
* @deprecated Use EntityId.fromInteger() instead. Will be removed in v1.0.0.
|
|
82
|
+
* @throws {InvalidParameterError} if value is empty
|
|
83
|
+
*/
|
|
84
|
+
static fromInteger(value: number): EntityId<string>;
|
|
85
|
+
/**
|
|
86
|
+
* @deprecated Use EntityId.fromBigInt() instead. Will be removed in v1.0.0.
|
|
87
|
+
* @throws {InvalidParameterError} if value is empty
|
|
88
|
+
*/
|
|
89
|
+
static fromBigInt(value: string | bigint): EntityId<string>;
|
|
90
|
+
/**
|
|
91
|
+
* @deprecated Use EntityId.fromText() instead. Will be removed in v1.0.0.
|
|
92
|
+
* @throws {MissingValueError} if value is empty
|
|
93
|
+
* @throws {InvalidParameterError} if value is empty
|
|
94
|
+
*/
|
|
95
|
+
static fromText(value: string): EntityId<string>;
|
|
96
|
+
/** @deprecated Use EntityId.create() instead */
|
|
97
|
+
createWithRandomUUID(): EntityId<string>;
|
|
98
|
+
/** @deprecated Use EntityId.fromUUID() instead */
|
|
99
|
+
fromUUID(value: string): EntityId<string>;
|
|
100
|
+
/** @deprecated Use EntityId.fromInteger() instead */
|
|
101
|
+
fromInteger(value: number): EntityId<string>;
|
|
102
|
+
/** @deprecated Use EntityId.fromBigInt() instead */
|
|
103
|
+
fromBigInt(value: string | bigint): EntityId<string>;
|
|
104
|
+
/** @deprecated Use EntityId.fromText() instead */
|
|
105
|
+
fromText(value: string): EntityId<string>;
|
|
106
|
+
}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@vytches/ddd-contracts"),r=require("@vytches/ddd-domain-primitives"),e=require("@vytches/ddd-utils");class u{constructor(t){t!==null&&typeof t=="object"?this.value=Object.freeze(t):this.value=t}equals(t){return t?this.value===t.value?!0:typeof this.value=="object"&&this.value!==null?JSON.stringify(this.value)===JSON.stringify(t.value):!1:!1}toString(){return String(this.value)}toJSON(){return this.value}getValue(){return this.value}}const d=new Set;function o(n,t){d.has(n)||(d.add(n),console.warn(`[@vytches/ddd-value-objects] EntityIdFactory.${n}() is deprecated and will be removed in v1.0.0. Use ${t} instead. See https://github.com/vytches/ddd/blob/main/CHANGELOG.md for migration details.`))}class i extends l.EntityId{constructor(t,a){super(t,a)}validate(t){switch(this.getType()){case"uuid":return e.LibUtils.isValidUUID(t);case"integer":return e.LibUtils.isValidInteger(t);case"bigint":return e.LibUtils.isValidBigInt(t);case"text":return e.LibUtils.isValidTextId(t);default:throw new r.InvalidParameterError(`Unsupported IdType: ${this.getType()}`)}}static create(){return new i(e.LibUtils.getUUID(),"uuid")}static createWithRandomUUID(){return i.create()}static fromUUID(t){if(!e.LibUtils.hasValue(t))throw r.MissingValueError.withValue("entity identifier");if(!e.LibUtils.isValidUUID(t))throw r.InvalidParameterError.withParameter("entity identifier");return new i(t,"uuid")}static fromInteger(t){if(!e.LibUtils.isValidInteger(t))throw r.InvalidParameterError.withParameter("entity identifier must be a non-negative integer");return new i(t.toString(),"integer")}static fromBigInt(t){const a=e.LibUtils.normalizeIdToString(t);if(!e.LibUtils.isValidBigInt(a))throw r.InvalidParameterError.withParameter("entity identifier must be a valid bigint");return new i(a,"bigint")}static fromText(t){if(!e.LibUtils.hasValue(t))throw r.MissingValueError.withValue("entity identifier");if(!e.LibUtils.isValidTextId(t))throw r.InvalidParameterError.withParameter("entity identifier contains invalid characters");return new i(t,"text")}static tryFromUUID(t){return e.LibUtils.hasValue(t)?e.LibUtils.isValidUUID(t)?e.Result.ok(new i(t,"uuid")):e.Result.fail(r.InvalidParameterError.withParameter("entity identifier")):e.Result.fail(r.MissingValueError.withValue("entity identifier"))}static tryFromInteger(t){return e.LibUtils.isValidInteger(t)?e.Result.ok(new i(t.toString(),"integer")):e.Result.fail(r.InvalidParameterError.withParameter("entity identifier must be a non-negative integer"))}static tryFromBigInt(t){const a=e.LibUtils.normalizeIdToString(t);return e.LibUtils.isValidBigInt(a)?e.Result.ok(new i(a,"bigint")):e.Result.fail(r.InvalidParameterError.withParameter("entity identifier must be a valid bigint"))}static tryFromText(t){return e.LibUtils.hasValue(t)?e.LibUtils.isValidTextId(t)?e.Result.ok(new i(t,"text")):e.Result.fail(r.InvalidParameterError.withParameter("entity identifier contains invalid characters")):e.Result.fail(r.MissingValueError.withValue("entity identifier"))}}class s{static createWithRandomUUID(){return o("createWithRandomUUID","EntityId.create()"),i.create()}static fromUUID(t){if(o("fromUUID","EntityId.fromUUID()"),!e.LibUtils.hasValue(t))throw r.MissingValueError.withValue("entity identifier");if(!e.LibUtils.isValidUUID(t))throw r.InvalidParameterError.withParameter("entity identifier");return new i(t,"uuid")}static fromInteger(t){if(o("fromInteger","EntityId.fromInteger()"),!e.LibUtils.isValidInteger(t))throw r.InvalidParameterError.withParameter("entity identifier must be a non-negative integer");return new i(t.toString(),"integer")}static fromBigInt(t){o("fromBigInt","EntityId.fromBigInt()");const a=e.LibUtils.normalizeIdToString(t);if(!e.LibUtils.isValidBigInt(a))throw r.InvalidParameterError.withParameter("entity identifier must be a valid bigint");return new i(a,"bigint")}static fromText(t){if(o("fromText","EntityId.fromText()"),!e.LibUtils.hasValue(t))throw r.MissingValueError.withValue("entity identifier");if(!e.LibUtils.isValidTextId(t))throw r.InvalidParameterError.withParameter("entity identifier contains invalid characters");return new i(t,"text")}createWithRandomUUID(){return i.create()}fromUUID(t){return s.fromUUID(t)}fromInteger(t){return s.fromInteger(t)}fromBigInt(t){return s.fromBigInt(t)}fromText(t){return s.fromText(t)}}function f(n){return n}function m(){return i.create()}function c(n){return i.fromUUID(n)}function I(n){return i.fromText(n)}exports.BaseValueObject=u;exports.EntityId=i;exports.EntityIdFactory=s;exports.brandedIdFromText=I;exports.brandedIdFromUUID=c;exports.createBrandedId=f;exports.newBrandedId=m;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { BaseValueObject, type ValueObjectValidator } from './base-value-object';
|
|
2
|
+
export { EntityId, EntityIdFactory } from './id.value-object';
|
|
3
|
+
export type { BrandedId } from './branded-id';
|
|
4
|
+
export { createBrandedId, newBrandedId, brandedIdFromUUID, brandedIdFromText } from './branded-id';
|
|
5
|
+
export type { IEntityId, IEntityIdFactory, IdType } from '@vytches/ddd-contracts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { EntityId as m } from "@vytches/ddd-contracts";
|
|
2
|
+
import { InvalidParameterError as r, MissingValueError as o } from "@vytches/ddd-domain-primitives";
|
|
3
|
+
import { LibUtils as e, Result as s } from "@vytches/ddd-utils";
|
|
4
|
+
class l {
|
|
5
|
+
constructor(t) {
|
|
6
|
+
t !== null && typeof t == "object" ? this.value = Object.freeze(t) : this.value = t;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Compares this value object with another for equality.
|
|
10
|
+
* Uses deep structural comparison for object values, === for primitives.
|
|
11
|
+
* @param valueObject - The value object to compare with
|
|
12
|
+
* @returns True if they are equal, false otherwise
|
|
13
|
+
*/
|
|
14
|
+
equals(t) {
|
|
15
|
+
return t ? this.value === t.value ? !0 : typeof this.value == "object" && this.value !== null ? JSON.stringify(this.value) === JSON.stringify(t.value) : !1 : !1;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Returns a string representation of the value object
|
|
19
|
+
* @returns String representation
|
|
20
|
+
*/
|
|
21
|
+
toString() {
|
|
22
|
+
return String(this.value);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Returns the value for JSON serialization.
|
|
26
|
+
* Returns T (not string) so JSON.stringify works correctly without double-encoding.
|
|
27
|
+
*/
|
|
28
|
+
toJSON() {
|
|
29
|
+
return this.value;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Gets the underlying value as a readonly reference.
|
|
33
|
+
* @returns The underlying value (frozen for object types)
|
|
34
|
+
*/
|
|
35
|
+
getValue() {
|
|
36
|
+
return this.value;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const u = /* @__PURE__ */ new Set();
|
|
40
|
+
function f(n, t) {
|
|
41
|
+
u.has(n) || (u.add(n), console.warn(
|
|
42
|
+
`[@vytches/ddd-value-objects] EntityIdFactory.${n}() is deprecated and will be removed in v1.0.0. Use ${t} instead. See https://github.com/vytches/ddd/blob/main/CHANGELOG.md for migration details.`
|
|
43
|
+
));
|
|
44
|
+
}
|
|
45
|
+
class i extends m {
|
|
46
|
+
constructor(t, a) {
|
|
47
|
+
super(t, a);
|
|
48
|
+
}
|
|
49
|
+
validate(t) {
|
|
50
|
+
switch (this.getType()) {
|
|
51
|
+
case "uuid":
|
|
52
|
+
return e.isValidUUID(t);
|
|
53
|
+
case "integer":
|
|
54
|
+
return e.isValidInteger(t);
|
|
55
|
+
case "bigint":
|
|
56
|
+
return e.isValidBigInt(t);
|
|
57
|
+
case "text":
|
|
58
|
+
return e.isValidTextId(t);
|
|
59
|
+
default:
|
|
60
|
+
throw new r(`Unsupported IdType: ${this.getType()}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Primary factory method - creates a new EntityId with a randomly generated UUID
|
|
65
|
+
* @returns New EntityId with random UUID
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const id = EntityId.create();
|
|
69
|
+
* console.log(id.getValue()); // Returns: '550e8400-e29b-41d4-a716-446655440000'
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
static create() {
|
|
73
|
+
return new i(e.getUUID(), "uuid");
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* @deprecated Use `create()` instead. Will be removed in next major version.
|
|
77
|
+
*/
|
|
78
|
+
static createWithRandomUUID() {
|
|
79
|
+
return i.create();
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* @returns EntityId instance
|
|
83
|
+
* @throws {MissingValueError} if value is empty
|
|
84
|
+
* @throws {InvalidParameterError} if value is not a valid UUID
|
|
85
|
+
*/
|
|
86
|
+
static fromUUID(t) {
|
|
87
|
+
if (!e.hasValue(t))
|
|
88
|
+
throw o.withValue("entity identifier");
|
|
89
|
+
if (!e.isValidUUID(t))
|
|
90
|
+
throw r.withParameter("entity identifier");
|
|
91
|
+
return new i(t, "uuid");
|
|
92
|
+
}
|
|
93
|
+
static fromInteger(t) {
|
|
94
|
+
if (!e.isValidInteger(t))
|
|
95
|
+
throw r.withParameter("entity identifier must be a non-negative integer");
|
|
96
|
+
return new i(t.toString(), "integer");
|
|
97
|
+
}
|
|
98
|
+
static fromBigInt(t) {
|
|
99
|
+
const a = e.normalizeIdToString(t);
|
|
100
|
+
if (!e.isValidBigInt(a))
|
|
101
|
+
throw r.withParameter("entity identifier must be a valid bigint");
|
|
102
|
+
return new i(a, "bigint");
|
|
103
|
+
}
|
|
104
|
+
static fromText(t) {
|
|
105
|
+
if (!e.hasValue(t))
|
|
106
|
+
throw o.withValue("entity identifier");
|
|
107
|
+
if (!e.isValidTextId(t))
|
|
108
|
+
throw r.withParameter("entity identifier contains invalid characters");
|
|
109
|
+
return new i(t, "text");
|
|
110
|
+
}
|
|
111
|
+
// --- Result-returning factory methods (non-throwing) ---
|
|
112
|
+
/**
|
|
113
|
+
* Create EntityId from UUID string, returning Result instead of throwing.
|
|
114
|
+
* @public
|
|
115
|
+
* @stable
|
|
116
|
+
* @since 0.24.0
|
|
117
|
+
*/
|
|
118
|
+
static tryFromUUID(t) {
|
|
119
|
+
return e.hasValue(t) ? e.isValidUUID(t) ? s.ok(new i(t, "uuid")) : s.fail(r.withParameter("entity identifier")) : s.fail(o.withValue("entity identifier"));
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Create EntityId from integer, returning Result instead of throwing.
|
|
123
|
+
* @public
|
|
124
|
+
* @stable
|
|
125
|
+
* @since 0.24.0
|
|
126
|
+
*/
|
|
127
|
+
static tryFromInteger(t) {
|
|
128
|
+
return e.isValidInteger(t) ? s.ok(new i(t.toString(), "integer")) : s.fail(
|
|
129
|
+
r.withParameter("entity identifier must be a non-negative integer")
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Create EntityId from bigint, returning Result instead of throwing.
|
|
134
|
+
* @public
|
|
135
|
+
* @stable
|
|
136
|
+
* @since 0.24.0
|
|
137
|
+
*/
|
|
138
|
+
static tryFromBigInt(t) {
|
|
139
|
+
const a = e.normalizeIdToString(t);
|
|
140
|
+
return e.isValidBigInt(a) ? s.ok(new i(a, "bigint")) : s.fail(
|
|
141
|
+
r.withParameter("entity identifier must be a valid bigint")
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Create EntityId from text, returning Result instead of throwing.
|
|
146
|
+
* @public
|
|
147
|
+
* @stable
|
|
148
|
+
* @since 0.24.0
|
|
149
|
+
*/
|
|
150
|
+
static tryFromText(t) {
|
|
151
|
+
return e.hasValue(t) ? e.isValidTextId(t) ? s.ok(new i(t, "text")) : s.fail(
|
|
152
|
+
r.withParameter("entity identifier contains invalid characters")
|
|
153
|
+
) : s.fail(o.withValue("entity identifier"));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
class d {
|
|
157
|
+
/**
|
|
158
|
+
* @deprecated Use EntityId.create() instead. Will be removed in v1.0.0.
|
|
159
|
+
*/
|
|
160
|
+
static createWithRandomUUID() {
|
|
161
|
+
return f("createWithRandomUUID", "EntityId.create()"), i.create();
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* @deprecated Use EntityId.fromUUID() instead. Will be removed in v1.0.0.
|
|
165
|
+
* @throws {MissingValueError} if value is empty
|
|
166
|
+
* @throws {InvalidParameterError} if value is empty
|
|
167
|
+
*/
|
|
168
|
+
static fromUUID(t) {
|
|
169
|
+
if (f("fromUUID", "EntityId.fromUUID()"), !e.hasValue(t))
|
|
170
|
+
throw o.withValue("entity identifier");
|
|
171
|
+
if (!e.isValidUUID(t))
|
|
172
|
+
throw r.withParameter("entity identifier");
|
|
173
|
+
return new i(t, "uuid");
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* @deprecated Use EntityId.fromInteger() instead. Will be removed in v1.0.0.
|
|
177
|
+
* @throws {InvalidParameterError} if value is empty
|
|
178
|
+
*/
|
|
179
|
+
static fromInteger(t) {
|
|
180
|
+
if (f("fromInteger", "EntityId.fromInteger()"), !e.isValidInteger(t))
|
|
181
|
+
throw r.withParameter("entity identifier must be a non-negative integer");
|
|
182
|
+
return new i(t.toString(), "integer");
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* @deprecated Use EntityId.fromBigInt() instead. Will be removed in v1.0.0.
|
|
186
|
+
* @throws {InvalidParameterError} if value is empty
|
|
187
|
+
*/
|
|
188
|
+
static fromBigInt(t) {
|
|
189
|
+
f("fromBigInt", "EntityId.fromBigInt()");
|
|
190
|
+
const a = e.normalizeIdToString(t);
|
|
191
|
+
if (!e.isValidBigInt(a))
|
|
192
|
+
throw r.withParameter("entity identifier must be a valid bigint");
|
|
193
|
+
return new i(a, "bigint");
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* @deprecated Use EntityId.fromText() instead. Will be removed in v1.0.0.
|
|
197
|
+
* @throws {MissingValueError} if value is empty
|
|
198
|
+
* @throws {InvalidParameterError} if value is empty
|
|
199
|
+
*/
|
|
200
|
+
static fromText(t) {
|
|
201
|
+
if (f("fromText", "EntityId.fromText()"), !e.hasValue(t))
|
|
202
|
+
throw o.withValue("entity identifier");
|
|
203
|
+
if (!e.isValidTextId(t))
|
|
204
|
+
throw r.withParameter("entity identifier contains invalid characters");
|
|
205
|
+
return new i(t, "text");
|
|
206
|
+
}
|
|
207
|
+
/** @deprecated Use EntityId.create() instead */
|
|
208
|
+
createWithRandomUUID() {
|
|
209
|
+
return i.create();
|
|
210
|
+
}
|
|
211
|
+
/** @deprecated Use EntityId.fromUUID() instead */
|
|
212
|
+
fromUUID(t) {
|
|
213
|
+
return d.fromUUID(t);
|
|
214
|
+
}
|
|
215
|
+
/** @deprecated Use EntityId.fromInteger() instead */
|
|
216
|
+
fromInteger(t) {
|
|
217
|
+
return d.fromInteger(t);
|
|
218
|
+
}
|
|
219
|
+
/** @deprecated Use EntityId.fromBigInt() instead */
|
|
220
|
+
fromBigInt(t) {
|
|
221
|
+
return d.fromBigInt(t);
|
|
222
|
+
}
|
|
223
|
+
/** @deprecated Use EntityId.fromText() instead */
|
|
224
|
+
fromText(t) {
|
|
225
|
+
return d.fromText(t);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function w(n) {
|
|
229
|
+
return n;
|
|
230
|
+
}
|
|
231
|
+
function I() {
|
|
232
|
+
return i.create();
|
|
233
|
+
}
|
|
234
|
+
function U(n) {
|
|
235
|
+
return i.fromUUID(n);
|
|
236
|
+
}
|
|
237
|
+
function V(n) {
|
|
238
|
+
return i.fromText(n);
|
|
239
|
+
}
|
|
240
|
+
export {
|
|
241
|
+
l as BaseValueObject,
|
|
242
|
+
i as EntityId,
|
|
243
|
+
d as EntityIdFactory,
|
|
244
|
+
V as brandedIdFromText,
|
|
245
|
+
U as brandedIdFromUUID,
|
|
246
|
+
w as createBrandedId,
|
|
247
|
+
I as newBrandedId
|
|
248
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vytches/ddd-value-objects",
|
|
3
|
+
"version": "0.26.0",
|
|
4
|
+
"description": "Enhanced value objects and EntityId implementations",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LLMGUIDE.md"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"ddd",
|
|
24
|
+
"domain-driven-design",
|
|
25
|
+
"typescript",
|
|
26
|
+
"value, objects"
|
|
27
|
+
],
|
|
28
|
+
"author": "VytchesDDD",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/vytches/ddd.git",
|
|
33
|
+
"directory": "packages/value-objects"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@vytches/ddd-utils": "0.26.0",
|
|
37
|
+
"@vytches/ddd-domain-primitives": "0.26.0",
|
|
38
|
+
"@vytches/ddd-contracts": "0.26.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"typescript": "^5.0.0",
|
|
42
|
+
"vite": "^7.3.2",
|
|
43
|
+
"vite-plugin-dts": "^4.0.0",
|
|
44
|
+
"vitest": "^2.0.0",
|
|
45
|
+
"@vytches/ddd-testing": "0.26.0"
|
|
46
|
+
},
|
|
47
|
+
"nx": {
|
|
48
|
+
"tags": [
|
|
49
|
+
"scope:value-objects",
|
|
50
|
+
"type:lib",
|
|
51
|
+
"layer:foundation"
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"registry": "https://registry.npmjs.org",
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "vite build",
|
|
60
|
+
"dev": "vite build --watch",
|
|
61
|
+
"clean": "rm -rf dist .tsbuildinfo",
|
|
62
|
+
"test": "vitest run",
|
|
63
|
+
"test:watch": "vitest",
|
|
64
|
+
"test:coverage": "vitest run --coverage",
|
|
65
|
+
"lint": "eslint src --ext .ts",
|
|
66
|
+
"type-check": "tsc --noEmit"
|
|
67
|
+
}
|
|
68
|
+
}
|