UNPKG

12.4 kBJavaScriptView Raw
1"use strict";
2/* eslint-disable max-classes-per-file */
3var __assign = (this && this.__assign) || function () {
4 __assign = Object.assign || function(t) {
5 for (var s, i = 1, n = arguments.length; i < n; i++) {
6 s = arguments[i];
7 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
8 t[p] = s[p];
9 }
10 return t;
11 };
12 return __assign.apply(this, arguments);
13};
14var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
15 if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
16 if (ar || !(i in from)) {
17 if (!ar) ar = Array.prototype.slice.call(from, 0, i);
18 ar[i] = from[i];
19 }
20 }
21 return to.concat(ar || Array.prototype.slice.call(from));
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.EntityBase = exports.isExpandedProperty = exports.isExistentProperty = exports.isSelectedProperty = exports.Entity = void 0;
25var util_1 = require("@sap-cloud-sdk/util");
26var entity_builder_1 = require("./entity-builder");
27var properties_util_1 = require("./properties-util");
28/**
29 * Super class for all representations of OData entity types.
30 */
31var Entity = /** @class */ (function () {
32 function Entity() {
33 (0, properties_util_1.nonEnumerable)(this, '_oDataVersion');
34 (0, properties_util_1.nonEnumerable)(this, '_customFields');
35 this._customFields = {};
36 }
37 Entity.entityBuilder = function (entityConstructor) {
38 var builder = new entity_builder_1.EntityBuilder(entityConstructor);
39 entityConstructor._allFields.forEach(function (field) {
40 var fieldName = "".concat((0, util_1.camelCase)(field._fieldName));
41 builder[fieldName] = function (value) {
42 this.entity[fieldName] = value;
43 return this;
44 };
45 });
46 return builder;
47 };
48 Object.defineProperty(Entity.prototype, "versionIdentifier", {
49 /**
50 * ETag version identifier accessor.
51 * @returns The ETag version identifier of the retrieved entity, returns `undefined` if not retrieved.
52 */
53 get: function () {
54 return this._versionIdentifier;
55 },
56 enumerable: false,
57 configurable: true
58 });
59 /**
60 * Returns a map that contains all entity custom fields.
61 * @returns A map of all defined custom fields in the entity
62 */
63 Entity.prototype.getCustomFields = function () {
64 return this._customFields;
65 };
66 /**
67 * Custom field value getter.
68 * @param fieldName - The name of the custom field
69 * @returns The value of the corresponding custom field
70 */
71 Entity.prototype.getCustomField = function (fieldName) {
72 return this._customFields[fieldName];
73 };
74 /**
75 * Sets a new custom field in the entity or updates it.
76 * Throws an error, if the provided custom field name is already defined by an original field in entity.
77 * @param fieldName - The name of the custom field to update
78 * @param value - The value of the field
79 * @returns The entity itself, to facilitate method chaining
80 */
81 Entity.prototype.setCustomField = function (fieldName, value) {
82 if (this.isConflictingCustomField(fieldName)) {
83 throw new Error("The field name \"".concat(fieldName, "\" is already defined in the entity and cannot be set as custom field."));
84 }
85 this._customFields[fieldName] = value;
86 return this;
87 };
88 /**
89 * Validates whether a custom field exists in the entity.
90 * @param fieldName - The name of the custom field to update
91 * @returns A boolean value, that indicates whether a custom field is defined in entity
92 */
93 Entity.prototype.hasCustomField = function (fieldName) {
94 return this._customFields[fieldName] !== undefined;
95 };
96 /**
97 * Sets custom fields on an entity.
98 * @param customFields - Custom fields to set on the entity.
99 * @returns The entity itself, to facilitate method chaining
100 */
101 Entity.prototype.setCustomFields = function (customFields) {
102 var _this = this;
103 Object.entries(customFields).forEach(function (_a) {
104 var key = _a[0], value = _a[1];
105 _this.setCustomField(key, value);
106 });
107 return this;
108 };
109 /**
110 * @deprecated Since v1.34.1. Use [[setCustomFields]] instead.
111 * Sets all retrieved custom fields in entity.
112 * @param customFields - Extracted custom fields from a retrieved entity.
113 * @returns The entity itself, to facilitate method chaining.
114 */
115 Entity.prototype.initializeCustomFields = function (customFields) {
116 return this.setCustomFields(customFields);
117 };
118 /**
119 * Set the ETag version identifier of the retrieved entity.
120 * @param etag - The returned ETag version of the entity.
121 * @returns The entity itself, to facilitate method chaining.
122 */
123 Entity.prototype.setVersionIdentifier = function (etag) {
124 if (etag && typeof etag === 'string') {
125 (0, properties_util_1.nonEnumerable)(this, '_versionIdentifier');
126 this._versionIdentifier = etag;
127 }
128 return this;
129 };
130 /**
131 * @deprecated Since 1.12.0. Will be hidden in version 2.0.
132 * Initializes or sets the remoteState of the entity.
133 * This function is called on all read, create and update requests.
134 * This function should be called after [[initializeCustomFields]], if custom fields are defined.
135 * @param state - State to be set as remote state.
136 * @returns The entity itself, to facilitate method chaining
137 */
138 Entity.prototype.setOrInitializeRemoteState = function (state) {
139 var _this = this;
140 if (!this.remoteState) {
141 (0, properties_util_1.nonEnumerable)(this, 'remoteState');
142 }
143 state = state || this.asObject();
144 this.remoteState = Object.entries(state).reduce(function (stateObject, _a) {
145 var _b;
146 var fieldName = _a[0], value = _a[1];
147 var propertyName = _this[(0, util_1.camelCase)(fieldName)]
148 ? (0, util_1.camelCase)(fieldName)
149 : fieldName;
150 return __assign(__assign({}, stateObject), (_b = {}, _b[propertyName] = value, _b));
151 }, {});
152 return this;
153 };
154 /**
155 * Returns all updated custom field properties compared to the last known remote state.
156 * @returns An object containing all updated custom properties, with their new values.
157 */
158 Entity.prototype.getUpdatedCustomFields = function () {
159 var _this = this;
160 if (!this.remoteState) {
161 return this._customFields;
162 }
163 return Object.entries(this.getCustomFields())
164 .filter(function (_a) {
165 var fieldName = _a[0], value = _a[1];
166 return _this.remoteState[fieldName] !== value;
167 })
168 .reduce(function (updatedCustomFields, _a) {
169 var _b;
170 var fieldName = _a[0], value = _a[1];
171 return (__assign(__assign({}, updatedCustomFields), (_b = {}, _b[fieldName] = value, _b)));
172 }, {});
173 };
174 /**
175 * Returns all changed properties compared to the last known remote state.
176 * The returned properties do not include custom fields.
177 * Use [[getUpdatedCustomFields]], if you need custom fields.
178 * @returns Entity with all properties that changed
179 */
180 Entity.prototype.getUpdatedProperties = function () {
181 var current = this.asObject();
182 return this.getUpdatedPropertyNames().reduce(function (patch, key) {
183 var _a;
184 return (__assign(__assign({}, patch), (_a = {}, _a[key] = current[key], _a)));
185 }, {});
186 };
187 /**
188 * Returns all changed property names compared to the last known remote state.
189 * The returned properties names do not include custom fields.
190 * Use [[getUpdatedCustomFields]], if you need custom fields.
191 * @returns Entity with all properties that changed
192 */
193 Entity.prototype.getUpdatedPropertyNames = function () {
194 var _this = this;
195 var currentState = this.asObject();
196 var names = Object.keys(currentState).filter(function (key) { return _this.propertyIsEnumerable(key) && !_this.hasCustomField(key); });
197 return !this.remoteState
198 ? names
199 : names.filter(function (key) { return !(0, util_1.equal)(_this.remoteState[key], currentState[key]); });
200 };
201 /**
202 * @deprecated Since v1.34.1. Use [[asObject]] instead.
203 * Returns a map of all defined fields in entity to their current values.
204 * @param visitedEntities - List of entities to check in case of circular dependencies.
205 * @returns Entity with all defined entity fields
206 */
207 Entity.prototype.getCurrentMapKeys = function (visitedEntities) {
208 if (visitedEntities === void 0) { visitedEntities = []; }
209 return this.asObject(visitedEntities);
210 };
211 Entity.prototype.isVisitedEntity = function (entity, visitedEntities) {
212 if (visitedEntities === void 0) { visitedEntities = []; }
213 return Array.isArray(entity)
214 ? entity.some(function (multiLinkChild) { return visitedEntities.includes(multiLinkChild); })
215 : visitedEntities.includes(entity);
216 };
217 Entity.prototype.getCurrentStateForKey = function (key, visitedEntities) {
218 if (visitedEntities === void 0) { visitedEntities = []; }
219 if ((0, properties_util_1.isNavigationProperty)(key, this.constructor)) {
220 if ((0, util_1.isNullish)(this[key])) {
221 return this[key];
222 }
223 return Array.isArray(this[key])
224 ? this[key].map(function (linkedEntity) {
225 return linkedEntity.getCurrentMapKeys(visitedEntities);
226 })
227 : this[key].getCurrentMapKeys(visitedEntities);
228 }
229 return Array.isArray(this[key]) ? __spreadArray([], this[key], true) : this[key];
230 };
231 /**
232 * Validates whether a field name does not conflict with an original field name and thus can be defined as custom fields.
233 * @param customFieldName - Field name to check
234 * @returns Boolean value that describes whether a field name can be defined as custom field
235 */
236 Entity.prototype.isConflictingCustomField = function (customFieldName) {
237 return this.constructor._allFields
238 .map(function (f) { return f._fieldName; })
239 .includes(customFieldName);
240 };
241 /**
242 * Creates an object containing all defined properties, navigation properties and custom fields in the entity.
243 * @param visitedEntities - List of entities to check in case of circular dependencies.
244 * @returns Entity as an object with all defined entity fields
245 */
246 Entity.prototype.asObject = function (visitedEntities) {
247 var _this = this;
248 if (visitedEntities === void 0) { visitedEntities = []; }
249 visitedEntities.push(this);
250 return Object.keys(this)
251 .filter(function (key) {
252 return _this.propertyIsEnumerable(key) &&
253 (!(0, properties_util_1.isNavigationProperty)(key, _this.constructor) ||
254 !_this.isVisitedEntity(_this[key], visitedEntities));
255 })
256 .reduce(function (accumulatedMap, key) {
257 var _a;
258 return (__assign(__assign({}, accumulatedMap), (_a = {}, _a[key] = _this.getCurrentStateForKey(key, visitedEntities), _a)));
259 }, this.getCustomFields());
260 };
261 return Entity;
262}());
263exports.Entity = Entity;
264exports.EntityBase = Entity;
265/* eslint-disable valid-jsdoc */
266/**
267 * @hidden
268 */
269function isSelectedProperty(json, field) {
270 return json.hasOwnProperty(field._fieldName);
271}
272exports.isSelectedProperty = isSelectedProperty;
273/**
274 * @hidden
275 */
276function isExistentProperty(json, link) {
277 return isSelectedProperty(json, link) && json[link._fieldName] !== null;
278}
279exports.isExistentProperty = isExistentProperty;
280/**
281 * @hidden
282 */
283function isExpandedProperty(json, link) {
284 return (isExistentProperty(json, link) &&
285 !json[link._fieldName].hasOwnProperty('__deferred'));
286}
287exports.isExpandedProperty = isExpandedProperty;
288//# sourceMappingURL=entity.js.map
\No newline at end of file