UNPKG

kentico-cloud-delivery

Version:

Official Kentico Cloud Delivery SDK

73 lines 3.08 kB
import { ContentItemSystemAttributes } from '../models/item/content-item-system-attributes'; import { ContentItem } from '../models/item/content-item.class'; var StronglyTypedResolver = /** @class */ (function () { function StronglyTypedResolver() { } /** * Indicates if given type has a type resolver * @param type Type * @param resolvers Type resolvers */ StronglyTypedResolver.prototype.hasTypeResolver = function (type, resolvers) { return !(!resolvers.find(function (m) { return m.type === type; })); }; /** * Creates non generic ContentItem used when content type does not have a strongly typed model */ StronglyTypedResolver.prototype.createContentItem = function (item) { var contentItem = new ContentItem(); // use typed 'system' property contentItem.system = new ContentItemSystemAttributes({ name: item.system.name, codename: item.system.codename, id: item.system.id, lastModified: item.system.last_modified, language: item.system.language, type: item.system.type, sitemapLocations: item.system.sitemap_locations }); return contentItem; }; /** * Takes given type name and creates a strongly typed model based specified in client configuration * @param type Type of the content item * @param item Typed content item * @param resolvers Type resolvers */ StronglyTypedResolver.prototype.createTypedObj = function (type, item, typeResolvers) { var typedItem = this.createEmptyTypedObj(type, typeResolvers); if (!typedItem) { throw Error("Cannot find resolver for type '" + type + "'. This error means that no class was registered as TypeResolver"); } // use typed 'system' property typedItem.system = new ContentItemSystemAttributes({ name: item.system.name, codename: item.system.codename, id: item.system.id, lastModified: item.system.last_modified, language: item.system.language, type: item.system.type, sitemapLocations: item.system.sitemap_locations }); return typedItem; }; /** * Creates empty typed object of given type * @param type Type of the content item * @param resolvers Type resolvers */ StronglyTypedResolver.prototype.createEmptyTypedObj = function (type, resolvers) { if (!type) { throw Error('Cannot resolve type because no type name was provided'); } var typeResolver = resolvers.find(function (m) { return m.type === type; }); if (!typeResolver) { return undefined; } return typeResolver.resolve(); }; return StronglyTypedResolver; }()); export { StronglyTypedResolver }; export var stronglyTypedResolver = new StronglyTypedResolver(); //# sourceMappingURL=strongly-type.resolver.js.map