UNPKG

domain-objects

Version:

A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.

43 lines 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUpdatableProperties = void 0; const error_fns_1 = require("@ehmpathy/error-fns"); const type_fns_1 = require("type-fns"); const assertDomainObjectIsSafeToManipulate_1 = require("../constraints/assertDomainObjectIsSafeToManipulate"); const DomainEntity_1 = require("../instantiation/DomainEntity"); const DomainObject_1 = require("../instantiation/DomainObject"); const DomainEntityUpdatablePropertiesMustBeDefinedError_1 = require("./DomainEntityUpdatablePropertiesMustBeDefinedError"); /** * Extracts an object that contains only the updatable properties of the domain object, for DomainEntity * * Uses the definition of a DomainEntity in order to extract the properties that are updatable for the domain object generically * * note * - this should not be called on DomainLiterals or DomainEvents, since by definition they can not have updatable properties */ const getUpdatableProperties = (dobj) => { var _a; // make sure its an instance of DomainObject if (!(dobj instanceof DomainObject_1.DomainObject)) throw new Error('getUpdatableProperties called on object that is not an instance of a DomainObject. Are you sure you instantiated the object? (Related: see `DomainObject.nested`)'); // make sure that its safe to manipulate (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(dobj); // handle DomainEntity if (dobj instanceof DomainEntity_1.DomainEntity) { const className = dobj.constructor.name; const updatableProps = dobj.constructor.updatable; if (!updatableProps) throw new DomainEntityUpdatablePropertiesMustBeDefinedError_1.DomainEntityUpdatablePropertiesMustBeDefinedError({ entityName: className, nameOfFunctionNeededFor: 'getUpdatableProperties', }); return (0, type_fns_1.pick)(dobj, updatableProps.flat()); } // throw error we get here, this is unexpected throw new error_fns_1.UnexpectedCodePathError('unexpected domain object type for getUpdatableProperties. expected DomainEntity', { dobjClass: (_a = dobj === null || dobj === void 0 ? void 0 : dobj.constructor) === null || _a === void 0 ? void 0 : _a.name, dobj, }); }; exports.getUpdatableProperties = getUpdatableProperties; //# sourceMappingURL=getUpdatableProperties.js.map