UNPKG

kentico-cloud-delivery

Version:

Official Kentico Cloud Delivery SDK

239 lines 12.1 kB
import { FieldDecorators, Fields, FieldType } from '../fields'; import { Link } from '../models'; import { richTextResolver, stronglyTypedResolver, urlSlugResolver } from '../resolvers'; var FieldMapper = /** @class */ (function () { function FieldMapper(config, richTextHtmlParser) { this.config = config; this.richTextHtmlParser = richTextHtmlParser; this.defaultModularContentWrapperTag = 'p'; this.defaultModularContentWrapperClasses = ['kc-modular-item-wrapper']; } /** * Maps all fields in given content item and returns strongly typed content item based on the resolver specified * in DeliveryClientConfig * @param item Item to map (raw response from Kentico Cloud) * @param modularContent Modular content sent along with item * @param queryConfig Query configuration */ FieldMapper.prototype.mapFields = function (item, modularContent, queryConfig, processedItems) { var _this = this; if (!item) { throw Error("Cannot map fields because item is not defined"); } if (!item.system) { throw Error("Cannot map item because it does not contain system attributes. This is an essential field and every item should have one."); } if (!item.elements) { throw Error("Cannot map elements of item with codename '" + item.system.codename + "'"); } if (!processedItems) { throw Error("ProcessedItems need to be initialized"); } if (!Array.isArray(processedItems)) { throw Error("ProcessedItems need to be an array of 'IContentItems'"); } var properties = Object.getOwnPropertyNames(item.elements); var itemTyped; // check if resolver for this type is available if (this.config.typeResolvers && stronglyTypedResolver.hasTypeResolver(item.system.type, this.config.typeResolvers)) { itemTyped = stronglyTypedResolver.createTypedObj(item.system.type, item, this.config.typeResolvers); } else { itemTyped = stronglyTypedResolver.createContentItem(item); } // add/taken item to processed items to avoid infinite recursion var processedItem = processedItems.find(function (m) { return m.system.codename === item.system.codename; }); if (!processedItem) { processedItems.push(itemTyped); } properties.forEach(function (fieldName) { var field = item.elements[fieldName]; var propertyName; // resolve field to a custom model property if (itemTyped.propertyResolver) { propertyName = itemTyped.propertyResolver(fieldName); } // if property hasn't been resolved, try with decorator if (!propertyName) { propertyName = FieldDecorators.getPropertyName(itemTyped, fieldName); } // if property name is null/empty, use elements codename if (!propertyName) { propertyName = fieldName; } itemTyped[propertyName] = _this.mapField(field, modularContent, itemTyped, queryConfig, processedItems); }); return itemTyped; }; FieldMapper.prototype.mapField = function (field, modularContent, item, queryConfig, processedItems) { var fieldType = field.type.toLowerCase(); if (fieldType === FieldType.ModularContent.toString()) { return this.mapModularField(field, modularContent, queryConfig, processedItems); } else if (fieldType === FieldType.Text.toLowerCase()) { return this.mapTextField(field); } else if (fieldType === FieldType.Asset.toLowerCase()) { return this.mapAssetsField(field); } else if (fieldType === FieldType.Number.toLowerCase()) { return this.mapNumberField(field); } else if (fieldType === FieldType.MultipleChoice.toLowerCase()) { return this.mapMultipleChoiceField(field); } else if (fieldType === FieldType.DateTime.toLowerCase()) { return this.mapDateTimeField(field); } else if (fieldType === FieldType.RichText.toLowerCase()) { return this.mapRichTextField(field, modularContent, queryConfig, processedItems); } else if (fieldType === FieldType.UrlSlug.toLowerCase()) { return this.mapUrlSlugField(field, item, queryConfig); } else if (fieldType === FieldType.Taxonomy.toLowerCase()) { return this.mapTaxonomyField(field); } var error = "Unsupported field type '" + field.type + "'"; throw Error(error); }; FieldMapper.prototype.mapRichTextField = function (field, modularContent, queryConfig, processedItems) { var _this = this; // get all modular content items nested in rich text var modularItems = []; if (field.modular_content) { if (Array.isArray(field.modular_content)) { field.modular_content.forEach(function (codename) { // get modular item and check if it exists (it might not be included in response due to 'Depth' parameter) var modularContentItem = modularContent[codename]; if (!modularContentItem) { throw Error("Modular content item with codename '" + codename + "' is not present in Delivery response.\n This modular item was requested by '" + field.name + "' field.\n Error can usually be solved by increasing 'Depth' parameter of your query."); } var modularItem = _this.mapFields(modularContent[codename], modularContent, queryConfig, processedItems); if (modularItem != null) { modularItems.push(modularItem); } }); } } // extract and map links var links = this.mapRichTextLinks(field.links); return new Fields.RichTextField(field.name, field.value, { links: links, resolveHtml: function () { return richTextResolver.resolveHtml(field.value, { enableAdvancedLogging: _this.config.enableAdvancedLogging ? _this.config.enableAdvancedLogging : false, typeResolvers: _this.config.typeResolvers ? _this.config.typeResolvers : [], richTextHtmlParser: _this.richTextHtmlParser, modularItems: modularItems, links: links, queryConfig: queryConfig, modularContentWrapperTag: _this.config.modularContentResolver && _this.config.modularContentResolver.modularContentWrapperTag ? _this.config.modularContentResolver.modularContentWrapperTag : _this.defaultModularContentWrapperTag, modularContentWrapperClasses: _this.config.modularContentResolver && _this.config.modularContentResolver.modularContentWrapperClasses ? _this.config.modularContentResolver.modularContentWrapperClasses : _this.defaultModularContentWrapperClasses }); } }); }; FieldMapper.prototype.mapDateTimeField = function (field) { return new Fields.DateTimeField(field.name, field.value); }; FieldMapper.prototype.mapMultipleChoiceField = function (field) { return new Fields.MultipleChoiceField(field.name, field.value); }; FieldMapper.prototype.mapNumberField = function (field) { return new Fields.NumberField(field.name, field.value); }; FieldMapper.prototype.mapTextField = function (field) { return new Fields.TextField(field.name, field.value); }; FieldMapper.prototype.mapAssetsField = function (field) { return new Fields.AssetsField(field.name, field.value); }; FieldMapper.prototype.mapTaxonomyField = function (field) { return new Fields.TaxonomyField(field.name, field.value, field.taxonomy_group); }; FieldMapper.prototype.mapUrlSlugField = function (field, item, queryConfig) { var _this = this; var linkResolver = this.getLinkResolverForUrlSlugField(item, queryConfig); return new Fields.UrlSlugField(field.name, field.value, { resolveUrl: function () { return urlSlugResolver.resolveUrl({ fieldName: field.name, type: field.type, fieldValue: field.value, item: item, enableAdvancedLogging: _this.config.enableAdvancedLogging ? _this.config.enableAdvancedLogging : false, linkResolver: linkResolver }); } }); }; FieldMapper.prototype.mapModularField = function (field, modularContent, queryConfig, processedItems) { var _this = this; if (!field) { if (this.config.enableAdvancedLogging) { console.warn("Cannot map modular content field because field does not exist"); } return null; } if (!field.value) { if (this.config.enableAdvancedLogging) { console.warn("Cannot map modular content of '" + field.name + "' because its value does not exist"); } return null; } // modular content is always returned in an array var modularContentItems = []; var fieldModularContent = field.value; fieldModularContent.forEach(function (modularItemCodename) { var modularItem = modularContent[modularItemCodename]; if (!modularItem) { if (_this.config.enableAdvancedLogging) { console.warn("Cannot map '" + field.name + "' modular content item. Make sure you use 'DepthParameter' in case your modular content is nested."); } } // try to map only if the modular item was present in response if (modularItem) { // add/taken item to processed items to avoid infinite recursion var processedItem = processedItems.find(function (m) { return m.system.codename === modularItem.system.codename; }); if (processedItem) { modularContentItems.push(processedItem); } else { var newModularItem = _this.mapFields(modularItem, modularContent, queryConfig, processedItems); modularContentItems.push(newModularItem); processedItem = newModularItem; } } }); return modularContentItems; }; FieldMapper.prototype.getLinkResolverForUrlSlugField = function (item, queryConfig) { // link resolver defined by the query config (= by calling method) has priority over type's global link resolver var linkResolver = undefined; if (queryConfig.linkResolver) { linkResolver = queryConfig.linkResolver; } else { linkResolver = item.linkResolver; } return linkResolver; }; FieldMapper.prototype.mapRichTextLinks = function (linksJson) { var links = []; for (var _i = 0, _a = Object.keys(linksJson); _i < _a.length; _i++) { var linkItemId = _a[_i]; var linkRaw = linksJson[linkItemId]; links.push(new Link({ codename: linkRaw.codename, itemId: linkItemId, urlSlug: linkRaw.url_slug, type: linkRaw.type })); } return links; }; return FieldMapper; }()); export { FieldMapper }; //# sourceMappingURL=field.mapper.js.map