@longzai-intelligence/shared-kernel 0.0.1 → 0.0.2

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/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`zod`);var t=class{_id;_props;_events=[];constructor(e,t){this._id=e,this._props=t}get id(){return this._id}get props(){return this._props}updateProps(e,t){this._props[e]=t}addEvent(e){this._events.push(e)}pullEvents(){let e=[...this._events];return this._events=[],e}hasEvents(){return this._events.length>0}equals(e){return this._id===e._id}},n=class extends t{_version=1;_isDirty=!1;get version(){return this._version}get isDirty(){return this._isDirty}markAsDirty(){this._isDirty=!0,this._version++}},r=class{static TYPE;eventId;occurredAt;traceContext;constructor(e,t){this.eventId=e,this.occurredAt=new Date,this.traceContext=t}toJSON(){return{eventId:this.eventId,type:this.type,occurredAt:this.occurredAt.toISOString(),traceContext:this.traceContext}}},i=class extends r{type=`entity:created`;constructor(e,t,n){super(e),this.entityType=t,this.entityId=n}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId}}},a=class extends r{type=`entity:updated`;constructor(e,t,n,r){super(e),this.entityType=t,this.entityId=n,this.changes=r}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId,changes:this.changes}}},o=class extends r{type=`entity:deleted`;constructor(e,t,n){super(e),this.entityType=t,this.entityId=n}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId}}},s=class extends Error{constructor(e,t=`DOMAIN_ERROR`,n){super(e),this.code=t,this.context=n,this.name=`DomainError`}toJSON(){return{name:this.name,code:this.code,message:this.message,context:this.context,stack:this.stack}}},c=class extends s{constructor(e,t,n){super(e,`VALIDATION_ERROR`,{field:t,value:n}),this.field=t,this.value=n,this.name=`ValidationError`}},l=class extends s{constructor(e,t){super(e,`BUSINESS_RULE_ERROR`,{rule:t}),this.name=`BusinessRuleError`}},u=class extends s{constructor(e,t){super(`${e} 未找到: ${t}`,`ENTITY_NOT_FOUND`,{entityType:e,entityId:t}),this.entityType=e,this.entityId=t,this.name=`EntityNotFoundError`}},d=class extends s{constructor(e,t,n){super(e,`REPOSITORY_ERROR`,n),this.cause=t,this.name=`RepositoryError`}toJSON(){return{...super.toJSON(),cause:this.cause?.message}}},f=class extends s{constructor(e=`并发冲突,请重试`){super(e,`CONCURRENCY_ERROR`),this.name=`ConcurrencyError`}},p=class extends s{constructor(e,t){super(`无权限执行操作: ${t} on ${e}`,`PERMISSION_DENIED`,{resource:e,action:t}),this.resource=e,this.action=t,this.name=`PermissionDeniedError`}},m=class extends s{constructor(e,t){super(e,`CONFIGURATION_ERROR`,{key:t}),this.key=t,this.name=`ConfigurationError`}},h=class extends s{constructor(e,t,n){super(t,`EXTERNAL_SERVICE_ERROR`,{service:e}),this.service=e,this.name=`ExternalServiceError`,n&&(this.cause=n)}};function g(e){return e instanceof s?e:e instanceof Error?new d(e.message,e):new d(String(e))}function _(e,t,n){(n?t.withOperation(n):t).error(e.message,e,{code:e.code,name:e.name,context:e.context})}function v(e){return[`CONCURRENCY_ERROR`,`EXTERNAL_SERVICE_ERROR`].includes(e.code)}function y(e){return[`VALIDATION_ERROR`,`BUSINESS_RULE_ERROR`,`ENTITY_NOT_FOUND`,`PERMISSION_DENIED`].includes(e.code)}function b(e){return e instanceof c}function x(e){return e instanceof l}function S(e){return e instanceof u}function C(e){return e instanceof d}function w(e){return e instanceof f}function T(e){return e instanceof p}function E(e){return e instanceof m}function D(e){return e instanceof h}const O=Symbol(`UnitOfWork`);var k=class{_value;constructor(e){this._value=Object.freeze(e)}get value(){return this._value}valueOf(){return this._value}toString(){return String(this._value)}},A=class e extends k{constructor(e){super(e)}static fromString(t){if(!t||t.trim().length===0)throw Error(`实体 ID 不能为空`);return new e(t)}equals(e){return this._value===e._value}},j=class e extends k{constructor(e){super(e)}static now(){return new e(Date.now())}static fromDate(t){return new e(t.getTime())}static fromMillis(t){return new e(t)}toDate(){return new Date(this._value)}equals(e){return this._value===e._value}},M=class e extends k{constructor(e){super(e)}static initial(){return new e(0)}static fromNumber(t){if(!Number.isInteger(t)||t<0)throw Error(`版本号必须是非负整数`);return new e(t)}increment(){return new e(this._value+1)}equals(e){return this._value===e._value}isInitial(){return this._value===0}};let N=function(e){return e.LOCAL=`local`,e.PENDING=`pending`,e.SYNCED=`synced`,e.CONFLICT=`conflict`,e}({});var P=class e extends k{constructor(e){super(e)}static local(){return new e(N.LOCAL)}static pending(){return new e(N.PENDING)}static synced(){return new e(N.SYNCED)}static conflict(){return new e(N.CONFLICT)}static fromString(t){switch(t){case`local`:return e.local();case`pending`:return e.pending();case`synced`:return e.synced();case`conflict`:return e.conflict();default:throw Error(`未知的同步状态: ${t}`)}}needsSync(){return this._value===N.LOCAL||this._value===N.PENDING}equals(e){return this._value===e._value}},F=class{isSuccess=!0;isFailure=!1;constructor(e){this.value=e}map(e){return L(e(this.value))}flatMap(e){return e(this.value)}mapError(e){return L(this.value)}getOrElse(e){return this.value}match(e){return e.success(this.value)}},I=class{isSuccess=!1;isFailure=!0;constructor(e){this.error=e}map(e){return R(this.error)}flatMap(e){return R(this.error)}mapError(e){return R(e(this.error))}getOrElse(e){return e}match(e){return e.failure(this.error)}};function L(e){return new F(e)}function R(e){return new I(e)}function z(e){let t=[];for(let n of e){if(n.isFailure)return R(n.error);t.push(n.value)}return L(t)}function B(e){return e.isSuccess}function V(e){return e.isFailure}function H(e){if(e.isFailure)throw g(e.error);return e.value}const U=e.z.string().uuid(`无效的UUID格式`),W=U,G=e.z.number().int().positive(),K=e.z.number().int().nonnegative(),q=e.z.enum([`create`,`update`,`delete`]),J=e.z.enum([`local-wins`,`remote-wins`,`merge`,`manual`]),Y=e.z.object({id:e.z.string(),type:e.z.string()}),X=e.z.object({start:e.z.coerce.date(),end:e.z.coerce.date()}),Z=e.z.object({id:e.z.string(),type:e.z.string(),version:K,data:e.z.record(e.z.string(),e.z.unknown()),timestamp:G}),Q=e.z.object({entityId:e.z.string(),entityType:e.z.string(),changeType:q,data:e.z.record(e.z.string(),e.z.unknown()).optional(),timestamp:G,version:K}),$=e.z.object({id:e.z.string(),entityType:e.z.string(),entityId:e.z.string(),localVersion:K,remoteVersion:K,localData:e.z.record(e.z.string(),e.z.unknown()),remoteData:e.z.record(e.z.string(),e.z.unknown()),conflictedAt:G}),ee=e.z.object({conflictId:e.z.string(),strategy:J,resolvedData:e.z.record(e.z.string(),e.z.unknown()),resolvedAt:G});exports.AggregateRoot=n,exports.AggregateRootEntity=n,exports.BusinessRuleError=l,exports.ChangeTypeSchema=q,exports.ConcurrencyError=f,exports.ConfigurationError=m,exports.ConflictDataSchema=$,exports.ConflictResolutionSchema=ee,exports.ConflictResolutionStrategySchema=J,exports.DomainError=s,exports.DomainEvent=r,exports.Entity=t,exports.EntityChangeSchema=Q,exports.EntityCreatedEvent=i,exports.EntityDeletedEvent=o,exports.EntityIdSchema=W,exports.EntityIdVO=A,exports.EntityNotFoundError=u,exports.EntityUpdatedEvent=a,exports.ExternalServiceError=h,exports.Failure=I,exports.IdReferenceSchema=Y,exports.PermissionDeniedError=p,exports.RepositoryError=d,exports.Success=F,exports.SyncDataSchema=Z,exports.SyncStatus=N,exports.SyncStatusVO=P,exports.TimeRangeSchema=X,exports.TimestampSchema=G,exports.TimestampVO=j,exports.UNIT_OF_WORK_TOKEN=O,exports.UuidSchema=U,exports.ValidationError=c,exports.ValueObject=k,exports.VersionSchema=K,exports.VersionVO=M,exports.combine=z,exports.failure=R,exports.isBusinessRuleError=x,exports.isClientError=y,exports.isConcurrencyError=w,exports.isConfigurationError=E,exports.isEntityNotFoundError=S,exports.isExternalServiceError=D,exports.isFailure=V,exports.isPermissionDeniedError=T,exports.isRepositoryError=C,exports.isRetryableError=v,exports.isSuccess=B,exports.isValidationError=b,exports.logError=_,exports.success=L,exports.toDomainError=g,exports.unwrapResult=H;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`zod`);var t=class{_id;_props;_events=[];constructor(e,t){this._id=e,this._props=t}get id(){return this._id}get props(){return this._props}updateProps(e,t){this._props[e]=t}addEvent(e){this._events.push(e)}pullEvents(){let e=[...this._events];return this._events=[],e}hasEvents(){return this._events.length>0}equals(e){return this._id===e._id}},n=class extends t{_version=1;_isDirty=!1;get version(){return this._version}get isDirty(){return this._isDirty}markAsDirty(){this._isDirty=!0,this._version++}},r=class{static TYPE;eventId;occurredAt;traceContext;constructor(e,t){this.eventId=e,this.occurredAt=new Date,this.traceContext=t}toJSON(){return{eventId:this.eventId,type:this.type,occurredAt:this.occurredAt.toISOString(),traceContext:this.traceContext}}},i=class extends r{type=`entity:created`;constructor(e,t,n){super(e),this.entityType=t,this.entityId=n}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId}}},a=class extends r{type=`entity:updated`;constructor(e,t,n,r){super(e),this.entityType=t,this.entityId=n,this.changes=r}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId,changes:this.changes}}},o=class extends r{type=`entity:deleted`;constructor(e,t,n){super(e),this.entityType=t,this.entityId=n}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId}}},s=class extends Error{constructor(e,t=`DOMAIN_ERROR`,n){super(e),this.code=t,this.context=n,this.name=`DomainError`}toJSON(){return{name:this.name,code:this.code,message:this.message,context:this.context,stack:this.stack}}},c=class extends s{constructor(e,t,n){super(e,`VALIDATION_ERROR`,{field:t,value:n}),this.field=t,this.value=n,this.name=`ValidationError`}},l=class extends s{constructor(e,t){super(e,`BUSINESS_RULE_ERROR`,{rule:t}),this.name=`BusinessRuleError`}},u=class extends s{constructor(e,t){super(`${e} 未找到: ${t}`,`ENTITY_NOT_FOUND`,{entityType:e,entityId:t}),this.entityType=e,this.entityId=t,this.name=`EntityNotFoundError`}},d=class extends s{constructor(e,t,n){super(e,`REPOSITORY_ERROR`,n),this.cause=t,this.name=`RepositoryError`}toJSON(){return{...super.toJSON(),cause:this.cause?.message}}},f=class extends s{constructor(e=`并发冲突,请重试`){super(e,`CONCURRENCY_ERROR`),this.name=`ConcurrencyError`}},p=class extends s{constructor(e,t){super(`无权限执行操作: ${t} on ${e}`,`PERMISSION_DENIED`,{resource:e,action:t}),this.resource=e,this.action=t,this.name=`PermissionDeniedError`}},m=class extends s{constructor(e,t){super(e,`CONFIGURATION_ERROR`,{key:t}),this.key=t,this.name=`ConfigurationError`}},h=class extends s{constructor(e,t,n){super(t,`EXTERNAL_SERVICE_ERROR`,{service:e}),this.service=e,this.name=`ExternalServiceError`,n&&(this.cause=n)}};function g(e){return e instanceof s?e:e instanceof Error?new d(e.message,e):new d(String(e))}function _(e,t,n){(n?t.withOperation(n):t).error(e.message,e,{code:e.code,name:e.name,context:e.context})}function v(e){return[`CONCURRENCY_ERROR`,`EXTERNAL_SERVICE_ERROR`].includes(e.code)}function y(e){return[`VALIDATION_ERROR`,`BUSINESS_RULE_ERROR`,`ENTITY_NOT_FOUND`,`PERMISSION_DENIED`].includes(e.code)}function b(e){return e instanceof c}function x(e){return e instanceof l}function S(e){return e instanceof u}function C(e){return e instanceof d}function w(e){return e instanceof f}function T(e){return e instanceof p}function E(e){return e instanceof m}function D(e){return e instanceof h}const O=Symbol(`UnitOfWork`);var k=class{_value;constructor(e){this._value=Object.freeze(e)}get value(){return this._value}valueOf(){return this._value}toString(){return String(this._value)}},A=class{isSuccess=!0;isFailure=!1;constructor(e){this.value=e}map(e){return M(e(this.value))}flatMap(e){return e(this.value)}mapError(e){return M(this.value)}getOrElse(e){return this.value}match(e){return e.success(this.value)}},j=class{isSuccess=!1;isFailure=!0;constructor(e){this.error=e}map(e){return N(this.error)}flatMap(e){return N(this.error)}mapError(e){return N(e(this.error))}getOrElse(e){return e}match(e){return e.failure(this.error)}};function M(e){return new A(e)}function N(e){return new j(e)}function P(e){let t=[];for(let n of e){if(n.isFailure)return N(n.error);t.push(n.value)}return M(t)}function F(e){return e.isSuccess}function I(e){return e.isFailure}function L(e){if(e.isFailure)throw g(e.error);return e.value}var R=class e extends k{static UUID_V4_REGEX=/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;constructor(e){super(e)}static fromUUID(t){return!t||t.trim().length===0?N(new c(`实体 ID 不能为空`,`id`,t)):this.UUID_V4_REGEX.test(t)?M(new e(t)):N(new c(`无效的 UUID 格式`,`id`,t))}static generate(){return new e(crypto.randomUUID())}static fromString(t){if(!t||t.trim().length===0)throw Error(`实体 ID 不能为空`);return new e(t)}equals(e){return this._value===e._value}},z=class e extends k{constructor(e){super(e)}static now(){return new e(Date.now())}static fromDate(t){return new e(t.getTime())}static fromMillis(t){return new e(t)}toDate(){return new Date(this._value)}equals(e){return this._value===e._value}},B=class e extends k{constructor(e){super(e)}static initial(){return new e(0)}static fromNumber(t){if(!Number.isInteger(t)||t<0)throw Error(`版本号必须是非负整数`);return new e(t)}increment(){return new e(this._value+1)}equals(e){return this._value===e._value}isInitial(){return this._value===0}};let V=function(e){return e.LOCAL=`local`,e.PENDING=`pending`,e.SYNCED=`synced`,e.CONFLICT=`conflict`,e}({});var H=class e extends k{constructor(e){super(e)}static local(){return new e(V.LOCAL)}static pending(){return new e(V.PENDING)}static synced(){return new e(V.SYNCED)}static conflict(){return new e(V.CONFLICT)}static fromString(t){switch(t){case`local`:return e.local();case`pending`:return e.pending();case`synced`:return e.synced();case`conflict`:return e.conflict();default:throw Error(`未知的同步状态: ${t}`)}}needsSync(){return this._value===V.LOCAL||this._value===V.PENDING}equals(e){return this._value===e._value}};const U=e.z.number().int().positive(),W=e.z.number().int().nonnegative(),G=e.z.enum([`create`,`update`,`delete`]),K=e.z.enum([`local-wins`,`remote-wins`,`merge`,`manual`]),q=e.z.object({id:e.z.string(),type:e.z.string()}),J=e.z.object({start:e.z.coerce.date(),end:e.z.coerce.date()}),Y=e.z.object({id:e.z.string(),type:e.z.string(),version:W,data:e.z.record(e.z.string(),e.z.unknown()),timestamp:U}),X=e.z.object({entityId:e.z.string(),entityType:e.z.string(),changeType:G,data:e.z.record(e.z.string(),e.z.unknown()).optional(),timestamp:U,version:W}),Z=e.z.object({id:e.z.string(),entityType:e.z.string(),entityId:e.z.string(),localVersion:W,remoteVersion:W,localData:e.z.record(e.z.string(),e.z.unknown()),remoteData:e.z.record(e.z.string(),e.z.unknown()),conflictedAt:U}),Q=e.z.object({conflictId:e.z.string(),strategy:K,resolvedData:e.z.record(e.z.string(),e.z.unknown()),resolvedAt:U});exports.AggregateRoot=n,exports.AggregateRootEntity=n,exports.BusinessRuleError=l,exports.ChangeTypeSchema=G,exports.ConcurrencyError=f,exports.ConfigurationError=m,exports.ConflictDataSchema=Z,exports.ConflictResolutionSchema=Q,exports.ConflictResolutionStrategySchema=K,exports.DomainError=s,exports.DomainEvent=r,exports.Entity=t,exports.EntityChangeSchema=X,exports.EntityCreatedEvent=i,exports.EntityDeletedEvent=o,exports.EntityIdVO=R,exports.EntityNotFoundError=u,exports.EntityUpdatedEvent=a,exports.ExternalServiceError=h,exports.Failure=j,exports.IdReferenceSchema=q,exports.PermissionDeniedError=p,exports.RepositoryError=d,exports.Success=A,exports.SyncDataSchema=Y,exports.SyncStatus=V,exports.SyncStatusVO=H,exports.TimeRangeSchema=J,exports.TimestampSchema=U,exports.TimestampVO=z,exports.UNIT_OF_WORK_TOKEN=O,exports.ValidationError=c,exports.ValueObject=k,exports.VersionSchema=W,exports.VersionVO=B,exports.combine=P,exports.failure=N,exports.isBusinessRuleError=x,exports.isClientError=y,exports.isConcurrencyError=w,exports.isConfigurationError=E,exports.isEntityNotFoundError=S,exports.isExternalServiceError=D,exports.isFailure=I,exports.isPermissionDeniedError=T,exports.isRepositoryError=C,exports.isRetryableError=v,exports.isSuccess=F,exports.isValidationError=b,exports.logError=_,exports.success=M,exports.toDomainError=g,exports.unwrapResult=L;
package/dist/index.d.cts CHANGED
@@ -251,7 +251,10 @@ declare abstract class ValueObject<T> {
251
251
  //#endregion
252
252
  //#region src/domain/value-object/entity-id.vo.d.ts
253
253
  declare class EntityIdVO extends ValueObject<string> {
254
+ private static readonly UUID_V4_REGEX;
254
255
  private constructor();
256
+ static fromUUID(value: string): Result<EntityIdVO, ValidationError>;
257
+ static generate(): EntityIdVO;
255
258
  static fromString(value: string): EntityIdVO;
256
259
  equals(other: EntityIdVO): boolean;
257
260
  }
@@ -337,8 +340,6 @@ type Reference = {
337
340
  };
338
341
  //#endregion
339
342
  //#region src/domain/schemas/common.schema.d.ts
340
- declare const UuidSchema: z.ZodString;
341
- declare const EntityIdSchema: z.ZodString;
342
343
  declare const TimestampSchema: z.ZodNumber;
343
344
  declare const VersionSchema: z.ZodNumber;
344
345
  declare const ChangeTypeSchema: z.ZodEnum<{
@@ -400,11 +401,9 @@ declare const ConflictResolutionSchema: z.ZodObject<{
400
401
  resolvedData: z.ZodRecord<z.ZodString, z.ZodUnknown>;
401
402
  resolvedAt: z.ZodNumber;
402
403
  }, z.core.$strip>;
403
- type Uuid = z.infer<typeof UuidSchema>;
404
- type EntityId = z.infer<typeof EntityIdSchema>;
405
404
  type Timestamp = z.infer<typeof TimestampSchema>;
406
405
  type Version = z.infer<typeof VersionSchema>;
407
406
  type ChangeType = z.infer<typeof ChangeTypeSchema>;
408
407
  type IdReference = z.infer<typeof IdReferenceSchema>;
409
408
  //#endregion
410
- export { AggregateRootEntity as AggregateRoot, AggregateRootEntity, BusinessRuleError, type ChangeType, ChangeTypeSchema, ConcurrencyError, ConfigurationError, type ConflictData, ConflictDataSchema, type ConflictResolution, ConflictResolutionSchema, type ConflictResolutionStrategy, ConflictResolutionStrategySchema, DomainError, DomainEvent, type DomainEventConstructor, type DomainEventHandler, Entity, type EntityChange, EntityChangeSchema, EntityCreatedEvent, EntityDeletedEvent, type EntityId, EntityIdSchema, EntityIdVO, EntityNotFoundError, EntityUpdatedEvent, type EventConstructor, ExternalServiceError, Failure, type IdReference, IdReferenceSchema, PermissionDeniedError, type QueryOptions, type Reference, type Repository, RepositoryError, type Result, type SortParams, Success, type SyncData, SyncDataSchema, SyncStatus, SyncStatusVO, type TimeRange, TimeRangeSchema, type Timestamp, TimestampSchema, TimestampVO, type TraceContext, type TracingContextManager, UNIT_OF_WORK_TOKEN, type UnitOfWork, type Uuid, UuidSchema, ValidationError, ValueObject, type Version, VersionSchema, VersionVO, combine, failure, isBusinessRuleError, isClientError, isConcurrencyError, isConfigurationError, isEntityNotFoundError, isExternalServiceError, isFailure, isPermissionDeniedError, isRepositoryError, isRetryableError, isSuccess, isValidationError, logError, success, toDomainError, unwrapResult };
409
+ export { AggregateRootEntity as AggregateRoot, AggregateRootEntity, BusinessRuleError, type ChangeType, ChangeTypeSchema, ConcurrencyError, ConfigurationError, type ConflictData, ConflictDataSchema, type ConflictResolution, ConflictResolutionSchema, type ConflictResolutionStrategy, ConflictResolutionStrategySchema, DomainError, DomainEvent, type DomainEventConstructor, type DomainEventHandler, Entity, type EntityChange, EntityChangeSchema, EntityCreatedEvent, EntityDeletedEvent, EntityIdVO, EntityNotFoundError, EntityUpdatedEvent, type EventConstructor, ExternalServiceError, Failure, type IdReference, IdReferenceSchema, PermissionDeniedError, type QueryOptions, type Reference, type Repository, RepositoryError, type Result, type SortParams, Success, type SyncData, SyncDataSchema, SyncStatus, SyncStatusVO, type TimeRange, TimeRangeSchema, type Timestamp, TimestampSchema, TimestampVO, type TraceContext, type TracingContextManager, UNIT_OF_WORK_TOKEN, type UnitOfWork, ValidationError, ValueObject, type Version, VersionSchema, VersionVO, combine, failure, isBusinessRuleError, isClientError, isConcurrencyError, isConfigurationError, isEntityNotFoundError, isExternalServiceError, isFailure, isPermissionDeniedError, isRepositoryError, isRetryableError, isSuccess, isValidationError, logError, success, toDomainError, unwrapResult };
package/dist/index.d.mts CHANGED
@@ -251,7 +251,10 @@ declare abstract class ValueObject<T> {
251
251
  //#endregion
252
252
  //#region src/domain/value-object/entity-id.vo.d.ts
253
253
  declare class EntityIdVO extends ValueObject<string> {
254
+ private static readonly UUID_V4_REGEX;
254
255
  private constructor();
256
+ static fromUUID(value: string): Result<EntityIdVO, ValidationError>;
257
+ static generate(): EntityIdVO;
255
258
  static fromString(value: string): EntityIdVO;
256
259
  equals(other: EntityIdVO): boolean;
257
260
  }
@@ -337,8 +340,6 @@ type Reference = {
337
340
  };
338
341
  //#endregion
339
342
  //#region src/domain/schemas/common.schema.d.ts
340
- declare const UuidSchema: z.ZodString;
341
- declare const EntityIdSchema: z.ZodString;
342
343
  declare const TimestampSchema: z.ZodNumber;
343
344
  declare const VersionSchema: z.ZodNumber;
344
345
  declare const ChangeTypeSchema: z.ZodEnum<{
@@ -400,11 +401,9 @@ declare const ConflictResolutionSchema: z.ZodObject<{
400
401
  resolvedData: z.ZodRecord<z.ZodString, z.ZodUnknown>;
401
402
  resolvedAt: z.ZodNumber;
402
403
  }, z.core.$strip>;
403
- type Uuid = z.infer<typeof UuidSchema>;
404
- type EntityId = z.infer<typeof EntityIdSchema>;
405
404
  type Timestamp = z.infer<typeof TimestampSchema>;
406
405
  type Version = z.infer<typeof VersionSchema>;
407
406
  type ChangeType = z.infer<typeof ChangeTypeSchema>;
408
407
  type IdReference = z.infer<typeof IdReferenceSchema>;
409
408
  //#endregion
410
- export { AggregateRootEntity as AggregateRoot, AggregateRootEntity, BusinessRuleError, type ChangeType, ChangeTypeSchema, ConcurrencyError, ConfigurationError, type ConflictData, ConflictDataSchema, type ConflictResolution, ConflictResolutionSchema, type ConflictResolutionStrategy, ConflictResolutionStrategySchema, DomainError, DomainEvent, type DomainEventConstructor, type DomainEventHandler, Entity, type EntityChange, EntityChangeSchema, EntityCreatedEvent, EntityDeletedEvent, type EntityId, EntityIdSchema, EntityIdVO, EntityNotFoundError, EntityUpdatedEvent, type EventConstructor, ExternalServiceError, Failure, type IdReference, IdReferenceSchema, PermissionDeniedError, type QueryOptions, type Reference, type Repository, RepositoryError, type Result, type SortParams, Success, type SyncData, SyncDataSchema, SyncStatus, SyncStatusVO, type TimeRange, TimeRangeSchema, type Timestamp, TimestampSchema, TimestampVO, type TraceContext, type TracingContextManager, UNIT_OF_WORK_TOKEN, type UnitOfWork, type Uuid, UuidSchema, ValidationError, ValueObject, type Version, VersionSchema, VersionVO, combine, failure, isBusinessRuleError, isClientError, isConcurrencyError, isConfigurationError, isEntityNotFoundError, isExternalServiceError, isFailure, isPermissionDeniedError, isRepositoryError, isRetryableError, isSuccess, isValidationError, logError, success, toDomainError, unwrapResult };
409
+ export { AggregateRootEntity as AggregateRoot, AggregateRootEntity, BusinessRuleError, type ChangeType, ChangeTypeSchema, ConcurrencyError, ConfigurationError, type ConflictData, ConflictDataSchema, type ConflictResolution, ConflictResolutionSchema, type ConflictResolutionStrategy, ConflictResolutionStrategySchema, DomainError, DomainEvent, type DomainEventConstructor, type DomainEventHandler, Entity, type EntityChange, EntityChangeSchema, EntityCreatedEvent, EntityDeletedEvent, EntityIdVO, EntityNotFoundError, EntityUpdatedEvent, type EventConstructor, ExternalServiceError, Failure, type IdReference, IdReferenceSchema, PermissionDeniedError, type QueryOptions, type Reference, type Repository, RepositoryError, type Result, type SortParams, Success, type SyncData, SyncDataSchema, SyncStatus, SyncStatusVO, type TimeRange, TimeRangeSchema, type Timestamp, TimestampSchema, TimestampVO, type TraceContext, type TracingContextManager, UNIT_OF_WORK_TOKEN, type UnitOfWork, ValidationError, ValueObject, type Version, VersionSchema, VersionVO, combine, failure, isBusinessRuleError, isClientError, isConcurrencyError, isConfigurationError, isEntityNotFoundError, isExternalServiceError, isFailure, isPermissionDeniedError, isRepositoryError, isRetryableError, isSuccess, isValidationError, logError, success, toDomainError, unwrapResult };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{z as e}from"zod";var t=class{_id;_props;_events=[];constructor(e,t){this._id=e,this._props=t}get id(){return this._id}get props(){return this._props}updateProps(e,t){this._props[e]=t}addEvent(e){this._events.push(e)}pullEvents(){let e=[...this._events];return this._events=[],e}hasEvents(){return this._events.length>0}equals(e){return this._id===e._id}},n=class extends t{_version=1;_isDirty=!1;get version(){return this._version}get isDirty(){return this._isDirty}markAsDirty(){this._isDirty=!0,this._version++}},r=class{static TYPE;eventId;occurredAt;traceContext;constructor(e,t){this.eventId=e,this.occurredAt=new Date,this.traceContext=t}toJSON(){return{eventId:this.eventId,type:this.type,occurredAt:this.occurredAt.toISOString(),traceContext:this.traceContext}}},i=class extends r{type=`entity:created`;constructor(e,t,n){super(e),this.entityType=t,this.entityId=n}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId}}},a=class extends r{type=`entity:updated`;constructor(e,t,n,r){super(e),this.entityType=t,this.entityId=n,this.changes=r}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId,changes:this.changes}}},o=class extends r{type=`entity:deleted`;constructor(e,t,n){super(e),this.entityType=t,this.entityId=n}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId}}},s=class extends Error{constructor(e,t=`DOMAIN_ERROR`,n){super(e),this.code=t,this.context=n,this.name=`DomainError`}toJSON(){return{name:this.name,code:this.code,message:this.message,context:this.context,stack:this.stack}}},c=class extends s{constructor(e,t,n){super(e,`VALIDATION_ERROR`,{field:t,value:n}),this.field=t,this.value=n,this.name=`ValidationError`}},l=class extends s{constructor(e,t){super(e,`BUSINESS_RULE_ERROR`,{rule:t}),this.name=`BusinessRuleError`}},u=class extends s{constructor(e,t){super(`${e} 未找到: ${t}`,`ENTITY_NOT_FOUND`,{entityType:e,entityId:t}),this.entityType=e,this.entityId=t,this.name=`EntityNotFoundError`}},d=class extends s{constructor(e,t,n){super(e,`REPOSITORY_ERROR`,n),this.cause=t,this.name=`RepositoryError`}toJSON(){return{...super.toJSON(),cause:this.cause?.message}}},f=class extends s{constructor(e=`并发冲突,请重试`){super(e,`CONCURRENCY_ERROR`),this.name=`ConcurrencyError`}},p=class extends s{constructor(e,t){super(`无权限执行操作: ${t} on ${e}`,`PERMISSION_DENIED`,{resource:e,action:t}),this.resource=e,this.action=t,this.name=`PermissionDeniedError`}},m=class extends s{constructor(e,t){super(e,`CONFIGURATION_ERROR`,{key:t}),this.key=t,this.name=`ConfigurationError`}},h=class extends s{constructor(e,t,n){super(t,`EXTERNAL_SERVICE_ERROR`,{service:e}),this.service=e,this.name=`ExternalServiceError`,n&&(this.cause=n)}};function g(e){return e instanceof s?e:e instanceof Error?new d(e.message,e):new d(String(e))}function _(e,t,n){(n?t.withOperation(n):t).error(e.message,e,{code:e.code,name:e.name,context:e.context})}function v(e){return[`CONCURRENCY_ERROR`,`EXTERNAL_SERVICE_ERROR`].includes(e.code)}function y(e){return[`VALIDATION_ERROR`,`BUSINESS_RULE_ERROR`,`ENTITY_NOT_FOUND`,`PERMISSION_DENIED`].includes(e.code)}function b(e){return e instanceof c}function x(e){return e instanceof l}function S(e){return e instanceof u}function C(e){return e instanceof d}function w(e){return e instanceof f}function T(e){return e instanceof p}function E(e){return e instanceof m}function D(e){return e instanceof h}const O=Symbol(`UnitOfWork`);var k=class{_value;constructor(e){this._value=Object.freeze(e)}get value(){return this._value}valueOf(){return this._value}toString(){return String(this._value)}},A=class e extends k{constructor(e){super(e)}static fromString(t){if(!t||t.trim().length===0)throw Error(`实体 ID 不能为空`);return new e(t)}equals(e){return this._value===e._value}},j=class e extends k{constructor(e){super(e)}static now(){return new e(Date.now())}static fromDate(t){return new e(t.getTime())}static fromMillis(t){return new e(t)}toDate(){return new Date(this._value)}equals(e){return this._value===e._value}},M=class e extends k{constructor(e){super(e)}static initial(){return new e(0)}static fromNumber(t){if(!Number.isInteger(t)||t<0)throw Error(`版本号必须是非负整数`);return new e(t)}increment(){return new e(this._value+1)}equals(e){return this._value===e._value}isInitial(){return this._value===0}};let N=function(e){return e.LOCAL=`local`,e.PENDING=`pending`,e.SYNCED=`synced`,e.CONFLICT=`conflict`,e}({});var P=class e extends k{constructor(e){super(e)}static local(){return new e(N.LOCAL)}static pending(){return new e(N.PENDING)}static synced(){return new e(N.SYNCED)}static conflict(){return new e(N.CONFLICT)}static fromString(t){switch(t){case`local`:return e.local();case`pending`:return e.pending();case`synced`:return e.synced();case`conflict`:return e.conflict();default:throw Error(`未知的同步状态: ${t}`)}}needsSync(){return this._value===N.LOCAL||this._value===N.PENDING}equals(e){return this._value===e._value}},F=class{isSuccess=!0;isFailure=!1;constructor(e){this.value=e}map(e){return L(e(this.value))}flatMap(e){return e(this.value)}mapError(e){return L(this.value)}getOrElse(e){return this.value}match(e){return e.success(this.value)}},I=class{isSuccess=!1;isFailure=!0;constructor(e){this.error=e}map(e){return R(this.error)}flatMap(e){return R(this.error)}mapError(e){return R(e(this.error))}getOrElse(e){return e}match(e){return e.failure(this.error)}};function L(e){return new F(e)}function R(e){return new I(e)}function z(e){let t=[];for(let n of e){if(n.isFailure)return R(n.error);t.push(n.value)}return L(t)}function B(e){return e.isSuccess}function V(e){return e.isFailure}function H(e){if(e.isFailure)throw g(e.error);return e.value}const U=e.string().uuid(`无效的UUID格式`),W=U,G=e.number().int().positive(),K=e.number().int().nonnegative(),q=e.enum([`create`,`update`,`delete`]),J=e.enum([`local-wins`,`remote-wins`,`merge`,`manual`]),Y=e.object({id:e.string(),type:e.string()}),X=e.object({start:e.coerce.date(),end:e.coerce.date()}),Z=e.object({id:e.string(),type:e.string(),version:K,data:e.record(e.string(),e.unknown()),timestamp:G}),Q=e.object({entityId:e.string(),entityType:e.string(),changeType:q,data:e.record(e.string(),e.unknown()).optional(),timestamp:G,version:K}),$=e.object({id:e.string(),entityType:e.string(),entityId:e.string(),localVersion:K,remoteVersion:K,localData:e.record(e.string(),e.unknown()),remoteData:e.record(e.string(),e.unknown()),conflictedAt:G}),ee=e.object({conflictId:e.string(),strategy:J,resolvedData:e.record(e.string(),e.unknown()),resolvedAt:G});export{n as AggregateRoot,n as AggregateRootEntity,l as BusinessRuleError,q as ChangeTypeSchema,f as ConcurrencyError,m as ConfigurationError,$ as ConflictDataSchema,ee as ConflictResolutionSchema,J as ConflictResolutionStrategySchema,s as DomainError,r as DomainEvent,t as Entity,Q as EntityChangeSchema,i as EntityCreatedEvent,o as EntityDeletedEvent,W as EntityIdSchema,A as EntityIdVO,u as EntityNotFoundError,a as EntityUpdatedEvent,h as ExternalServiceError,I as Failure,Y as IdReferenceSchema,p as PermissionDeniedError,d as RepositoryError,F as Success,Z as SyncDataSchema,N as SyncStatus,P as SyncStatusVO,X as TimeRangeSchema,G as TimestampSchema,j as TimestampVO,O as UNIT_OF_WORK_TOKEN,U as UuidSchema,c as ValidationError,k as ValueObject,K as VersionSchema,M as VersionVO,z as combine,R as failure,x as isBusinessRuleError,y as isClientError,w as isConcurrencyError,E as isConfigurationError,S as isEntityNotFoundError,D as isExternalServiceError,V as isFailure,T as isPermissionDeniedError,C as isRepositoryError,v as isRetryableError,B as isSuccess,b as isValidationError,_ as logError,L as success,g as toDomainError,H as unwrapResult};
1
+ import{z as e}from"zod";var t=class{_id;_props;_events=[];constructor(e,t){this._id=e,this._props=t}get id(){return this._id}get props(){return this._props}updateProps(e,t){this._props[e]=t}addEvent(e){this._events.push(e)}pullEvents(){let e=[...this._events];return this._events=[],e}hasEvents(){return this._events.length>0}equals(e){return this._id===e._id}},n=class extends t{_version=1;_isDirty=!1;get version(){return this._version}get isDirty(){return this._isDirty}markAsDirty(){this._isDirty=!0,this._version++}},r=class{static TYPE;eventId;occurredAt;traceContext;constructor(e,t){this.eventId=e,this.occurredAt=new Date,this.traceContext=t}toJSON(){return{eventId:this.eventId,type:this.type,occurredAt:this.occurredAt.toISOString(),traceContext:this.traceContext}}},i=class extends r{type=`entity:created`;constructor(e,t,n){super(e),this.entityType=t,this.entityId=n}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId}}},a=class extends r{type=`entity:updated`;constructor(e,t,n,r){super(e),this.entityType=t,this.entityId=n,this.changes=r}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId,changes:this.changes}}},o=class extends r{type=`entity:deleted`;constructor(e,t,n){super(e),this.entityType=t,this.entityId=n}toJSON(){return{...super.toJSON(),entityType:this.entityType,entityId:this.entityId}}},s=class extends Error{constructor(e,t=`DOMAIN_ERROR`,n){super(e),this.code=t,this.context=n,this.name=`DomainError`}toJSON(){return{name:this.name,code:this.code,message:this.message,context:this.context,stack:this.stack}}},c=class extends s{constructor(e,t,n){super(e,`VALIDATION_ERROR`,{field:t,value:n}),this.field=t,this.value=n,this.name=`ValidationError`}},l=class extends s{constructor(e,t){super(e,`BUSINESS_RULE_ERROR`,{rule:t}),this.name=`BusinessRuleError`}},u=class extends s{constructor(e,t){super(`${e} 未找到: ${t}`,`ENTITY_NOT_FOUND`,{entityType:e,entityId:t}),this.entityType=e,this.entityId=t,this.name=`EntityNotFoundError`}},d=class extends s{constructor(e,t,n){super(e,`REPOSITORY_ERROR`,n),this.cause=t,this.name=`RepositoryError`}toJSON(){return{...super.toJSON(),cause:this.cause?.message}}},f=class extends s{constructor(e=`并发冲突,请重试`){super(e,`CONCURRENCY_ERROR`),this.name=`ConcurrencyError`}},p=class extends s{constructor(e,t){super(`无权限执行操作: ${t} on ${e}`,`PERMISSION_DENIED`,{resource:e,action:t}),this.resource=e,this.action=t,this.name=`PermissionDeniedError`}},m=class extends s{constructor(e,t){super(e,`CONFIGURATION_ERROR`,{key:t}),this.key=t,this.name=`ConfigurationError`}},h=class extends s{constructor(e,t,n){super(t,`EXTERNAL_SERVICE_ERROR`,{service:e}),this.service=e,this.name=`ExternalServiceError`,n&&(this.cause=n)}};function g(e){return e instanceof s?e:e instanceof Error?new d(e.message,e):new d(String(e))}function _(e,t,n){(n?t.withOperation(n):t).error(e.message,e,{code:e.code,name:e.name,context:e.context})}function v(e){return[`CONCURRENCY_ERROR`,`EXTERNAL_SERVICE_ERROR`].includes(e.code)}function y(e){return[`VALIDATION_ERROR`,`BUSINESS_RULE_ERROR`,`ENTITY_NOT_FOUND`,`PERMISSION_DENIED`].includes(e.code)}function b(e){return e instanceof c}function x(e){return e instanceof l}function S(e){return e instanceof u}function C(e){return e instanceof d}function w(e){return e instanceof f}function T(e){return e instanceof p}function E(e){return e instanceof m}function D(e){return e instanceof h}const O=Symbol(`UnitOfWork`);var k=class{_value;constructor(e){this._value=Object.freeze(e)}get value(){return this._value}valueOf(){return this._value}toString(){return String(this._value)}},A=class{isSuccess=!0;isFailure=!1;constructor(e){this.value=e}map(e){return M(e(this.value))}flatMap(e){return e(this.value)}mapError(e){return M(this.value)}getOrElse(e){return this.value}match(e){return e.success(this.value)}},j=class{isSuccess=!1;isFailure=!0;constructor(e){this.error=e}map(e){return N(this.error)}flatMap(e){return N(this.error)}mapError(e){return N(e(this.error))}getOrElse(e){return e}match(e){return e.failure(this.error)}};function M(e){return new A(e)}function N(e){return new j(e)}function P(e){let t=[];for(let n of e){if(n.isFailure)return N(n.error);t.push(n.value)}return M(t)}function F(e){return e.isSuccess}function I(e){return e.isFailure}function L(e){if(e.isFailure)throw g(e.error);return e.value}var R=class e extends k{static UUID_V4_REGEX=/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;constructor(e){super(e)}static fromUUID(t){return!t||t.trim().length===0?N(new c(`实体 ID 不能为空`,`id`,t)):this.UUID_V4_REGEX.test(t)?M(new e(t)):N(new c(`无效的 UUID 格式`,`id`,t))}static generate(){return new e(crypto.randomUUID())}static fromString(t){if(!t||t.trim().length===0)throw Error(`实体 ID 不能为空`);return new e(t)}equals(e){return this._value===e._value}},z=class e extends k{constructor(e){super(e)}static now(){return new e(Date.now())}static fromDate(t){return new e(t.getTime())}static fromMillis(t){return new e(t)}toDate(){return new Date(this._value)}equals(e){return this._value===e._value}},B=class e extends k{constructor(e){super(e)}static initial(){return new e(0)}static fromNumber(t){if(!Number.isInteger(t)||t<0)throw Error(`版本号必须是非负整数`);return new e(t)}increment(){return new e(this._value+1)}equals(e){return this._value===e._value}isInitial(){return this._value===0}};let V=function(e){return e.LOCAL=`local`,e.PENDING=`pending`,e.SYNCED=`synced`,e.CONFLICT=`conflict`,e}({});var H=class e extends k{constructor(e){super(e)}static local(){return new e(V.LOCAL)}static pending(){return new e(V.PENDING)}static synced(){return new e(V.SYNCED)}static conflict(){return new e(V.CONFLICT)}static fromString(t){switch(t){case`local`:return e.local();case`pending`:return e.pending();case`synced`:return e.synced();case`conflict`:return e.conflict();default:throw Error(`未知的同步状态: ${t}`)}}needsSync(){return this._value===V.LOCAL||this._value===V.PENDING}equals(e){return this._value===e._value}};const U=e.number().int().positive(),W=e.number().int().nonnegative(),G=e.enum([`create`,`update`,`delete`]),K=e.enum([`local-wins`,`remote-wins`,`merge`,`manual`]),q=e.object({id:e.string(),type:e.string()}),J=e.object({start:e.coerce.date(),end:e.coerce.date()}),Y=e.object({id:e.string(),type:e.string(),version:W,data:e.record(e.string(),e.unknown()),timestamp:U}),X=e.object({entityId:e.string(),entityType:e.string(),changeType:G,data:e.record(e.string(),e.unknown()).optional(),timestamp:U,version:W}),Z=e.object({id:e.string(),entityType:e.string(),entityId:e.string(),localVersion:W,remoteVersion:W,localData:e.record(e.string(),e.unknown()),remoteData:e.record(e.string(),e.unknown()),conflictedAt:U}),Q=e.object({conflictId:e.string(),strategy:K,resolvedData:e.record(e.string(),e.unknown()),resolvedAt:U});export{n as AggregateRoot,n as AggregateRootEntity,l as BusinessRuleError,G as ChangeTypeSchema,f as ConcurrencyError,m as ConfigurationError,Z as ConflictDataSchema,Q as ConflictResolutionSchema,K as ConflictResolutionStrategySchema,s as DomainError,r as DomainEvent,t as Entity,X as EntityChangeSchema,i as EntityCreatedEvent,o as EntityDeletedEvent,R as EntityIdVO,u as EntityNotFoundError,a as EntityUpdatedEvent,h as ExternalServiceError,j as Failure,q as IdReferenceSchema,p as PermissionDeniedError,d as RepositoryError,A as Success,Y as SyncDataSchema,V as SyncStatus,H as SyncStatusVO,J as TimeRangeSchema,U as TimestampSchema,z as TimestampVO,O as UNIT_OF_WORK_TOKEN,c as ValidationError,k as ValueObject,W as VersionSchema,B as VersionVO,P as combine,N as failure,x as isBusinessRuleError,y as isClientError,w as isConcurrencyError,E as isConfigurationError,S as isEntityNotFoundError,D as isExternalServiceError,I as isFailure,T as isPermissionDeniedError,C as isRepositoryError,v as isRetryableError,F as isSuccess,b as isValidationError,_ as logError,M as success,g as toDomainError,L as unwrapResult};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@longzai-intelligence/shared-kernel",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.mts",
6
6
  "main": "./dist/index.cjs",
@@ -45,7 +45,7 @@
45
45
  "rimraf": "^6.1.3",
46
46
  "typescript": "^6.0.2",
47
47
  "vitest": "^4.1.3",
48
- "@longzai-intelligence/eslint-preset-library": "0.0.3",
48
+ "@longzai-intelligence/eslint-preset-library": "0.0.4",
49
49
  "@longzai-intelligence/vitest-config": "0.0.3",
50
50
  "@longzai-intelligence/typescript-config": "0.0.2",
51
51
  "tsdown": "^0.21.7",