UNPKG

kentico-cloud-delivery

Version:

Official Kentico Cloud Delivery SDK

83 lines 3.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var models_1 = require("../models"); var DeliveryItemStronglyTypeResolver = /** @class */ (function () { function DeliveryItemStronglyTypeResolver() { this.systemElementName = 'system'; this.elementsElementName = 'elements'; } /** * Creates item instance using either TypeResolver (if registered) or returns ContentItem */ DeliveryItemStronglyTypeResolver.prototype.createItemInstance = function (type, data, typeResolvers) { var typeResolver = this.getTypeResolver(type, typeResolvers); var itemInstance; if (typeResolver) { // type resolver is registered, create new instance of given type itemInstance = this.createInstanceWithResolver(data, typeResolver); } else { // not type resolver is register for this type, use ContentItem itemInstance = this.createContentItem(data.item); } if (!itemInstance) { throw Error("Item with codename '" + data.item.system.codename + "' could not be instantiated"); } this.assignRequiredContentItemData(itemInstance, data.item); return itemInstance; }; /** * Maps raw system response to strongly typed class * @param rawSystem Raw system response */ DeliveryItemStronglyTypeResolver.prototype.mapSystemAttributes = function (rawSystem) { return new models_1.ContentItemSystemAttributes({ name: rawSystem.name, codename: rawSystem.codename, id: rawSystem.id, lastModified: new Date(rawSystem.last_modified), language: rawSystem.language, type: rawSystem.type, sitemapLocations: rawSystem.sitemap_locations }); }; /** * Creates new instance of given type * @param type Type of the content item * @param resolvers Type resolvers */ DeliveryItemStronglyTypeResolver.prototype.createInstanceWithResolver = function (data, resolver) { return resolver.resolve(data); }; /** * Gets TypeResolver associated with given type (type = codename of Kentico Cloud content type) * @param type Kentico Cloud content type codename * @param resolvers Array of TypeResolver */ DeliveryItemStronglyTypeResolver.prototype.getTypeResolver = function (type, resolvers) { return resolvers.find(function (m) { return m.type.toLowerCase() === type.toLowerCase(); }); }; /** * Creates base ContentItem when content type does not have a strongly typed model */ DeliveryItemStronglyTypeResolver.prototype.createContentItem = function (item) { var contentItem = new models_1.ContentItem(); this.assignRequiredContentItemData(contentItem, item); return contentItem; }; /** * Maps default properties (system & elements) * @param item Mapped content item * @param rawItem Raw content item from response */ DeliveryItemStronglyTypeResolver.prototype.assignRequiredContentItemData = function (item, rawItem) { item.system = this.mapSystemAttributes(rawItem[this.systemElementName]); item._raw = { elements: rawItem[this.elementsElementName], }; }; return DeliveryItemStronglyTypeResolver; }()); exports.DeliveryItemStronglyTypeResolver = DeliveryItemStronglyTypeResolver; exports.stronglyTypedResolver = new DeliveryItemStronglyTypeResolver(); //# sourceMappingURL=delivery-item-strongly-type.resolver.js.map