UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

363 lines 15.3 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PropertyMapper = void 0; const culture_insensitive_property_1 = require("../contracts/base/culture-insensitive-property"); const culture_sensitive_property_1 = require("../contracts/base/culture-sensitive-property"); const data_type_1 = __importStar(require("../contracts/base/data-type")); const minimal_schema_1 = require("../contracts/querying/minimal-schema"); const property_data_1 = require("../contracts/querying/property-data"); const guard_1 = __importDefault(require("../guard")); class PropertyMapper { constructor(client) { guard_1.default.notNullOrUndefined(client); this._client = client; } mapPropertiesAsync(resource, loadedCultures, schema) { return __awaiter(this, void 0, void 0, function* () { if (resource == null || resource.properties == null || Object.keys(resource.properties).length === 0) { return []; } // If no minimal schema is available, build it from the definition. if (schema == null) { const definitionName = yield this._client.linkHelper.nameFromDefinitionAsync(resource.entityDefinition); if (definitionName == null) { throw new Error(`Could not resolve definition name from entity resource.`); } const definition = yield this._client.entityDefinitions.getCachedAsync(definitionName); if (definition == null) { throw new Error(`Could not load definition with name '${definitionName}.`); } schema = new minimal_schema_1.MinimalSchema(definitionName); const loadedProperties = Object.keys(resource.properties); schema.properties = definition .getPropertyDefinitions() .filter(propertyDefinition => loadedProperties.includes(propertyDefinition.name)) .map(propertyDefinition => { let hasDataSource = false; if (propertyDefinition.type === data_type_1.default.String) { const stringPropertyDef = propertyDefinition; hasDataSource = !!(stringPropertyDef.dataSourceName && stringPropertyDef.dataSourceName.length > 0); } return new property_data_1.PropertyData({ name: propertyDefinition.name, isMultiValue: propertyDefinition.isMultiValue, isMultilingual: propertyDefinition.isMultiLanguage, hasDataSource: hasDataSource, dataType: data_type_1.default[propertyDefinition.type], }); }); } const list = []; for (const definition of schema.properties) { const prop = this.mapProperty(resource, loadedCultures, definition); list.push(prop); } return list; }); } mapProperty(resource, loadedCultures, definition) { const name = definition.name; const typeInfo = PropertyMapper.getType(definition); const propertyValue = resource.properties[name]; let property; if (definition.hasDataSource) { property = PropertyMapper.mapDataSource(definition, name, typeInfo, propertyValue); } else if (definition.isMultilingual) { property = PropertyMapper.mapMultilingualProperty(loadedCultures, name, typeInfo, propertyValue); } else { let value; if (definition.isMultiValue) { value = PropertyMapper.convertValue(typeInfo.type, true, propertyValue); } else { value = PropertyMapper.convertValue(typeInfo.type, false, propertyValue); } property = new culture_insensitive_property_1.CultureInsensitiveProperty(name, typeInfo, value); } return property; } static mapMultilingualProperty(loadedCultures, name, typeInfo, propertyValue) { const values = {}; for (const culture of Object.keys(propertyValue)) { let value; if (typeInfo.isArray) { value = this.convertValue(typeInfo.type, true, propertyValue[culture]); } else { value = this.convertValue(typeInfo.type, false, propertyValue[culture]); } values[culture] = value; } const prop = new culture_sensitive_property_1.CultureSensitiveProperty(name, typeInfo, loadedCultures, values); return prop; } // #region SDK to Base // MapProperties static mapToPropertyResources(entity) { guard_1.default.notNullOrUndefined(entity); const dict = {}; for (const property of entity.properties) { const mappedProp = this.mapToPropertyData(property); dict[property.name] = mappedProp.value; } return dict; } static mapDirtyProperties(entity) { guard_1.default.notNullOrUndefined(entity); const result = {}; const dirtyProperties = entity.properties.filter(property => property.isDirty); for (const property of dirtyProperties) { const mappedProp = this.mapToPropertyData(property); result[mappedProp.key] = mappedProp.value; } return result; } // MapProperty static mapToPropertyData(property) { let propertyData; if (property instanceof culture_sensitive_property_1.CultureSensitiveProperty) { const values = property.getValues(); propertyData = { key: property.name, value: values }; } else if (property instanceof culture_insensitive_property_1.CultureInsensitiveProperty) { const value = property.getValue(); if (property.hasDataSource) { let dsValue; if (value != null) { if (Array.isArray(value)) { dsValue = []; // eslint-disable-next-line @typescript-eslint/no-unsafe-call value.map(v => dsValue.push({ identifier: v })); } else { dsValue = { identifier: value }; } } else { dsValue = null; } propertyData = { key: property.name, value: dsValue }; } else { propertyData = { key: property.name, value: value }; } } else { throw new Error(`Property of type ${property.dataType} is not supported.`); } return propertyData; } // #endregion static getType(data) { let type; switch (data.dataType) { case "Boolean": type = data_type_1.default.Boolean; break; case "Decimal": type = data_type_1.default.Decimal; break; case "Int32": case data_type_1.default[data_type_1.default.Integer]: type = data_type_1.default.Integer; break; case "Int64": case data_type_1.default[data_type_1.default.Long]: type = data_type_1.default.Long; break; case "DateTime": type = data_type_1.default.DateTime; break; case "DateTimeOffset": type = data_type_1.default.DateTimeOffset; break; case "String": type = data_type_1.default.String; break; case "JToken": case data_type_1.default[data_type_1.default.Json]: type = data_type_1.default.Json; break; default: throw new Error("Unsupported value type."); } return new data_type_1.TypeInfo(type, data.isMultiValue); } static convertValue(type, isArray, value) { let result; if (value == null) { return null; } if (isArray) { if (value instanceof Array) { return value.map(item => this.convertValue(type, false, item)); } else if (typeof value === "string") { // If it's a string, we might be able to parse it. try { const parsedValue = JSON.parse(value); if (parsedValue instanceof Array) { return parsedValue.map(item => this.convertValue(type, false, item)); } else { value = parsedValue; } } catch (ex) { // If parsing fails, simply use the string. } } //TODO: Throw error? // // else { // // return null; // // } } switch (type) { case data_type_1.default.String: // // typeof value === "string" ? value : `${value}`; result = String(value); break; case data_type_1.default.Boolean: result = typeof value === "string" ? value === "true" : !!value; break; case data_type_1.default.Integer: case data_type_1.default.Long: { const number = parseInt(value, 10); if (Number.isNaN(number)) { result = null; //TODO: Use regex /^[-+]?[0-9]*$/ and throw error? } else { result = number; } break; } case data_type_1.default.Decimal: { const number = parseFloat(value); if (Number.isNaN(number)) { result = null; //TODO: Use regex /^[-+]?\b[0-9]*\.?[0-9]*$/ and throw error? } else { result = number; } break; } case data_type_1.default.Json: { // Valid JSON values: string, number, boolean, null, array or JSON-object if (value == null) { result = null; } else if (typeof value === "object") { // Covers JSON-object and array result = JSON.parse(JSON.stringify(value)); } else if (typeof value === "string") { try { // When we have a string, it might be a serialized object we can parse. result = JSON.parse(value); } catch (ex) { // Not a valid JSON-object, so return the string value as-is. result = value; } } else { // We might be dealing with a boolean, number or null. result = value; } break; } case data_type_1.default.DateTime: { result = value; break; } case data_type_1.default.DateTimeOffset: { result = value; break; } } return result; } static mapDataSource(definition, name, typeInfo, propertyValue) { if (propertyValue == null) { const prop = new culture_insensitive_property_1.CultureInsensitiveProperty(name, typeInfo, null, true); return prop; } if (definition.isMultiValue) { const array = propertyValue instanceof Array ? propertyValue : typeof propertyValue === "string" ? JSON.parse(propertyValue) : null; const list = []; for (const token of array) { const value = this.mapDataSourceValue(token); if (value != null) { list.push(value); } } const prop = new culture_insensitive_property_1.CultureInsensitiveProperty(name, typeInfo, [...list], true); return prop; } else { const value = this.mapDataSourceValue(propertyValue); const prop = new culture_insensitive_property_1.CultureInsensitiveProperty(name, typeInfo, value, true); return prop; } } static mapDataSourceValue(dataSourceValue) { if (dataSourceValue == null) { return null; } const identifier = dataSourceValue["identifier"]; return identifier; } } exports.PropertyMapper = PropertyMapper; //# sourceMappingURL=property-mapper.js.map