@sap-cloud-sdk/odata-common
Version:
SAP Cloud SDK for JavaScript common functions of OData client generator and OpenAPI clint generator.
87 lines • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityBuilder = void 0;
const util_1 = require("@sap-cloud-sdk/util");
const properties_util_1 = require("./properties-util");
const logger = (0, util_1.createLogger)({
package: 'odata-common',
messageContext: 'entity-builder'
});
/**
* Contains the methods to build an entity.
*/
class EntityBuilder {
constructor(_entityApi) {
this._entityApi = _entityApi;
if (!this._entity) {
this._entity = new _entityApi.entityConstructor(_entityApi);
}
}
/**
* Sets the custom fields for the entity.
* @param customFields - The custom fields you want to add.
* @returns The entity builder itself for method chaining.
*/
withCustomFields(customFields) {
const validCustomFields = this.filterCustomFields(customFields);
this._entity = this._entity.setCustomFields(validCustomFields);
return this;
}
/**
* Builds the entity.
* @returns The entity.
*/
build() {
const entity = this._entity;
this._entity = new this._entityApi.entityConstructor(this._entityApi);
return entity;
}
/**
* Builds an entity from JSON representation.
* If you have obtained the JSON object as a request payload use the {@link entityDeserializer} 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 EntityBase constructed from JSON representation.
*/
fromJson(json) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const entityBuilder = this;
const [entityEntries, customEntries] = (0, util_1.partition)(Object.entries(json), ([key]) => typeof entityBuilder[key] === 'function');
entityEntries.forEach(([key, value]) => {
const propertyValue = (0, properties_util_1.isNavigationProperty)(key, this._entityApi.schema) && !!value
? buildNavigationPropertyFromJson(key, value, this._entityApi)
: value;
entityBuilder[key](propertyValue);
});
const customFields = customEntries.reduce((customFieldsObj, [key, value]) => ({ ...customFieldsObj, [key]: value }), {});
entityBuilder.withCustomFields(customFields);
return entityBuilder.build();
}
filterCustomFields(customFields) {
return Object.keys(customFields).reduce((validCfs, cf) => {
if (!this._entityApi.schema[(0, util_1.upperCaseSnakeCase)(cf)]) {
validCfs[cf] = customFields[cf];
}
logger.warn(`Field name "${cf}" is already existing in "${toClassName(this._entityApi.entityConstructor._entityName)}" and thus cannot be defined as custom field. `);
return validCfs;
}, {});
}
}
exports.EntityBuilder = EntityBuilder;
function toClassName(entityName) {
return entityName.substr(entityName.indexOf('_') + 1);
}
function buildNavigationPropertyFromJson(key, value, entityApi) {
const field = entityApi.schema[(0, util_1.upperCaseSnakeCase)(key)];
const linkedEntityApi = field._linkedEntityApi;
return Array.isArray(value)
? value.map(item => buildSingleEntityFromJson(item, linkedEntityApi))
: // TODO: remove type assertion?
buildSingleEntityFromJson(value, linkedEntityApi);
}
function buildSingleEntityFromJson(json, linkedEntityApi) {
return json instanceof linkedEntityApi.entityConstructor
? json
: linkedEntityApi.entityBuilder().fromJson(json);
}
//# sourceMappingURL=entity-builder.js.map