UNPKG

@sap-cloud-sdk/core

Version:
188 lines • 9.08 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); }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractCustomFields = exports.extractEtagFromHeader = exports.entityDeserializer = 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-deserializer' }); /** * Constructs an entityDeserializer given the OData v2 or v4 specific methods. * The concrete deserializers are created in odata/v2/entity-deserializer.ts and odata/v4/entity-deserializer.ts * @param edmToTs - Converters emd input to ts values. * @param extractODataETag - Extractor for the ETag. * @param extractDataFromOneToManyLink - Extractor for data related to one to many links. * @returns a entity deserializer as defined by [[EntityDeserializer]] */ function entityDeserializer(edmToTs, extractODataETag, extractDataFromOneToManyLink) { /** * Converts the JSON payload for a single entity into an instance of the corresponding generated entity class. * It sets the remote state to the data provided by the JSON payload. * If a version identifier is found in the '__metadata' or in the request header, the method also sets it. * @param json - The JSON payload. * @param entityConstructor - The constructor function of the entity class. * @param requestHeader - Optional parameter which may be used to add a version identifier (ETag) to the entity * @returns An instance of the entity class. */ function deserializeEntity(json, entityConstructor, requestHeader) { var etag = extractODataETag(json) || extractEtagFromHeader(requestHeader); return entityConstructor._allFields // type assertion for backwards compatibility, TODO: remove in v2.0 .filter(function (field) { return (0, odata_common_1.isSelectedProperty)(json, field); }) .reduce(function (entity, staticField) { entity[(0, name_converter_1.toPropertyFormat)(staticField._fieldName)] = getFieldValue(json, staticField); return entity; }, new entityConstructor()) .initializeCustomFields(extractCustomFields(json, entityConstructor)) .setVersionIdentifier(etag) .setOrInitializeRemoteState(); } function getFieldValue(json, field) { if (field instanceof odata_common_1.EdmTypeField) { return edmToTs(json[field._fieldName], field.edmType); } if (field instanceof odata_common_1.Link) { return getLinkFromJson(json, field); } if (field instanceof odata_common_1.ComplexTypeField) { if (json[field._fieldName]) { return field._complexType ? deserializeComplexType(json[field._fieldName], field._complexType) : deserializeComplexTypeLegacy(json[field._fieldName], field); } return json[field._fieldName]; } if (field instanceof odata_common_1.CollectionField) { return deserializeCollectionType(json[field._fieldName], field._fieldType); } if (field instanceof odata_common_1.EnumField) { return json[field._fieldName]; } } function getLinkFromJson(json, link) { return link instanceof odata_common_1.OneToOneLink ? getSingleLinkFromJson(json, link) : getMultiLinkFromJson(json, link); } // Be careful: if the return type is changed to `LinkedEntityT | undefined`, the test 'navigation properties should never be undefined' of the 'business-partner.spec.ts' will fail. // Not sure the purpose of the usage of null. function getSingleLinkFromJson(json, link) { if ((0, odata_common_1.isExpandedProperty)(json, link)) { return deserializeEntity(json[link._fieldName], link._linkedEntity); } return null; } function getMultiLinkFromJson(json, link) { if ((0, odata_common_1.isSelectedProperty)(json, link)) { var results = extractDataFromOneToManyLink(json[link._fieldName]); return results.map(function (linkJson) { return deserializeEntity(linkJson, link._linkedEntity); }); } } // TODO: get rid of this function in v2.0 function deserializeComplexTypeLegacy(json, complexTypeField) { 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.'); if (json === null) { return null; } return Object.entries(complexTypeField) .filter(function (_a) { var field = _a[1]; return (field instanceof odata_common_1.EdmTypeField || field instanceof odata_common_1.ComplexTypeField) && typeof json[field._fieldName] !== 'undefined'; }) .reduce(function (complexTypeObject, _a) { var _b; var fieldName = _a[0], field = _a[1]; return (__assign(__assign({}, complexTypeObject), (_b = {}, _b[(0, name_converter_1.toPropertyFormat)(fieldName)] = field instanceof odata_common_1.EdmTypeField ? edmToTs(json[field._fieldName], field.edmType) : deserializeComplexTypeLegacy(json[field._fieldName], field), _b))); }, {}); } function deserializeComplexTypeProperty(propertyValue, propertyMetadata) { if (propertyMetadata.isCollection) { return deserializeCollectionType(propertyValue, propertyMetadata.type); } if ((0, odata_common_1.isComplexTypeNameSpace)(propertyMetadata.type)) { return deserializeComplexType(propertyValue, propertyMetadata.type); } return edmToTs(propertyValue, propertyMetadata.type); } function deserializeComplexType(json, complexType) { if (json === null) { return null; } return complexType._propertyMetadata .map(function (property) { var _a; return (__assign({}, (typeof json[property.originalName] !== 'undefined' && (_a = {}, _a[property.name] = deserializeComplexTypeProperty(json[property.originalName], property), _a)))); }) .reduce(function (complexTypeInstance, property) { return (__assign(__assign({}, complexTypeInstance), property)); }); } function deserializeCollectionType(json, fieldType) { if ((0, odata_common_1.isEdmType)(fieldType)) { return json.map(function (val) { return edmToTs(val, fieldType); }); } if ((0, odata_common_1.isComplexTypeNameSpace)(fieldType)) { return json.map(function (val) { return deserializeComplexType(val, fieldType); }); } // Enum return json; } return { deserializeEntity: deserializeEntity, deserializeComplexType: deserializeComplexType }; } exports.entityDeserializer = entityDeserializer; function extractEtagFromHeader(headers) { return (0, util_1.pickValueIgnoreCase)(headers, 'etag'); } exports.extractEtagFromHeader = extractEtagFromHeader; /** * Extracts all custom fields from the JSON payload for a single entity. * In this context, a custom fields is every property that is not known in the corresponding entity class. * @param json - The JSON payload. * @param entityConstructor - The constructor function of the entity class. * @returns An object containing the custom fields as key-value pairs. */ function extractCustomFields(json, entityConstructor) { var regularODataProperties = __spreadArray([ '__metadata', '__deferred' ], entityConstructor._allFields.map(function (field) { return field._fieldName; }), true); var regularFields = new Set(regularODataProperties); return Object.keys(json) .filter(function (key) { return !regularFields.has(key); }) .reduce(function (customFields, key) { customFields[key] = json[key]; return customFields; }, {}); } exports.extractCustomFields = extractCustomFields; //# sourceMappingURL=entity-deserializer.js.map