UNPKG

@sap-cloud-sdk/core

Version:
151 lines • 7.31 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.entitySerializer = void 0; var util_1 = require("@sap-cloud-sdk/util"); var odata_common_1 = require("../odata-common"); var name_converter_1 = require("./name-converter"); var logger = (0, util_1.createLogger)({ package: 'core', messageContext: 'entity-serializer' }); /** * Constructs an entitySerializer given the OData v2 or v4 specific tsToEdm method. * The concrete serializers are created in odata/v2/entity-serializer.ts and odata/v4/entity-serializer.ts * @param tsToEdm - Converters ts input to EDM values * @returns a entity serializer as defined by [[EntitySerializer]] */ function entitySerializer(tsToEdm) { /** * Converts an instance of an entity class into a JSON payload to be sent to an OData service. * @param entity - An instance of an entity. * @param entityConstructor - The constructor function of that entity. * @param diff - Serialize changed properties only. This only applies on the first level in case there are navigational properties. * @returns JSON. */ function serializeEntity(entity, entityConstructor, diff) { if (diff === void 0) { diff = false; } return __assign(__assign({}, serializeEntityNonCustomFields(entity, entityConstructor, diff)), (diff ? entity.getUpdatedCustomFields() : entity.getCustomFields())); } function serializeField(field, fieldValue) { if (fieldValue === null || fieldValue === undefined) { return null; } if (field instanceof odata_common_1.EdmTypeField) { return tsToEdm(fieldValue, field.edmType); } if (field instanceof odata_common_1.OneToOneLink) { return serializeEntity(fieldValue, field._linkedEntity); } if (field instanceof odata_common_1.Link) { return fieldValue.map(function (linkedEntity) { return serializeEntity(linkedEntity, field._linkedEntity); }); } if (field instanceof odata_common_1.ComplexTypeField) { if (field._complexType) { return serializeComplexType(fieldValue, field._complexType); } return serializeComplexTypeFieldLegacy(field, fieldValue); } if (field instanceof odata_common_1.CollectionField) { return serializeCollection(fieldValue, field._fieldType); } if (field instanceof odata_common_1.EnumField) { return fieldValue; } } /** * Converts an instance of an entity class into a JSON payload to be sent to an OData service, ignoring custom fields. * @param entity - An instance of an entity. * @param entityConstructor - The constructor function of that entity. * @param diff - Serialize changed properties only. This only applies on the first level in case there are navigational properties. * @returns A JSON Representation of the non custom fields */ function serializeEntityNonCustomFields(entity, entityConstructor, diff) { if (diff === void 0) { diff = false; } return getFieldNames(entity, diff).reduce(function (serialized, key) { var _a; var field = entityConstructor[(0, name_converter_1.toStaticPropertyFormat)(key)]; var fieldValue = entity[key]; var serializedValue = serializeField(field, fieldValue); if (typeof serializedValue === 'undefined') { logger.warn("Could not serialize value for unknown field: ".concat(field, ". Skipping field.")); return serialized; } return __assign(__assign({}, serialized), (_a = {}, _a[field._fieldName] = serializedValue, _a)); }, {}); } function getFieldNames(entity, diff) { if (diff === void 0) { diff = false; } return entity ? diff ? entity.getUpdatedPropertyNames() : Object.keys(entity) : []; } // TODO: get rid of this function in v2.0 function serializeComplexTypeFieldLegacy(complexTypeField, fieldValue) { logger.warn('It seems that you are using an outdated OData client. To make this warning disappear, please regenerate your client using the latest version of the SAP Cloud SDK generator.'); return Object.entries(complexTypeField) .filter(function (_a) { var propertyKey = _a[0], propertyValue = _a[1]; return (propertyValue instanceof odata_common_1.EdmTypeField || propertyValue instanceof odata_common_1.ComplexTypeField) && typeof fieldValue[propertyKey] !== 'undefined'; }) .reduce(function (complexTypeObject, _a) { var _b; var propertyKey = _a[0], propertyValue = _a[1]; return (__assign(__assign({}, complexTypeObject), (_b = {}, _b[propertyValue._fieldName] = propertyValue instanceof odata_common_1.EdmTypeField ? tsToEdm(fieldValue[propertyKey], propertyValue.edmType) : serializeComplexTypeFieldLegacy(propertyValue, fieldValue[propertyKey]), _b))); }, {}); } function serializeComplexTypeProperty(propertyValue, propertyMetadata) { if (propertyMetadata.isCollection) { return serializeCollection(propertyValue, propertyMetadata.type); } if ((0, odata_common_1.isComplexTypeNameSpace)(propertyMetadata.type)) { return serializeComplexType(propertyValue, propertyMetadata.type); } return tsToEdm(propertyValue, propertyMetadata.type); } function serializeComplexType(fieldValue, complexType) { return complexType._propertyMetadata .map(function (property) { var _a; return (__assign({}, (typeof fieldValue[property.name] !== 'undefined' && (_a = {}, _a[property.originalName] = serializeComplexTypeProperty(fieldValue[property.name], property), _a)))); }) .reduce(function (complexTypeObject, property) { return (__assign(__assign({}, complexTypeObject), property)); }, {}); } function serializeCollection(fieldValue, fieldType) { if ((0, odata_common_1.isEdmType)(fieldType)) { return fieldValue.map(function (val) { return tsToEdm(val, fieldType); }); } if ((0, odata_common_1.isComplexTypeNameSpace)(fieldType)) { return fieldValue.map(function (val) { return serializeComplexType(val, fieldType); }); } // Enum return fieldValue; } return { serializeEntity: serializeEntity, serializeComplexType: serializeComplexType, serializeEntityNonCustomFields: serializeEntityNonCustomFields }; } exports.entitySerializer = entitySerializer; //# sourceMappingURL=entity-serializer.js.map