contentful
Advanced tools
Comparing version 3.0.8 to 3.1.1
@@ -26,2 +26,6 @@ 'use strict'; | ||
var _stringifySafe = require('../mixins/stringify-safe'); | ||
var _stringifySafe2 = _interopRequireDefault(_stringifySafe); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -56,2 +60,3 @@ | ||
* @prop {function(): Object} toPlainObject() - Returns this Entry collection as a plain JS object | ||
* @prop {function(?function=, space=): Object} stringifySafe(replacer,space) - Stringifies the entry collection, accounting for circular references. Circular references will be replaced with just a Link object, with a <code>circular</code> property set to <code>true</code>. See <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify">MDN</a> and <a href="https://www.npmjs.com/package/json-stringify-safe">json-stringify-safe</a> for more details on the arguments this method can take. | ||
*/ | ||
@@ -66,3 +71,3 @@ | ||
function wrapEntryCollection(data, resolveLinks) { | ||
var wrappedData = (0, _toPlainObject2.default)((0, _lang.cloneDeep)(data)); | ||
var wrappedData = (0, _stringifySafe2.default)((0, _toPlainObject2.default)((0, _lang.cloneDeep)(data))); | ||
if (resolveLinks) { | ||
@@ -69,0 +74,0 @@ var includes = prepareIncludes(wrappedData.includes, wrappedData.items); |
@@ -28,91 +28,94 @@ 'use strict'; | ||
function mixinLinkGetters(items, includes) { | ||
var linkGetter = (0, _function.memoize)(getLinksFromIncludes, memoizationResolver); | ||
(0, _collection.each)(items, function (item) { | ||
return setLocalizedFieldGetters(item.fields, includes, !!item.sys.locale); | ||
return setLocalizedFieldGetters(item.fields, !!item.sys.locale); | ||
}); | ||
} | ||
/** | ||
* If a field does not have a locale defined in sys, the content of that field | ||
* is an object where the keys are each available locale, and we need to iterate | ||
* over each of those | ||
* @private | ||
* @param {Object} fields - Fields object | ||
* @param {Object} includes - Object with lists of Entry, Asset, DeletedEntry and DeletedAsset | ||
* @param {boolean} hasLocale - If entry has been requested with a locale | ||
*/ | ||
function setLocalizedFieldGetters(fields, includes, hasLocale) { | ||
if (hasLocale) { | ||
setFieldGettersForFields(fields, includes); | ||
} else { | ||
(0, _collection.each)(fields, function (localizedField) { | ||
return setFieldGettersForFields(localizedField, includes); | ||
/** | ||
* If a field does not have a locale defined in sys, the content of that field | ||
* is an object where the keys are each available locale, and we need to iterate | ||
* over each of those | ||
* @private | ||
* @param {Object} fields - Fields object | ||
* @param {boolean} hasLocale - If entry has been requested with a locale | ||
*/ | ||
function setLocalizedFieldGetters(fields, hasLocale) { | ||
if (hasLocale) { | ||
setFieldGettersForFields(fields); | ||
} else { | ||
(0, _collection.each)(fields, function (localizedField) { | ||
return setFieldGettersForFields(localizedField); | ||
}); | ||
} | ||
} | ||
/** | ||
* Sets getters on each link field or list of link fields for each item | ||
* @private | ||
* @param {Object} fields - Fields object | ||
*/ | ||
function setFieldGettersForFields(fields) { | ||
(0, _collection.each)(fields, function (field, fieldKey) { | ||
if (Array.isArray(field)) { | ||
addGetterForLinkArray(field, fieldKey, fields); | ||
} else { | ||
addGetterForLink(field, fieldKey, fields); | ||
} | ||
}); | ||
} | ||
} | ||
/** | ||
* Sets getters on each link field or list of link fields for each item | ||
* @private | ||
* @param {Object} fields - Fields object | ||
* @param {Object} includes - Object with lists of Entry, Asset, DeletedEntry and DeletedAsset | ||
*/ | ||
function setFieldGettersForFields(fields, includes) { | ||
(0, _collection.each)(fields, function (field, fieldKey) { | ||
if (Array.isArray(field)) { | ||
addGetterForLinkArray(field, fieldKey, fields, includes); | ||
} else { | ||
addGetterForLink(field, fieldKey, fields, includes); | ||
/** | ||
* Sets a getter which resolves the link of the given fieldKey in fields | ||
* @private | ||
* @param {Object} field - Field object | ||
* @param {string} fieldKey | ||
* @param {Object} fields - Fields object | ||
*/ | ||
function addGetterForLink(field, fieldKey, fields) { | ||
if ((0, _object.get)(field, 'sys.type') === 'Link') { | ||
(0, _defineProperty2.default)(fields, fieldKey, { | ||
get: (0, _function.partial)(linkGetter, field) | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
/** | ||
* Sets a getter which resolves the link of the given fieldKey in fields | ||
* @private | ||
* @param {Object} field - Field object | ||
* @param {string} fieldKey | ||
* @param {Object} fields - Fields object | ||
* @param {Object} includes - Object with lists of Entry, Asset, DeletedEntry and DeletedAsset | ||
*/ | ||
function addGetterForLink(field, fieldKey, fields, includes) { | ||
if ((0, _object.get)(field, 'sys.type') === 'Link') { | ||
(0, _defineProperty2.default)(fields, fieldKey, { | ||
get: (0, _function.memoize)((0, _function.partial)(linkGetter, includes, field)) | ||
}); | ||
/** | ||
* Sets a getter which resolves the array of links of the given fieldKey in fields | ||
* @private | ||
* @param {Array<Object>} field - List field array | ||
* @param {string} fieldKey | ||
* @param {Object} fields - Fields object | ||
*/ | ||
function addGetterForLinkArray(listField, fieldKey, fields) { | ||
if ((0, _object.get)(listField[0], 'sys.type') === 'Link') { | ||
(0, _defineProperty2.default)(fields, fieldKey, { | ||
get: function get() { | ||
return (0, _collection.map)(listField, (0, _function.partial)(linkGetter)); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
/** | ||
* Sets a getter which resolves the array of links of the given fieldKey in fields | ||
* @private | ||
* @param {Array<Object>} field - List field array | ||
* @param {string} fieldKey | ||
* @param {Object} fields - Fields object | ||
* @param {Object} includes - Object with lists of Entry, Asset, DeletedEntry and DeletedAsset | ||
*/ | ||
function addGetterForLinkArray(listField, fieldKey, fields, includes) { | ||
if ((0, _object.get)(listField[0], 'sys.type') === 'Link') { | ||
(0, _defineProperty2.default)(fields, fieldKey, { | ||
get: (0, _function.memoize)(function () { | ||
return (0, _collection.map)(listField, (0, _function.partial)(linkGetter, includes)); | ||
}) | ||
}); | ||
/** | ||
* Looks for given link field in includes. | ||
* If linked entity is not found, it returns the original link. | ||
* This method shouldn't be used directly, and it's memoized whenever this | ||
* module's main method is used. | ||
* This is done to prevent the same link being resolved multiple times. | ||
* @private | ||
* @param {Object} field - Field object | ||
* @return {Object} Field, or link if field not resolved | ||
*/ | ||
function getLinksFromIncludes(field) { | ||
var link = (0, _collection.find)(includes[field.sys.linkType], ['sys.id', field.sys.id]); | ||
if (link && link.fields) { | ||
setLocalizedFieldGetters(link.fields, !!link.sys.locale); | ||
return link; | ||
} | ||
return field; | ||
} | ||
} | ||
/** | ||
* Looks for given link field in includes. | ||
* If linked entity is not found, it returns the original link. | ||
* @private | ||
* @param {Object} field - Field object | ||
* @param {Object} includes - Object with lists of Entry, Asset, DeletedEntry and DeletedAsset | ||
* @return {Object} Field, or link if field not resolved | ||
*/ | ||
function linkGetter(includes, field) { | ||
var link = (0, _collection.find)(includes[field.sys.linkType], ['sys.id', field.sys.id]); | ||
if (link && link.fields) { | ||
setLocalizedFieldGetters(link.fields, includes, !!link.sys.locale); | ||
return link; | ||
function memoizationResolver(link) { | ||
return link.sys.id; | ||
} | ||
return field; | ||
} |
@@ -52,2 +52,3 @@ { | ||
"follow-redirects": "0.0.7", | ||
"json-stringify-safe": "^5.0.1", | ||
"lodash": "^4.2.0" | ||
@@ -95,3 +96,3 @@ }, | ||
}, | ||
"version": "3.0.8" | ||
} | ||
"version": "3.1.1" | ||
} |
@@ -1,1 +0,1 @@ | ||
module.exports = '3.0.8' | ||
module.exports = '3.1.1' |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
801427
38
20836
4
+ Addedjson-stringify-safe@^5.0.1
+ Addedjson-stringify-safe@5.0.1(transitive)