import ValueObject from "./ValueObject"; /** * Represents a universally unique identifier for aggregates and entities. * * Identity is a Value Object that encapsulates aggregate root and entity identifiers. * It ensures that all IDs are valid UUIDs and provides type safety over plain strings. * * @example * ```typescript * // Create a new unique identity * const id1 = Identity.generate(); * * // Create from an existing UUID string * const id2 = Identity.fromString("550e8400-e29b-41d4-a716-446655440000"); * * // Use in aggregate * class Order extends EventSourcedAggregateRoot { * constructor(id: Identity) { * super(id.toString()); * } * } * * // Equality comparison * const id3 = Identity.fromString("550e8400-e29b-41d4-a716-446655440000"); * console.log(id2.equals(id3)); // true * ``` */ export default class Identity extends ValueObject<{ value: string; }> { private readonly value; private static readonly UUID_REGEX; private constructor(); /** * Generates a new unique identity using UUID v4. * * @returns A new Identity instance */ static generate(): Identity; /** * Generates a UUID v4 compatible string using crypto.randomBytes. * Fallback for environments without crypto.randomUUID. * * @returns A UUID v4 string */ private static generateUUIDv4; /** * Creates an Identity from an existing UUID string. * * @param uuid - A valid UUID string * @returns An Identity instance * @throws {Error} When the UUID format is invalid */ static fromString(uuid: string): Identity; /** * Returns the UUID as a string. * This method is used for backward compatibility with code expecting strings. * * @returns The UUID string */ toString(): string; /** * Returns the UUID value. * Alias for toString() for clearer intent when extracting the raw value. * * @returns The UUID string */ getValue(): string; /** * Validates that the value is a properly formatted UUID. * * @throws {Error} When the UUID format is invalid */ protected validate(): void; /** * Returns the components used for equality comparison. * * @returns An iterable containing the UUID value */ protected getEqualityComponents(): Iterable; }