kentico-cloud-delivery
Version:
Official Kentico Cloud Delivery SDK
75 lines • 3.24 kB
JavaScript
exports.__esModule = true;
var content_item_system_attributes_1 = require("../models/item/content-item-system-attributes");
var content_item_class_1 = require("../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 content_item_class_1.ContentItem();
// use typed 'system' property
contentItem.system = new content_item_system_attributes_1.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 content_item_system_attributes_1.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;
}());
exports.StronglyTypedResolver = StronglyTypedResolver;
exports.stronglyTypedResolver = new StronglyTypedResolver();
//# sourceMappingURL=strongly-type.resolver.js.map
;