UNPKG

@sap-cloud-sdk/core

Version:
113 lines 4.86 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntityBuilder = void 0; var util_1 = require("@sap-cloud-sdk/util"); var properties_util_1 = require("./properties-util"); var logger = (0, util_1.createLogger)({ package: 'core', messageContext: 'entity-builder' }); /** * @hidden */ var EntityBuilder = /** @class */ (function () { function EntityBuilder(_entityConstructor) { this._entityConstructor = _entityConstructor; if (!this.entity) { this.entity = new this._entityConstructor(); } } /** * Sets the custom fields for the entity. * @param customFields - The custom fields you want to add. * @returns The entity builder itself for method chaining */ EntityBuilder.prototype.withCustomFields = function (customFields) { var validCustomFields = this.filterCustomFields(customFields); this.entity = this.entity.setCustomFields(validCustomFields); return this; }; /** * Builds the entity. * @returns The entity. */ EntityBuilder.prototype.build = function () { var entity = this.entity; this.entity = new this._entityConstructor(); return entity; }; /** * Builds an entity from JSON representation. * If you have obtained the JSON as a request payload use the [[deserializeEntity]] methods. * Note that fields not mappable to a field in the target entity are silently ignored. * @param json - Representation of the entity in JSON format. * @returns Entity constructed from JSON representation. */ EntityBuilder.prototype.fromJson = function (json) { var entityBuilder = this._entityConstructor.builder(); var entityConstructor = this._entityConstructor; var _a = (0, util_1.partition)(Object.entries(json), function (_a) { var key = _a[0]; return typeof entityBuilder[key] === 'function'; }), entityEntries = _a[0], customEntries = _a[1]; entityEntries.forEach(function (_a) { var key = _a[0], value = _a[1]; var propertyValue = (0, properties_util_1.isNavigationProperty)(key, entityConstructor) && !!value ? buildNavigationPropertyFromJson(key, value, entityConstructor) : value; entityBuilder[key](propertyValue); }); var customFields = customEntries.reduce(function (customFieldsObj, _a) { var _b; var key = _a[0], value = _a[1]; if (key === '_customFields') { logger.warn("Setting custom fields in 'fromJson' through '_customFields' is deprecated and will soon be removed. Add properties to your JSON instead. (Deprecated since v1.38.1)"); return __assign(__assign({}, customFieldsObj), value); } return __assign(__assign({}, customFieldsObj), (_b = {}, _b[key] = value, _b)); }, {}); entityBuilder.withCustomFields(customFields); return entityBuilder.build(); }; EntityBuilder.prototype.filterCustomFields = function (customFields) { var _this = this; return Object.keys(customFields).reduce(function (validCfs, cf) { if (!_this._entityConstructor[(0, util_1.upperCaseSnakeCase)(cf)]) { validCfs[cf] = customFields[cf]; } logger.warn("Field name \"".concat(cf, "\" is already existing in \"").concat(toClassName(_this._entityConstructor._entityName), "\" and thus cannot be defined as custom field. ")); return validCfs; }, {}); }; return EntityBuilder; }()); exports.EntityBuilder = EntityBuilder; function toClassName(entityName) { return entityName.substr(entityName.indexOf('_') + 1); } function buildNavigationPropertyFromJson(key, value, entityConstructor) { var field = entityConstructor[(0, util_1.upperCaseSnakeCase)(key)]; var linkedEntityConstructor = field._linkedEntity; return Array.isArray(value) ? value.map(function (item) { return buildSingleEntityFromJson(item, linkedEntityConstructor); }) : buildSingleEntityFromJson(value, linkedEntityConstructor); } function buildSingleEntityFromJson(json, linkedEntityConstructor) { return json instanceof linkedEntityConstructor ? json : linkedEntityConstructor.builder().fromJson(json); } //# sourceMappingURL=entity-builder.js.map