@cosmicmind/domainjs
Version:
A domain-driven design framework for scalable systems.
2 lines (1 loc) • 4.12 kB
JavaScript
;var e=Object.defineProperty,r=(r,t)=>e(r,"name",{value:t,configurable:!0});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@cosmicmind/foundationjs"),n=require("@cosmicmind/patternjs"),i=class extends t.FoundationError{};r(i,"EntityError");let s=i;const o=r((e={})=>r=>d(r,e),"defineEntity");function a(e){return{set(r,t,n){const i=e.properties?.[t];!1===i?.validator?.(n,r)&&c(`${JSON.stringify(r)} ${JSON.stringify(t)} is invalid`,e),i?.updated?.(n,r[t],r);const s=Reflect.set(r,t,n);return e.trace?.(r),s}}}function c(e,r){const t=new s(e);throw r.error?.(t),t}function d(e,r={}){if(t.guard(e)){const n=r.properties;if(t.guard(n)){const i=new Proxy(e,a(r));for(const[s,o]of Object.entries(n))o.required?(s in e||c(`${JSON.stringify(e)} ${s} is required`,r),!1===o.validator?.(e[s],i)&&c(`${JSON.stringify(e)} ${s} is invalid`,r)):s in e&&void 0!==e[s]&&t.guard(o,"validator")&&!1===o.validator?.(e[s],i)&&c(`${JSON.stringify(e)} ${s} is invalid`,r);return r.created?.(i),r.trace?.(i),i}}c(`${JSON.stringify(e)} is invalid`,r)}r(a,"makeEntityHandler"),r(c,"throwError$2"),r(d,"makeEntity");const l=class extends n.Observable{};r(l,"Observable");let u=l;const f=class extends u{};r(f,"EventObservable");let g=f;const v=class extends t.FoundationError{};r(v,"EventError");let p=v;const y=r((e={})=>r=>x(r,e),"defineEvent");function E(e){return{set(){$("cannot modify event properties",e)}}}function $(e,r){const t=new p(e);throw r.error?.(t),t}function x(e,r={}){if(t.guard(e)){const n=r.properties;if(t.guard(n)){const i=new Proxy(e,E(r));for(const[s,o]of Object.entries(n))o.required?(s in e||$(`${JSON.stringify(e)} ${s} is required`,r),!1===o.validator?.(e[s],i)&&$(`${JSON.stringify(e)} ${s} is invalid`,r)):s in e&&void 0!==e[s]&&t.guard(o,"validator")&&!1===o.validator?.(e[s],i)&&$(`${JSON.stringify(e)} ${s} is invalid`,r);return r.created?.(i),i}}throw new p(`${String(e)} is invalid`)}r(E,"makeEventHandler"),r($,"throwError$1"),r(x,"makeEvent");const O=class extends g{root;constructor(e){super(),this.root="function"==typeof this.prepare?this.prepare(e):e}};r(O,"Aggregate");let w=O;function b(e,r={}){const t=o(r);return r=>new e(t(r))}r(b,"defineAggregate");const S=class extends u{};r(S,"MessageObservable");let h=S;const m=class extends t.FoundationError{};r(m,"MessageError");let J=m;const N=r((e={})=>r=>V(r,e),"defineMessage");function M(e){return{set(){q("cannot modify message properties",e)}}}function q(e,r){const t=new J(e);throw r.error?.(t),t}function V(e,r={}){if(t.guard(e)){const n=r.properties;if(t.guard(n)){const i=new Proxy(e,M(r));for(const[s,o]of Object.entries(n))o.required?(s in e||q(`${JSON.stringify(e)} ${s} is required`,r),!1===o.validator?.(e[s],i)&&q(`${JSON.stringify(e)} ${s} is invalid`,r)):s in e&&void 0!==e[s]&&t.guard(o,"validator")&&!1===o.validator?.(e[s],i)&&q(`${JSON.stringify(e)} ${s} is invalid`,r);return r.created?.(i),i}}throw new J(`${String(e)} is invalid`)}r(M,"makeMessageHandler"),r(q,"throwError"),r(V,"makeMessage");const j=class{_value;get value(){return this._value}constructor(e){this._value="function"==typeof this.prepare?this.prepare(e):e}};r(j,"Value");let k=j;const P=class extends t.FoundationError{};r(P,"ValueError");let A=P;const F=r((e,r={})=>t=>R(new e(t),t,r),"defineValue");function H(e){return{set(r,t,n){if("value"===t&&!1===e.validator?.(n,r))throw new A(`${String(t)} is invalid`);return Reflect.set(r,t,n)}}}function _(e,r){const t=new A(e);throw r.error?.(t),t}function R(e,r,n={}){if(t.guard(e)){const t=new Proxy(e,H(n));if(!1===n.validator?.(r,t)){_(`${e.constructor.name} is invalid: ${JSON.stringify(r)}`,n)}return n.created?.(t),n.trace?.(t),t}_(`${JSON.stringify(e)} is invalid`,n)}r(H,"createValueHandler"),r(_,"throwErrorAndTrace"),r(R,"createValue"),exports.Aggregate=w,exports.EntityError=s,exports.EventError=p,exports.EventObservable=g,exports.MessageError=J,exports.MessageObservable=h,exports.Observable=u,exports.Value=k,exports.ValueError=A,exports.defineAggregate=b,exports.defineEntity=o,exports.defineEvent=y,exports.defineMessage=N,exports.defineValue=F;