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.13 kB
"use strict"; /** biome-ignore-all lint/suspicious/noExplicitAny: in this file, we depend on `any` to make it generic enough to reuse */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Ref = void 0; const refByUnique_1 = require("../reference/refByUnique"); const DomainLiteral_1 = require("./DomainLiteral"); const isOfDomainEntity_1 = require("./inherit/isOfDomainEntity"); const isOfDomainEvent_1 = require("./inherit/isOfDomainEvent"); // extend the domain literal into a custom class, so that we can rename it into Ref, without mutating the global DomainLiteral class class RefBase extends DomainLiteral_1.DomainLiteral { constructor(props) { // if props itself is a domain entity or domain event, extract its reference (using unique keys as the default) if ((0, isOfDomainEntity_1.isOfDomainEntity)(props) || (0, isOfDomainEvent_1.isOfDomainEvent)(props)) { super((0, refByUnique_1.refByUnique)(props)); return; } // if props is not itself a domain entity/event, check each property // and apply refByUnique on any nested domain entities/events const transformedProps = {}; for (const key of Object.keys(props)) { const value = props[key]; if (value && typeof value === 'object' && ((0, isOfDomainEntity_1.isOfDomainEntity)(value) || (0, isOfDomainEvent_1.isOfDomainEvent)(value))) { transformedProps[key] = exports.Ref.as(value); } else { transformedProps[key] = value; } } super(transformedProps); } } RefBase.metadata = []; // create a constructor for Ref, so that we can instantiate references // todo: actually use a class and extend DomainLiteral, when typescript supports more specialized types for classes; for now, we create a constructor via a procedure exports.Ref = RefBase; // overwrite the constructor name to 'Ref', to keep things consistent Object.defineProperty(RefBase, 'name', { value: 'Ref', writable: false, }); //# sourceMappingURL=Ref.js.map