kentico-cloud-delivery
Version:
Official Kentico Cloud Delivery SDK
1,269 lines (1,191 loc) • 1.64 MB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(window, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./lib/index.ts");
/******/ })
/************************************************************************/
/******/ ({
/***/ "./lib/client/delivery-client.ts":
/*!***************************************!*\
!*** ./lib/client/delivery-client.ts ***!
\***************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var query_1 = __webpack_require__(/*! ../query */ "./lib/query/index.ts");
var sdk_info_generated_1 = __webpack_require__(/*! ../sdk-info.generated */ "./lib/sdk-info.generated.ts");
var services_1 = __webpack_require__(/*! ../services */ "./lib/services/index.ts");
var parser_adapter_1 = __webpack_require__(/*! ../parser/parser-adapter */ "./lib/parser/parser-adapter.ts");
var DeliveryClient = /** @class */ (function () {
/**
* Delivery client used to fetch data from Kentico Cloud
* @constructor
* @param {IDeliveryClientConfig} config - The client configuration
*/
function DeliveryClient(config) {
this.config = config;
if (!config) {
throw Error("Please provide Delivery client configuration");
}
this.queryService = new services_1.QueryService(config, new services_1.DeliveryHttpService(), parser_adapter_1.getParserAdapter(), {
host: sdk_info_generated_1.sdkInfo.host,
name: sdk_info_generated_1.sdkInfo.name,
version: sdk_info_generated_1.sdkInfo.version
});
}
/**
* Gets query for multiple types
*/
DeliveryClient.prototype.types = function () {
return new query_1.MultipleTypeQuery(this.config, this.queryService);
};
/**
* Gets query for single type
* @param {string} typeCodename - Codename of the type to fetch
*/
DeliveryClient.prototype.type = function (typeCodename) {
return new query_1.SingleTypeQuery(this.config, this.queryService, typeCodename);
};
/**
* Gets query for multiple items
*/
DeliveryClient.prototype.items = function () {
return new query_1.MultipleItemQuery(this.config, this.queryService);
};
/**
* Gets query for single item
* @param {string} codename - Codename of item to fetch
*/
DeliveryClient.prototype.item = function (codename) {
return new query_1.SingleItemQuery(this.config, this.queryService, codename);
};
/**
* Gets query for single taxonomy
* @param {string} codename - Codename of taxonomy to fetch
*/
DeliveryClient.prototype.taxonomy = function (codename) {
return new query_1.TaxonomyQuery(this.config, this.queryService, codename);
};
/**
* Gets query for multiple taxonomies
*/
DeliveryClient.prototype.taxonomies = function () {
return new query_1.TaxonomiesQuery(this.config, this.queryService);
};
/**
* Gets query for an element within a type
* @param {string} typeCodename - Codename of the type
* @param {string} elementCodename - Codename of the element
*/
DeliveryClient.prototype.element = function (typeCodename, elementCodename) {
return new query_1.ElementQuery(this.config, this.queryService, typeCodename, elementCodename);
};
return DeliveryClient;
}());
exports.DeliveryClient = DeliveryClient;
/***/ }),
/***/ "./lib/client/index.ts":
/*!*****************************!*\
!*** ./lib/client/index.ts ***!
\*****************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
__export(__webpack_require__(/*! ./delivery-client */ "./lib/client/delivery-client.ts"));
/***/ }),
/***/ "./lib/fields/field-decorators.ts":
/*!****************************************!*\
!*** ./lib/fields/field-decorators.ts ***!
\****************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
__webpack_require__(/*! reflect-metadata */ "./node_modules/reflect-metadata/Reflect.js");
var FieldDecorators;
(function (FieldDecorators) {
var codenameMetadataKey = Symbol('codename');
var generateKey = function (name) {
return codenameMetadataKey.toString() + ":" + name;
};
/**
* Get the metadata entry saved by the decorator
* @param target - object instance
* @param fieldName - field name (code name from Kentico Cloud)
*/
function getPropertyName(target, fieldName) {
return Reflect.getMetadata(generateKey(fieldName), target);
}
FieldDecorators.getPropertyName = getPropertyName;
/**
* Decorator - reates metadata entry for the @target - Value is the property name.
* This will then be retrieved in the FieldMap service when resolving the field name
* @param value - field code name
*/
function codename(value) {
return function (target, propertyKey) {
Reflect.defineMetadata(generateKey(value), propertyKey, target);
};
}
FieldDecorators.codename = codename;
})(FieldDecorators = exports.FieldDecorators || (exports.FieldDecorators = {}));
/***/ }),
/***/ "./lib/fields/field-models.ts":
/*!************************************!*\
!*** ./lib/fields/field-models.ts ***!
\************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var FieldModels;
(function (FieldModels) {
var AssetModel = /** @class */ (function () {
/**
* Represents Kentico Cloud's asset
* @constructor
* @param {string} name - Name of the asset
* @param {string} type - Type of the asset
* @param {number} size - Size of the asset
* @param {string} description - Description of the asset
* @param {string} url - Url of the asset
*/
function AssetModel(name, type, size, description, url) {
this.name = name;
this.type = type;
this.size = size;
this.description = description;
this.url = url;
}
return AssetModel;
}());
FieldModels.AssetModel = AssetModel;
var MultipleChoiceOption = /** @class */ (function () {
/**
* Represents Kentico Cloud's multiple choice option
* @constructor
* @param {string} name - Name of the option
* @param {string} codename - Codename of the option
*/
function MultipleChoiceOption(name, codename) {
this.name = name;
this.codename = codename;
}
return MultipleChoiceOption;
}());
FieldModels.MultipleChoiceOption = MultipleChoiceOption;
var TaxonomyTerm = /** @class */ (function () {
/**
* Represents taxonomy term
* @constructor
* @param {string} name - Name of the taxonomy option
* @param {string} codename - Codename of the option
*/
function TaxonomyTerm(name, codename) {
this.name = name;
this.codename = codename;
}
return TaxonomyTerm;
}());
FieldModels.TaxonomyTerm = TaxonomyTerm;
})(FieldModels = exports.FieldModels || (exports.FieldModels = {}));
/***/ }),
/***/ "./lib/fields/field-type.ts":
/*!**********************************!*\
!*** ./lib/fields/field-type.ts ***!
\**********************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
/**
* Represents codename of Kentico Cloud field types
*/
var FieldType;
(function (FieldType) {
FieldType["Text"] = "text";
FieldType["Number"] = "number";
FieldType["ModularContent"] = "modular_content";
FieldType["Asset"] = "asset";
FieldType["DateTime"] = "date_time";
FieldType["RichText"] = "rich_text";
FieldType["MultipleChoice"] = "multiple_choice";
FieldType["UrlSlug"] = "url_slug";
FieldType["Taxonomy"] = "taxonomy";
})(FieldType = exports.FieldType || (exports.FieldType = {}));
/***/ }),
/***/ "./lib/fields/field-types.ts":
/*!***********************************!*\
!*** ./lib/fields/field-types.ts ***!
\***********************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var field_models_1 = __webpack_require__(/*! ./field-models */ "./lib/fields/field-models.ts");
var field_type_1 = __webpack_require__(/*! ./field-type */ "./lib/fields/field-type.ts");
var Fields;
(function (Fields) {
var TextField = /** @class */ (function () {
/**
* Represents text field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
*/
function TextField(name, value) {
this.name = name;
this.value = value;
/**
* Type of the field
*/
this.type = field_type_1.FieldType.Text;
this.text = this.value;
}
return TextField;
}());
Fields.TextField = TextField;
var MultipleChoiceField = /** @class */ (function () {
/**
* Represents multiple choice field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
*/
function MultipleChoiceField(name, value) {
var _this = this;
this.name = name;
this.value = value;
/**
* Multiple choice options
*/
this.options = [];
/**
* Type of the field
*/
this.type = field_type_1.FieldType.MultipleChoice;
if (this.value && Array.isArray(this.value)) {
this.value.forEach(function (option) {
var optionTemp = option;
_this.options.push(new field_models_1.FieldModels.MultipleChoiceOption(optionTemp.name, optionTemp.codename));
});
}
}
return MultipleChoiceField;
}());
Fields.MultipleChoiceField = MultipleChoiceField;
var DateTimeField = /** @class */ (function () {
/**
* Represents date time field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
*/
function DateTimeField(name, value) {
this.name = name;
this.value = value;
/**
* Type of the field
*/
this.type = field_type_1.FieldType.DateTime;
this.datetime = new Date(value);
}
return DateTimeField;
}());
Fields.DateTimeField = DateTimeField;
var RichTextField = /** @class */ (function () {
/**
* Represents rich text field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
* @param {() => string} resolveHtml - Function that resolves HTML
* @param {Link[]} links - Links for this rich text field
*/
function RichTextField(name, value, data) {
this.name = name;
this.value = value;
/**
* Type of the field
*/
this.type = field_type_1.FieldType.RichText;
/**
* Links
*/
this.links = [];
Object.assign(this, data);
}
RichTextField.prototype.getHtml = function () {
// check if html was already resolved
if (this.resolvedHtml) {
return this.resolvedHtml;
}
this.resolvedHtml = this.resolveHtml();
return this.resolvedHtml;
};
return RichTextField;
}());
Fields.RichTextField = RichTextField;
var NumberField = /** @class */ (function () {
/**
* Represents number field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
*/
function NumberField(name, value) {
this.name = name;
this.value = value;
/**
* Type of the field
*/
this.type = field_type_1.FieldType.Number;
this.number = value;
}
return NumberField;
}());
Fields.NumberField = NumberField;
var AssetsField = /** @class */ (function () {
/**
* Represents asset field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {any} value - Value of the field
*/
function AssetsField(name, value) {
var _this = this;
this.name = name;
this.value = value;
/**
* Type of the field
*/
this.type = field_type_1.FieldType.Asset;
/**
* List of assets used in this field
*/
this.assets = [];
if (!value) {
throw Error("Cannot bind assets field because no value was provided");
}
if (!Array.isArray(value)) {
throw Error("Cannot bind assets because the provided value is not an array");
}
this.value.forEach(function (asset) {
_this.assets.push(new field_models_1.FieldModels.AssetModel(asset.name, asset.type, asset.size, asset.description, asset.url));
});
}
return AssetsField;
}());
Fields.AssetsField = AssetsField;
var UrlSlugField = /** @class */ (function () {
/**
* Represents URL slug field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
* @param {() => string | undefined} resolveUrl - Function to get url of the link
*/
function UrlSlugField(name, value, data) {
this.name = name;
this.value = value;
/**
* Type of the field
*/
this.type = field_type_1.FieldType.UrlSlug;
Object.assign(this, data);
}
UrlSlugField.prototype.getUrl = function () {
if (this.resolvedUrl) {
return this.resolvedUrl;
}
this.resolvedUrl = this.resolveUrl();
return this.resolvedUrl;
};
return UrlSlugField;
}());
Fields.UrlSlugField = UrlSlugField;
var TaxonomyField = /** @class */ (function () {
/**
* Represents number field of Kentico Cloud item
* @constructor
* @param {string} name - Name of the field
* @param {string} value - Value of the field
* @param {string | undefined} taxonomyGroup - Codename of the taxonomy group
*/
function TaxonomyField(name, value, taxonomyGroup) {
var _this = this;
this.name = name;
this.value = value;
this.taxonomyGroup = taxonomyGroup;
/**
* Type of the field
*/
this.type = field_type_1.FieldType.Taxonomy;
/**
* List of assigned taxonomy terms
*/
this.taxonomyTerms = [];
if (!value) {
throw Error("Cannot map taxonomy field because no value was provided");
}
if (!Array.isArray(value)) {
throw Error("Cannot get taxonomy field because the provided value is not an array");
}
var taxonomyList = value;
taxonomyList.forEach(function (term) {
_this.taxonomyTerms.push(new field_models_1.FieldModels.TaxonomyTerm(term.name, term.codename));
});
}
return TaxonomyField;
}());
Fields.TaxonomyField = TaxonomyField;
})(Fields = exports.Fields || (exports.Fields = {}));
/***/ }),
/***/ "./lib/fields/index.ts":
/*!*****************************!*\
!*** ./lib/fields/index.ts ***!
\*****************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
__export(__webpack_require__(/*! ./field-decorators */ "./lib/fields/field-decorators.ts"));
__export(__webpack_require__(/*! ./field-models */ "./lib/fields/field-models.ts"));
__export(__webpack_require__(/*! ./field-type */ "./lib/fields/field-type.ts"));
__export(__webpack_require__(/*! ./field-types */ "./lib/fields/field-types.ts"));
/***/ }),
/***/ "./lib/index.ts":
/*!**********************!*\
!*** ./lib/index.ts ***!
\**********************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
/* client */
__export(__webpack_require__(/*! ./client */ "./lib/client/index.ts"));
// sdk info */
__export(__webpack_require__(/*! ./sdk-info.generated */ "./lib/sdk-info.generated.ts"));
/* fields */
__export(__webpack_require__(/*! ./fields */ "./lib/fields/index.ts"));
/* parser */
__export(__webpack_require__(/*! ./parser */ "./lib/parser/index.ts"));
/* resolvers */
__export(__webpack_require__(/*! ./resolvers */ "./lib/resolvers/index.ts"));
/* services */
__export(__webpack_require__(/*! ./services */ "./lib/services/index.ts"));
/* mappers */
__export(__webpack_require__(/*! ./mappers */ "./lib/mappers/index.ts"));
/* queries */
__export(__webpack_require__(/*! ./query */ "./lib/query/index.ts"));
/* models */
__export(__webpack_require__(/*! ./models */ "./lib/models/index.ts"));
/***/ }),
/***/ "./lib/mappers/element.mapper.ts":
/*!***************************************!*\
!*** ./lib/mappers/element.mapper.ts ***!
\***************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var models_1 = __webpack_require__(/*! ../models */ "./lib/models/index.ts");
var ElementMapper = /** @class */ (function () {
function ElementMapper() {
}
ElementMapper.prototype.mapElement = function (response) {
if (!response) {
throw Error("Invalid response for mapping element");
}
return new models_1.Element({
codename: response.codename,
name: response.name,
type: response.type,
options: response.options ? response.options : [],
taxonomyGroup: response.taxonomy_group
});
};
return ElementMapper;
}());
exports.ElementMapper = ElementMapper;
/***/ }),
/***/ "./lib/mappers/field.mapper.ts":
/*!*************************************!*\
!*** ./lib/mappers/field.mapper.ts ***!
\*************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var fields_1 = __webpack_require__(/*! ../fields */ "./lib/fields/index.ts");
var models_1 = __webpack_require__(/*! ../models */ "./lib/models/index.ts");
var resolvers_1 = __webpack_require__(/*! ../resolvers */ "./lib/resolvers/index.ts");
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 && resolvers_1.stronglyTypedResolver.hasTypeResolver(item.system.type, this.config.typeResolvers)) {
itemTyped = resolvers_1.stronglyTypedResolver.createTypedObj(item.system.type, item, this.config.typeResolvers);
}
else {
itemTyped = resolvers_1.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 = fields_1.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 === fields_1.FieldType.ModularContent.toString()) {
return this.mapModularField(field, modularContent, queryConfig, processedItems);
}
else if (fieldType === fields_1.FieldType.Text.toLowerCase()) {
return this.mapTextField(field);
}
else if (fieldType === fields_1.FieldType.Asset.toLowerCase()) {
return this.mapAssetsField(field);
}
else if (fieldType === fields_1.FieldType.Number.toLowerCase()) {
return this.mapNumberField(field);
}
else if (fieldType === fields_1.FieldType.MultipleChoice.toLowerCase()) {
return this.mapMultipleChoiceField(field);
}
else if (fieldType === fields_1.FieldType.DateTime.toLowerCase()) {
return this.mapDateTimeField(field);
}
else if (fieldType === fields_1.FieldType.RichText.toLowerCase()) {
return this.mapRichTextField(field, modularContent, queryConfig, processedItems);
}
else if (fieldType === fields_1.FieldType.UrlSlug.toLowerCase()) {
return this.mapUrlSlugField(field, item, queryConfig);
}
else if (fieldType === fields_1.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_1.Fields.RichTextField(field.name, field.value, {
links: links,
resolveHtml: function () { return resolvers_1.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_1.Fields.DateTimeField(field.name, field.value);
};
FieldMapper.prototype.mapMultipleChoiceField = function (field) {
return new fields_1.Fields.MultipleChoiceField(field.name, field.value);
};
FieldMapper.prototype.mapNumberField = function (field) {
return new fields_1.Fields.NumberField(field.name, field.value);
};
FieldMapper.prototype.mapTextField = function (field) {
return new fields_1.Fields.TextField(field.name, field.value);
};
FieldMapper.prototype.mapAssetsField = function (field) {
return new fields_1.Fields.AssetsField(field.name, field.value);
};
FieldMapper.prototype.mapTaxonomyField = function (field) {
return new fields_1.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_1.Fields.UrlSlugField(field.name, field.value, {
resolveUrl: function () { return resolvers_1.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 models_1.Link({
codename: linkRaw.codename,
itemId: linkItemId,
urlSlug: linkRaw.url_slug,
type: linkRaw.type
}));
}
return links;
};
return FieldMapper;
}());
exports.FieldMapper = FieldMapper;
/***/ }),
/***/ "./lib/mappers/index.ts":
/*!******************************!*\
!*** ./lib/mappers/index.ts ***!
\******************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
__export(__webpack_require__(/*! ./element.mapper */ "./lib/mappers/element.mapper.ts"));
__export(__webpack_require__(/*! ./field.mapper */ "./lib/mappers/field.mapper.ts"));
__export(__webpack_require__(/*! ./item.mapper */ "./lib/mappers/item.mapper.ts"));
__export(__webpack_require__(/*! ./response.mapper */ "./lib/mappers/response.mapper.ts"));
__export(__webpack_require__(/*! ./taxonomy.mapper */ "./lib/mappers/taxonomy.mapper.ts"));
__export(__webpack_require__(/*! ./type.mapper */ "./lib/mappers/type.mapper.ts"));
/***/ }),
/***/ "./lib/mappers/item.mapper.ts":
/*!************************************!*\
!*** ./lib/mappers/item.mapper.ts ***!
\************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var mappers_1 = __webpack_require__(/*! ../mappers */ "./lib/mappers/index.ts");
var ItemMapper = /** @class */ (function () {
function ItemMapper(config, richTextHtmlParser) {
this.config = config;
this.richTextHtmlParser = richTextHtmlParser;
this.fieldMapper = new mappers_1.FieldMapper(config, richTextHtmlParser);
}
/**
* Maps single item to its proper strongly typed model from the given Cloud response
* @param response Cloud response used to map the item
* @param queryConfig Query configuration
*/
ItemMapper.prototype.mapSingleItem = function (response, queryConfig) {
return this.mapItem(response.item, response.modular_content, queryConfig);
};
/**
* Maps multiple items to their strongly typed model from the given Cloud response
* @param response Cloud response used to map the item
* @param queryConfig Query configuration
*/
ItemMapper.prototype.mapMultipleItems = function (response, queryConfig) {
var that = this;
return response.items.map(function (item) {
return that.mapItem(item, response.modular_content, queryConfig);
});
};
ItemMapper.prototype.mapItem = function (item, modularContent, queryConfig) {
if (item == null) {
throw Error("Could not map item because its undefined");
}
return this.fieldMapper.mapFields(item, modularContent, queryConfig, []);
};
return ItemMapper;
}());
exports.ItemMapper = ItemMapper;
/***/ }),
/***/ "./lib/mappers/response.mapper.ts":
/*!****************************************!*\
!*** ./lib/mappers/response.mapper.ts ***!
\****************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var models_1 = __webpack_require__(/*! ../models */ "./lib/models/index.ts");
var element_mapper_1 = __webpack_require__(/*! ./element.mapper */ "./lib/mappers/element.mapper.ts");
var item_mapper_1 = __webpack_require__(/*! ./item.mapper */ "./lib/mappers/item.mapper.ts");
var taxonomy_mapper_1 = __webpack_require__(/*! ./taxonomy.mapper */ "./lib/mappers/taxonomy.mapper.ts");
var type_mapper_1 = __webpack_require__(/*! ./type.mapper */ "./lib/mappers/type.mapper.ts");
var ResponseMapper = /** @class */ (function () {
function ResponseMapper(config, richTextHtmlParser) {
this.config = config;
this.richTextHtmlParser = richTextHtmlParser;
this.typeMapper = new type_mapper_1.TypeMapper();
this.itemMapper = new item_mapper_1.ItemMapper(config, richTextHtmlParser);
this.taxonomyMapper = new taxonomy_mapper_1.TaxonomyMapper();
this.elementMapper = new element_mapper_1.ElementMapper();
}
/**
* Gets response for getting a single type
* @param response Response data
*/
ResponseMapper.prototype.mapSingleTypeResponse = function (response) {
var cloudResponse = response.data;
// map type
var type = this.typeMapper.mapSingleType(cloudResponse);
return new models_1.TypeResponses.DeliveryTypeResponse(type, this.mapResponseDebug(response));
};
/**
* Gets resposne for getting multiple types
* @param response Response data
* @param options Options
*/
ResponseMapper.prototype.mapMultipleTypeResponse = function (response) {
var cloudResponse = response.data;
// map types
var types = this.typeMapper.mapMultipleTypes(cloudResponse);
// pagination
var pagination = new models_1.Pagination({
skip: cloudResponse.pagination.skip,
count: cloudResponse.pagination.count,
limit: cloudResponse.pagination.limit,
next_page: cloudResponse.pagination.next_page
});
return new models_1.TypeResponses.DeliveryTypeListingResponse(types, pagination, this.mapResponseDebug(response));
};
/**
* Gets response for getting single item
* @param response Response data
* @param queryConfig Query configuration
*/
ResponseMapper.prototype.mapSingleResponse = function (response, queryConfig) {
var cloudResponse = response.data;
// map item
var item = this.itemMapper.mapSingleItem(cloudResponse, queryConfig);
return new models_1.ItemResponses.DeliveryItemResponse(item, this.mapResponseDebug(response));
};
/**
* Gets response for getting multiple items
* @param response Response data
* @param queryConfig Query configuration
*/
ResponseMapper.prototype.mapMultipleResponse = function (response, queryConfig) {
var cloudResponse = response.data;
// map items
var items = this.itemMapper.mapMultipleItems(cloudResponse, queryConfig);
// pagination
var pagination = new models_1.Pagination({
skip: cloudResponse.pagination.skip,
count: cloudResponse.pagination.count,
limit: cloudResponse.pagination.limit,
next_page: cloudResponse.pagination.next_page
});
return new models_1.ItemResponses.DeliveryItemListingResponse(items, pagination, this.mapResponseDebug(response));
};
/**
* Gets response for getting single taxonomy item
* @param response Response data
*/
ResponseMapper.prototype.mapTaxonomyResponse = function (response) {
var cloudResponse = response.data;
// map taxonomy
var taxonomy = this.taxonomyMapper.mapTaxonomy(cloudResponse.system, cloudResponse.terms);
return new models_1.TaxonomyResponses.TaxonomyResponse(taxonomy, this.mapResponseDebug(response));
};
/**
* Gets response for getting multiples taxonomies
* @param response Response data
*/
ResponseMapper.prototype.mapTaxonomiesResponse = function (response) {
var cloudResponse = response.data;
// map taxonomies
var taxonomies = this.taxonomyMapper.mapTaxonomies(cloudResponse.taxonomies);
// pagination
var pagination = new models_1.Pagination({
skip: cloudResponse.pagination.skip,
count: cloudResponse.pagination.count,
limit: cloudResponse.pagination.limit,
next_page: cloudResponse.pagination.next_page
});
return new models_1.TaxonomyResponses.TaxonomiesResponse(taxonomies, pagination, this.mapResponseDebug(response));
};
/**
* Gets response for getting single content type element
* @param response Response data
*/
ResponseMapper.prototype.mapElementResponse = function (response) {
var cloudResponse = response.data;
// map element
var element = this.elementMapper.mapElement(cloudResponse);
return new models_1.ElementResponses.ElementResponse(element, this.mapResponseDebug(response));
};
ResponseMapper.prototype.mapResponseDebug = function (response) {
if (!response) {
throw Error("Cannot map 'debug' model from the response");
}
return {
response: response.response
};
};
return ResponseMapper;
}());
exports.ResponseMapper = ResponseMapper;
/***/ }),
/***/ "./lib/mappers/taxonomy.mapper.ts":
/*!****************************************!*\
!*** ./lib/mappers/taxonomy.mapper.ts ***!
\****************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var models_1 = __webpack_require__(/*! ../models */ "./lib/models/index.ts");
var TaxonomyMapper = /** @class */ (function () {
function TaxonomyMapper() {
}
TaxonomyMapper.prototype.mapTaxonomy = function (taxonomySystem, taxonomyTerms) {
if (!taxonomySystem) {
throw Error("Cannot map taxonomy due to missing 'system' property");
}
if (!taxonomyTerms) {
throw Error("Cannot map taxonomy due to missing 'terms' property");
}
if (!Array.isArray(taxonomyTerms)) {
throw Error("Cannot map terms because no terms array was provided");
}
var mappedSystemAttributes = new models_1.TaxonomySystemAttributes({
name: taxonomySystem.name,
codename: taxonomySystem.codename,
id: taxonomySystem.id,
lastModified: taxonomySystem.last_modified
});
var mappedTerms = this.mapTaxonomyTerms(taxonomyTerms);
return new models_1.TaxonomyGroup(mappedSystemAttributes, mappedTerms);
};
TaxonomyMapper.prototype.mapTaxonomies = function (taxonomies) {
var _this = this;
if (!taxonomies) {
throw Error("Cannot map taxonomy due to missing 'taxonomies' property");
}
if (!Array.isArray(taxonomies)) {
throw Error("Cannot map taxonomies because the 'taxonomies' property is not an array ");
}
var mappedTaxonomies = [];
taxonomies.forEach(function (taxonomy) {
mappedTaxonomies.push(_this.mapTaxonomy(taxonomy.system, taxonomy.terms));
});
return mappedTaxonomies;
};
/**
* Recursively map array of taxonomy terms
* @param termsArray Terms array to map
*/
TaxonomyMapper.prototype.mapTaxonomyTerms = function (termsArray) {
var _this = this;
if (termsArray.length === 0) {
return [];
}
var mappedTermsArray = [];
termsArray.forEach(function (terms) {
var mappedTerms = new models_1.TaxonomyTerms({
codename: terms.codename,
name: terms.name,
terms: _this.mapTaxonomyTerms(terms.terms)
});
mappedTermsArray.push(mappedTerms);
});
return mappedTermsArray;
};
return TaxonomyMapper;
}());
exports.TaxonomyMapper = TaxonomyMapper;
/***/ }),
/***/ "./lib/mappers/type.mapper.ts":
/*!************************************!*\
!*** ./lib/mappers/type.mapper.ts ***!
\************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var models_1 = __webpack_require__(/*! ../models */ "./lib/models/index.ts");
var TypeMapper = /** @class */ (function () {
function TypeMapper() {
}
TypeMapper.prototype.mapSingleType = function (response) {
return this.mapType(response);
};
TypeMapper.prototype.mapMultipleTypes = function (response) {
var that = this;
return response.types.map(function (type) {
return that.mapType(type);
});
};
TypeMapper.prototype.mapType = function (type) {
if (!type) {
throw Error("Cannot map type");
}
if (!type.elements) {
throw Error("Cannot map type elements");
}
var typeSystem = new models_1.ContentTypeSystemAttributes({
codename: type.system.codename,
id: type.system.id,
name: type.system.name,
lastModified: type.system.last_modified
});
var elements = [];
var elementNames = Object.getOwnPropertyNames(type.elements);
elementNames.forEach(function (elementName) {
var typeElement = type.elements[elementName];
if (!typeElement) {
throw Error("Cannot find element '" + elementName + "' on type '" + type + "'");
}
// use json property as a codename of the type element
var elementCodename = elementName;
// extra properties for certain field types
var taxonomyGroup = typeElement.taxonomy_group;
var options = [];
// some elements can contain options
var rawOptions = typeElement.options;
if (rawOptions) {
if (!Array.isArray(rawOptions)) {
throw Error("Content type 'options' property has to be an array");
}
rawOptions.forEach(function (rawOption) {
options.push(new models_1.ElementOption(rawOption.name, rawOption.codename));
});
}
elements.push(new models_1.Element({
codename: elementCodename,
taxonomyGroup: taxonomyGroup,
options: options,
name: typeElement.name,
type: typeElement.type
}));
});
return new models_1.ContentType({
system: typeSystem,
elements: elements
});
};
return TypeMapper;
}());
exports.TypeMapper = TypeMapper;
/***/ }),
/***/ "./lib/models/common/cloud-error.class.ts":
/*!************************************************!*\
!*** ./lib/models/common/cloud-error.class.ts ***!
\************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
exports.__esModule = true;
var DeliveryCloudError = /** @class */ (function () {
function DeliveryCloudError(data) {
Object.assign(this, data);
}
return DeliveryCloudError;
}());
exports.DeliveryCloudError = Deliv