contentful-resolve-response
Advanced tools
Comparing version 1.9.1 to 1.9.2
@@ -1,17 +0,37 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fast_copy_1 = require("fast-copy"); | ||
const UNRESOLVED_LINK = {}; // Unique object to avoid polyfill bloat using Symbol() | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _fastCopy = require('fast-copy'); | ||
var _fastCopy2 = _interopRequireDefault(_fastCopy); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
var UNRESOLVED_LINK = {}; // unique object to avoid polyfill bloat using Symbol() | ||
/** | ||
* IsLink Function | ||
* isLink Function | ||
* Checks if the object has sys.type "Link" | ||
* @param object | ||
*/ | ||
const isLink = (object) => object && object.sys && object.sys.type === 'Link'; | ||
var isLink = function isLink(object) { | ||
return object && object.sys && object.sys.type === 'Link'; | ||
}; | ||
/** | ||
* IsResourceLink Function | ||
* isResourceLink Function | ||
* Checks if the object has sys.type "ResourceLink" | ||
* @param object | ||
*/ | ||
const isResourceLink = (object) => object && object.sys && object.sys.type === 'ResourceLink'; | ||
var isResourceLink = function isResourceLink(object) { | ||
return object && object.sys && object.sys.type === 'ResourceLink'; | ||
}; | ||
/** | ||
@@ -28,8 +48,10 @@ * Creates a key with spaceId and a key without for entityMap | ||
*/ | ||
const makeEntityMapKeys = (sys) => { | ||
if (sys.space && sys.environment) { | ||
return [`${sys.type}!${sys.id}`, `${sys.space.sys.id}!${sys.environment.sys.id}!${sys.type}!${sys.id}`]; | ||
} | ||
return [`${sys.type}!${sys.id}`]; | ||
var makeEntityMapKeys = function makeEntityMapKeys(sys) { | ||
if (sys.space && sys.environment) { | ||
return [`${sys.type}!${sys.id}`, `${sys.space.sys.id}!${sys.environment.sys.id}!${sys.type}!${sys.id}`]; | ||
} | ||
return [`${sys.type}!${sys.id}`]; | ||
}; | ||
/** | ||
@@ -46,20 +68,38 @@ * Looks up in entityMap | ||
*/ | ||
const lookupInEntityMap = (entityMap, linkData) => { | ||
const { entryId, linkType, spaceId, environmentId } = linkData; | ||
if (spaceId && environmentId) { | ||
return entityMap.get(`${spaceId}!${environmentId}!${linkType}!${entryId}`); | ||
} | ||
return entityMap.get(`${linkType}!${entryId}`); | ||
var lookupInEntityMap = function lookupInEntityMap(entityMap, linkData) { | ||
var entryId = linkData.entryId, | ||
linkType = linkData.linkType, | ||
spaceId = linkData.spaceId, | ||
environmentId = linkData.environmentId; | ||
if (spaceId && environmentId) { | ||
return entityMap.get(`${spaceId}!${environmentId}!${linkType}!${entryId}`); | ||
} | ||
return entityMap.get(`${linkType}!${entryId}`); | ||
}; | ||
const getIdsFromUrn = (urn) => { | ||
const regExp = /.*:spaces\/([^/]+)(?:\/environments\/([^/]+))?\/entries\/([^/]+)$/; | ||
if (!regExp.test(urn)) { | ||
return undefined; | ||
} | ||
// eslint-disable-next-line no-unused-vars | ||
const [_, spaceId, environmentId = 'master', entryId] = urn.match(regExp); | ||
return { spaceId, environmentId, entryId }; | ||
var getIdsFromUrn = function getIdsFromUrn(urn) { | ||
var regExp = /.*:spaces\/([^/]+)(?:\/environments\/([^/]+))?\/entries\/([^/]+)$/; | ||
if (!regExp.test(urn)) { | ||
return undefined; | ||
} | ||
// eslint-disable-next-line no-unused-vars | ||
var _urn$match = urn.match(regExp), | ||
_urn$match2 = _slicedToArray(_urn$match, 4), | ||
_ = _urn$match2[0], | ||
spaceId = _urn$match2[1], | ||
_urn$match2$ = _urn$match2[2], | ||
environmentId = _urn$match2$ === undefined ? 'master' : _urn$match2$, | ||
entryId = _urn$match2[3]; | ||
return { spaceId, environmentId, entryId }; | ||
}; | ||
/** | ||
* GetResolvedLink Function | ||
* getResolvedLink Function | ||
* | ||
@@ -70,23 +110,36 @@ * @param entityMap | ||
*/ | ||
const getResolvedLink = (entityMap, link) => { | ||
const { type, linkType } = link.sys; | ||
if (type === 'ResourceLink') { | ||
if (!linkType.startsWith('Contentful:')) { | ||
return link; | ||
} | ||
const { urn } = link.sys; | ||
const { spaceId, environmentId, entryId } = getIdsFromUrn(urn); | ||
const extractedLinkType = linkType.split(':')[1]; | ||
return (lookupInEntityMap(entityMap, { | ||
linkType: extractedLinkType, | ||
entryId, | ||
spaceId, | ||
environmentId, | ||
}) || UNRESOLVED_LINK); | ||
var getResolvedLink = function getResolvedLink(entityMap, link) { | ||
var _link$sys = link.sys, | ||
type = _link$sys.type, | ||
linkType = _link$sys.linkType; | ||
if (type === 'ResourceLink') { | ||
if (!linkType.startsWith('Contentful:')) { | ||
return link; | ||
} | ||
const { id: entryId } = link.sys; | ||
return lookupInEntityMap(entityMap, { linkType, entryId }) || UNRESOLVED_LINK; | ||
var urn = link.sys.urn; | ||
var _getIdsFromUrn = getIdsFromUrn(urn), | ||
spaceId = _getIdsFromUrn.spaceId, | ||
environmentId = _getIdsFromUrn.environmentId, | ||
_entryId = _getIdsFromUrn.entryId; | ||
var extractedLinkType = linkType.split(':')[1]; | ||
return lookupInEntityMap(entityMap, { | ||
linkType: extractedLinkType, | ||
entryId: _entryId, | ||
spaceId, | ||
environmentId | ||
}) || UNRESOLVED_LINK; | ||
} | ||
var entryId = link.sys.id; | ||
return lookupInEntityMap(entityMap, { linkType, entryId }) || UNRESOLVED_LINK; | ||
}; | ||
/** | ||
* CleanUpUnresolvedLinks Function | ||
* cleanUpLinks Function | ||
* - Removes unresolvable links from Arrays and Objects | ||
@@ -96,40 +149,18 @@ * | ||
*/ | ||
const cleanUpUnresolvedLinks = (input) => { | ||
if (Array.isArray(input)) { | ||
return input.filter((val) => val !== UNRESOLVED_LINK); | ||
var cleanUpLinks = function cleanUpLinks(input) { | ||
if (Array.isArray(input)) { | ||
return input.filter(function (val) { | ||
return val !== UNRESOLVED_LINK; | ||
}); | ||
} | ||
for (var key in input) { | ||
if (input[key] === UNRESOLVED_LINK) { | ||
delete input[key]; | ||
} | ||
for (const key in input) { | ||
if (input[key] === UNRESOLVED_LINK) { | ||
delete input[key]; | ||
} | ||
} | ||
return input; | ||
} | ||
return input; | ||
}; | ||
const normalizeLink = (entityMap, link, removeUnresolved) => { | ||
const resolvedLink = getResolvedLink(entityMap, link); | ||
if (resolvedLink === UNRESOLVED_LINK) { | ||
return removeUnresolved ? resolvedLink : link; | ||
} | ||
return resolvedLink; | ||
}; | ||
const maybeNormalizeLink = (entityMap, maybeLink, removeUnresolved) => { | ||
if (Array.isArray(maybeLink)) { | ||
return maybeLink.reduce((acc, link) => { | ||
const normalizedLink = maybeNormalizeLink(entityMap, link, removeUnresolved); | ||
if (removeUnresolved && normalizedLink === UNRESOLVED_LINK) { | ||
return acc; | ||
} | ||
acc.push(normalizedLink); | ||
return acc; | ||
}, []); | ||
} | ||
else if (typeof maybeLink === 'object') { | ||
if (isLink(maybeLink) || isResourceLink(maybeLink)) { | ||
return normalizeLink(entityMap, maybeLink, removeUnresolved); | ||
} | ||
} | ||
return maybeLink; | ||
}; | ||
/** | ||
* WalkMutate Function | ||
* walkMutate Function | ||
* @param input | ||
@@ -141,53 +172,46 @@ * @param predicate | ||
*/ | ||
const walkMutate = (input, predicate, mutator, removeUnresolved) => { | ||
if (predicate(input)) { | ||
return mutator(input); | ||
var walkMutate = function walkMutate(input, predicate, mutator, removeUnresolved) { | ||
if (predicate(input)) { | ||
return mutator(input); | ||
} | ||
if (input && typeof input === 'object') { | ||
for (var key in input) { | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (input.hasOwnProperty(key)) { | ||
input[key] = walkMutate(input[key], predicate, mutator, removeUnresolved); | ||
} | ||
} | ||
if (input && typeof input === 'object') { | ||
for (const key in input) { | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (input.hasOwnProperty(key)) { | ||
input[key] = walkMutate(input[key], predicate, mutator, removeUnresolved); | ||
} | ||
} | ||
if (removeUnresolved) { | ||
input = cleanUpUnresolvedLinks(input); | ||
} | ||
if (removeUnresolved) { | ||
input = cleanUpLinks(input); | ||
} | ||
return input; | ||
} | ||
return input; | ||
}; | ||
const makeEntryObject = (item, itemEntryPoints) => { | ||
if (!Array.isArray(itemEntryPoints)) { | ||
return item; | ||
} | ||
const entryPoints = Object.keys(item).filter((ownKey) => itemEntryPoints.indexOf(ownKey) !== -1); | ||
return entryPoints.reduce((entryObj, entryPoint) => { | ||
entryObj[entryPoint] = item[entryPoint]; | ||
return entryObj; | ||
}, {}); | ||
var normalizeLink = function normalizeLink(entityMap, link, removeUnresolved) { | ||
var resolvedLink = getResolvedLink(entityMap, link); | ||
if (resolvedLink === UNRESOLVED_LINK) { | ||
return removeUnresolved ? resolvedLink : link; | ||
} | ||
return resolvedLink; | ||
}; | ||
/** | ||
* Only normalize the top level properties of the entrypoint (e.g. item.fields), | ||
* as JSON fields can contain values that are objects that look like links, but are not. | ||
*/ | ||
const normalizeFromEntryPoint = (entityMap, entryPoint, removeUnresolved) => { | ||
if (!entryPoint) { | ||
return undefined; | ||
} | ||
if (Array.isArray(entryPoint)) { | ||
return maybeNormalizeLink(entityMap, entryPoint, removeUnresolved); | ||
} | ||
else if (typeof entryPoint === 'object') { | ||
return Object.entries(entryPoint).reduce((acc, [key, val]) => { | ||
const normalizedLink = maybeNormalizeLink(entityMap, val, removeUnresolved); | ||
if (removeUnresolved && normalizedLink === UNRESOLVED_LINK) { | ||
return acc; | ||
} | ||
acc[key] = normalizedLink; | ||
return acc; | ||
}, {}); | ||
} | ||
var makeEntryObject = function makeEntryObject(item, itemEntryPoints) { | ||
if (!Array.isArray(itemEntryPoints)) { | ||
return item; | ||
} | ||
var entryPoints = Object.keys(item).filter(function (ownKey) { | ||
return itemEntryPoints.indexOf(ownKey) !== -1; | ||
}); | ||
return entryPoints.reduce(function (entryObj, entryPoint) { | ||
entryObj[entryPoint] = item[entryPoint]; | ||
return entryObj; | ||
}, {}); | ||
}; | ||
/** | ||
* ResolveResponse Function | ||
* resolveResponse Function | ||
* Resolves contentful response to normalized form. | ||
@@ -200,28 +224,38 @@ * @param {Object} response Contentful response | ||
*/ | ||
const resolveResponse = (response, options) => { | ||
options ||= {}; | ||
if (!response.items) { | ||
return []; | ||
} | ||
const responseClone = (0, fast_copy_1.default)(response); | ||
const allIncludes = Object.keys(responseClone.includes || {}).reduce((all, type) => [...all, ...response.includes[type]], []); | ||
const allEntries = [...responseClone.items, ...allIncludes].filter((entity) => Boolean(entity.sys)); | ||
const entityMap = new Map(allEntries.reduce((acc, entity) => { | ||
const entries = makeEntityMapKeys(entity.sys).map((key) => [key, entity]); | ||
acc.push(...entries); | ||
return acc; | ||
}, [])); | ||
allEntries.forEach((item) => { | ||
if (options.itemEntryPoints && options.itemEntryPoints.length) { | ||
for (const entryPoint of options.itemEntryPoints) { | ||
item[entryPoint] = normalizeFromEntryPoint(entityMap, item[entryPoint], options.removeUnresolved); | ||
} | ||
} | ||
else { | ||
const entryObject = makeEntryObject(item, options.itemEntryPoints); | ||
Object.assign(item, walkMutate(entryObject, (x) => isLink(x) || isResourceLink(x), (link) => normalizeLink(entityMap, link, options.removeUnresolved), options.removeUnresolved)); | ||
} | ||
var resolveResponse = function resolveResponse(response, options) { | ||
options = options || {}; | ||
if (!response.items) { | ||
return []; | ||
} | ||
var responseClone = (0, _fastCopy2.default)(response); | ||
var allIncludes = Object.keys(responseClone.includes || {}).reduce(function (all, type) { | ||
return [].concat(_toConsumableArray(all), _toConsumableArray(response.includes[type])); | ||
}, []); | ||
var allEntries = [].concat(_toConsumableArray(responseClone.items), _toConsumableArray(allIncludes)).filter(function (entity) { | ||
return Boolean(entity.sys); | ||
}); | ||
var entityMap = new Map(allEntries.reduce(function (acc, entity) { | ||
var entries = makeEntityMapKeys(entity.sys).map(function (key) { | ||
return [key, entity]; | ||
}); | ||
return responseClone.items; | ||
acc.push.apply(acc, _toConsumableArray(entries)); | ||
return acc; | ||
}, [])); | ||
allEntries.forEach(function (item) { | ||
var entryObject = makeEntryObject(item, options.itemEntryPoints); | ||
Object.assign(item, walkMutate(entryObject, function (x) { | ||
return isLink(x) || isResourceLink(x); | ||
}, function (link) { | ||
return normalizeLink(entityMap, link, options.removeUnresolved); | ||
}, options.removeUnresolved)); | ||
}); | ||
return responseClone.items; | ||
}; | ||
exports.default = resolveResponse; | ||
module.exports = exports.default; |
@@ -0,15 +1,29 @@ | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
import copy from 'fast-copy'; | ||
const UNRESOLVED_LINK = {}; // Unique object to avoid polyfill bloat using Symbol() | ||
var UNRESOLVED_LINK = {}; // unique object to avoid polyfill bloat using Symbol() | ||
/** | ||
* IsLink Function | ||
* isLink Function | ||
* Checks if the object has sys.type "Link" | ||
* @param object | ||
*/ | ||
const isLink = (object) => object && object.sys && object.sys.type === 'Link'; | ||
var isLink = function isLink(object) { | ||
return object && object.sys && object.sys.type === 'Link'; | ||
}; | ||
/** | ||
* IsResourceLink Function | ||
* isResourceLink Function | ||
* Checks if the object has sys.type "ResourceLink" | ||
* @param object | ||
*/ | ||
const isResourceLink = (object) => object && object.sys && object.sys.type === 'ResourceLink'; | ||
var isResourceLink = function isResourceLink(object) { | ||
return object && object.sys && object.sys.type === 'ResourceLink'; | ||
}; | ||
/** | ||
@@ -26,8 +40,10 @@ * Creates a key with spaceId and a key without for entityMap | ||
*/ | ||
const makeEntityMapKeys = (sys) => { | ||
if (sys.space && sys.environment) { | ||
return [`${sys.type}!${sys.id}`, `${sys.space.sys.id}!${sys.environment.sys.id}!${sys.type}!${sys.id}`]; | ||
} | ||
return [`${sys.type}!${sys.id}`]; | ||
var makeEntityMapKeys = function makeEntityMapKeys(sys) { | ||
if (sys.space && sys.environment) { | ||
return [sys.type + '!' + sys.id, sys.space.sys.id + '!' + sys.environment.sys.id + '!' + sys.type + '!' + sys.id]; | ||
} | ||
return [sys.type + '!' + sys.id]; | ||
}; | ||
/** | ||
@@ -44,20 +60,38 @@ * Looks up in entityMap | ||
*/ | ||
const lookupInEntityMap = (entityMap, linkData) => { | ||
const { entryId, linkType, spaceId, environmentId } = linkData; | ||
if (spaceId && environmentId) { | ||
return entityMap.get(`${spaceId}!${environmentId}!${linkType}!${entryId}`); | ||
} | ||
return entityMap.get(`${linkType}!${entryId}`); | ||
var lookupInEntityMap = function lookupInEntityMap(entityMap, linkData) { | ||
var entryId = linkData.entryId, | ||
linkType = linkData.linkType, | ||
spaceId = linkData.spaceId, | ||
environmentId = linkData.environmentId; | ||
if (spaceId && environmentId) { | ||
return entityMap.get(spaceId + '!' + environmentId + '!' + linkType + '!' + entryId); | ||
} | ||
return entityMap.get(linkType + '!' + entryId); | ||
}; | ||
const getIdsFromUrn = (urn) => { | ||
const regExp = /.*:spaces\/([^/]+)(?:\/environments\/([^/]+))?\/entries\/([^/]+)$/; | ||
if (!regExp.test(urn)) { | ||
return undefined; | ||
} | ||
// eslint-disable-next-line no-unused-vars | ||
const [_, spaceId, environmentId = 'master', entryId] = urn.match(regExp); | ||
return { spaceId, environmentId, entryId }; | ||
var getIdsFromUrn = function getIdsFromUrn(urn) { | ||
var regExp = /.*:spaces\/([^/]+)(?:\/environments\/([^/]+))?\/entries\/([^/]+)$/; | ||
if (!regExp.test(urn)) { | ||
return undefined; | ||
} | ||
// eslint-disable-next-line no-unused-vars | ||
var _urn$match = urn.match(regExp), | ||
_urn$match2 = _slicedToArray(_urn$match, 4), | ||
_ = _urn$match2[0], | ||
spaceId = _urn$match2[1], | ||
_urn$match2$ = _urn$match2[2], | ||
environmentId = _urn$match2$ === undefined ? 'master' : _urn$match2$, | ||
entryId = _urn$match2[3]; | ||
return { spaceId: spaceId, environmentId: environmentId, entryId: entryId }; | ||
}; | ||
/** | ||
* GetResolvedLink Function | ||
* getResolvedLink Function | ||
* | ||
@@ -68,23 +102,36 @@ * @param entityMap | ||
*/ | ||
const getResolvedLink = (entityMap, link) => { | ||
const { type, linkType } = link.sys; | ||
if (type === 'ResourceLink') { | ||
if (!linkType.startsWith('Contentful:')) { | ||
return link; | ||
} | ||
const { urn } = link.sys; | ||
const { spaceId, environmentId, entryId } = getIdsFromUrn(urn); | ||
const extractedLinkType = linkType.split(':')[1]; | ||
return (lookupInEntityMap(entityMap, { | ||
linkType: extractedLinkType, | ||
entryId, | ||
spaceId, | ||
environmentId, | ||
}) || UNRESOLVED_LINK); | ||
var getResolvedLink = function getResolvedLink(entityMap, link) { | ||
var _link$sys = link.sys, | ||
type = _link$sys.type, | ||
linkType = _link$sys.linkType; | ||
if (type === 'ResourceLink') { | ||
if (!linkType.startsWith('Contentful:')) { | ||
return link; | ||
} | ||
const { id: entryId } = link.sys; | ||
return lookupInEntityMap(entityMap, { linkType, entryId }) || UNRESOLVED_LINK; | ||
var urn = link.sys.urn; | ||
var _getIdsFromUrn = getIdsFromUrn(urn), | ||
spaceId = _getIdsFromUrn.spaceId, | ||
environmentId = _getIdsFromUrn.environmentId, | ||
_entryId = _getIdsFromUrn.entryId; | ||
var extractedLinkType = linkType.split(':')[1]; | ||
return lookupInEntityMap(entityMap, { | ||
linkType: extractedLinkType, | ||
entryId: _entryId, | ||
spaceId: spaceId, | ||
environmentId: environmentId | ||
}) || UNRESOLVED_LINK; | ||
} | ||
var entryId = link.sys.id; | ||
return lookupInEntityMap(entityMap, { linkType: linkType, entryId: entryId }) || UNRESOLVED_LINK; | ||
}; | ||
/** | ||
* CleanUpUnresolvedLinks Function | ||
* cleanUpLinks Function | ||
* - Removes unresolvable links from Arrays and Objects | ||
@@ -94,40 +141,18 @@ * | ||
*/ | ||
const cleanUpUnresolvedLinks = (input) => { | ||
if (Array.isArray(input)) { | ||
return input.filter((val) => val !== UNRESOLVED_LINK); | ||
var cleanUpLinks = function cleanUpLinks(input) { | ||
if (Array.isArray(input)) { | ||
return input.filter(function (val) { | ||
return val !== UNRESOLVED_LINK; | ||
}); | ||
} | ||
for (var key in input) { | ||
if (input[key] === UNRESOLVED_LINK) { | ||
delete input[key]; | ||
} | ||
for (const key in input) { | ||
if (input[key] === UNRESOLVED_LINK) { | ||
delete input[key]; | ||
} | ||
} | ||
return input; | ||
} | ||
return input; | ||
}; | ||
const normalizeLink = (entityMap, link, removeUnresolved) => { | ||
const resolvedLink = getResolvedLink(entityMap, link); | ||
if (resolvedLink === UNRESOLVED_LINK) { | ||
return removeUnresolved ? resolvedLink : link; | ||
} | ||
return resolvedLink; | ||
}; | ||
const maybeNormalizeLink = (entityMap, maybeLink, removeUnresolved) => { | ||
if (Array.isArray(maybeLink)) { | ||
return maybeLink.reduce((acc, link) => { | ||
const normalizedLink = maybeNormalizeLink(entityMap, link, removeUnresolved); | ||
if (removeUnresolved && normalizedLink === UNRESOLVED_LINK) { | ||
return acc; | ||
} | ||
acc.push(normalizedLink); | ||
return acc; | ||
}, []); | ||
} | ||
else if (typeof maybeLink === 'object') { | ||
if (isLink(maybeLink) || isResourceLink(maybeLink)) { | ||
return normalizeLink(entityMap, maybeLink, removeUnresolved); | ||
} | ||
} | ||
return maybeLink; | ||
}; | ||
/** | ||
* WalkMutate Function | ||
* walkMutate Function | ||
* @param input | ||
@@ -139,53 +164,46 @@ * @param predicate | ||
*/ | ||
const walkMutate = (input, predicate, mutator, removeUnresolved) => { | ||
if (predicate(input)) { | ||
return mutator(input); | ||
var walkMutate = function walkMutate(input, predicate, mutator, removeUnresolved) { | ||
if (predicate(input)) { | ||
return mutator(input); | ||
} | ||
if (input && (typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object') { | ||
for (var key in input) { | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (input.hasOwnProperty(key)) { | ||
input[key] = walkMutate(input[key], predicate, mutator, removeUnresolved); | ||
} | ||
} | ||
if (input && typeof input === 'object') { | ||
for (const key in input) { | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (input.hasOwnProperty(key)) { | ||
input[key] = walkMutate(input[key], predicate, mutator, removeUnresolved); | ||
} | ||
} | ||
if (removeUnresolved) { | ||
input = cleanUpUnresolvedLinks(input); | ||
} | ||
if (removeUnresolved) { | ||
input = cleanUpLinks(input); | ||
} | ||
return input; | ||
} | ||
return input; | ||
}; | ||
const makeEntryObject = (item, itemEntryPoints) => { | ||
if (!Array.isArray(itemEntryPoints)) { | ||
return item; | ||
} | ||
const entryPoints = Object.keys(item).filter((ownKey) => itemEntryPoints.indexOf(ownKey) !== -1); | ||
return entryPoints.reduce((entryObj, entryPoint) => { | ||
entryObj[entryPoint] = item[entryPoint]; | ||
return entryObj; | ||
}, {}); | ||
var normalizeLink = function normalizeLink(entityMap, link, removeUnresolved) { | ||
var resolvedLink = getResolvedLink(entityMap, link); | ||
if (resolvedLink === UNRESOLVED_LINK) { | ||
return removeUnresolved ? resolvedLink : link; | ||
} | ||
return resolvedLink; | ||
}; | ||
/** | ||
* Only normalize the top level properties of the entrypoint (e.g. item.fields), | ||
* as JSON fields can contain values that are objects that look like links, but are not. | ||
*/ | ||
const normalizeFromEntryPoint = (entityMap, entryPoint, removeUnresolved) => { | ||
if (!entryPoint) { | ||
return undefined; | ||
} | ||
if (Array.isArray(entryPoint)) { | ||
return maybeNormalizeLink(entityMap, entryPoint, removeUnresolved); | ||
} | ||
else if (typeof entryPoint === 'object') { | ||
return Object.entries(entryPoint).reduce((acc, [key, val]) => { | ||
const normalizedLink = maybeNormalizeLink(entityMap, val, removeUnresolved); | ||
if (removeUnresolved && normalizedLink === UNRESOLVED_LINK) { | ||
return acc; | ||
} | ||
acc[key] = normalizedLink; | ||
return acc; | ||
}, {}); | ||
} | ||
var makeEntryObject = function makeEntryObject(item, itemEntryPoints) { | ||
if (!Array.isArray(itemEntryPoints)) { | ||
return item; | ||
} | ||
var entryPoints = Object.keys(item).filter(function (ownKey) { | ||
return itemEntryPoints.indexOf(ownKey) !== -1; | ||
}); | ||
return entryPoints.reduce(function (entryObj, entryPoint) { | ||
entryObj[entryPoint] = item[entryPoint]; | ||
return entryObj; | ||
}, {}); | ||
}; | ||
/** | ||
* ResolveResponse Function | ||
* resolveResponse Function | ||
* Resolves contentful response to normalized form. | ||
@@ -198,28 +216,37 @@ * @param {Object} response Contentful response | ||
*/ | ||
const resolveResponse = (response, options) => { | ||
options ||= {}; | ||
if (!response.items) { | ||
return []; | ||
} | ||
const responseClone = copy(response); | ||
const allIncludes = Object.keys(responseClone.includes || {}).reduce((all, type) => [...all, ...response.includes[type]], []); | ||
const allEntries = [...responseClone.items, ...allIncludes].filter((entity) => Boolean(entity.sys)); | ||
const entityMap = new Map(allEntries.reduce((acc, entity) => { | ||
const entries = makeEntityMapKeys(entity.sys).map((key) => [key, entity]); | ||
acc.push(...entries); | ||
return acc; | ||
}, [])); | ||
allEntries.forEach((item) => { | ||
if (options.itemEntryPoints && options.itemEntryPoints.length) { | ||
for (const entryPoint of options.itemEntryPoints) { | ||
item[entryPoint] = normalizeFromEntryPoint(entityMap, item[entryPoint], options.removeUnresolved); | ||
} | ||
} | ||
else { | ||
const entryObject = makeEntryObject(item, options.itemEntryPoints); | ||
Object.assign(item, walkMutate(entryObject, (x) => isLink(x) || isResourceLink(x), (link) => normalizeLink(entityMap, link, options.removeUnresolved), options.removeUnresolved)); | ||
} | ||
var resolveResponse = function resolveResponse(response, options) { | ||
options = options || {}; | ||
if (!response.items) { | ||
return []; | ||
} | ||
var responseClone = copy(response); | ||
var allIncludes = Object.keys(responseClone.includes || {}).reduce(function (all, type) { | ||
return [].concat(_toConsumableArray(all), _toConsumableArray(response.includes[type])); | ||
}, []); | ||
var allEntries = [].concat(_toConsumableArray(responseClone.items), _toConsumableArray(allIncludes)).filter(function (entity) { | ||
return Boolean(entity.sys); | ||
}); | ||
var entityMap = new Map(allEntries.reduce(function (acc, entity) { | ||
var entries = makeEntityMapKeys(entity.sys).map(function (key) { | ||
return [key, entity]; | ||
}); | ||
return responseClone.items; | ||
acc.push.apply(acc, _toConsumableArray(entries)); | ||
return acc; | ||
}, [])); | ||
allEntries.forEach(function (item) { | ||
var entryObject = makeEntryObject(item, options.itemEntryPoints); | ||
Object.assign(item, walkMutate(entryObject, function (x) { | ||
return isLink(x) || isResourceLink(x); | ||
}, function (link) { | ||
return normalizeLink(entityMap, link, options.removeUnresolved); | ||
}, options.removeUnresolved)); | ||
}); | ||
return responseClone.items; | ||
}; | ||
export default resolveResponse; | ||
export default resolveResponse; |
{ | ||
"name": "contentful-resolve-response", | ||
"version": "1.9.1", | ||
"version": "1.9.2", | ||
"description": "", | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/esm/index.js", | ||
"jsnext:main": "./dist/esm/index.js", | ||
"scripts": { | ||
"build": "tsc --project tsconfig.cjs.json && tsc --project tsconfig.esm.json", | ||
"build": "BABEL_ENV=cjs babel index.js -d dist/cjs/ && BABEL_ENV=esm babel index.js -d dist/esm/", | ||
"lint": "eslint index.js test", | ||
"test": "mocha --import=tsx 'test/**/*-test.js'", | ||
"test": "BABEL_ENV=cjs mocha --require babel-register 'test/**/*-test.js'", | ||
"test-watch": "npm run test -- --watch", | ||
@@ -15,2 +17,3 @@ "precommit": "npm run lint", | ||
"format": "prettier --config ./.prettierrc --write \"{*.js,**/*.js,*.ts,**/*.ts,*.json,**/*.json}\"", | ||
"commitlint-circle": "commitlint-circle", | ||
"semantic-release": "semantic-release" | ||
@@ -34,12 +37,14 @@ }, | ||
"@semantic-release/git": "^10.0.1", | ||
"babel-cli": "^6.26.0", | ||
"babel-plugin-add-module-exports": "^1.0.2", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-register": "^6.26.0", | ||
"chai": "^4.3.6", | ||
"dirty-chai": "^2.0.1", | ||
"eslint": "^9.15.0", | ||
"eslint-plugin-mocha": "^10.5.0", | ||
"eslint-plugin-mocha": "^10.0.3", | ||
"husky": "^9.0.6", | ||
"mocha": "^11.0.1", | ||
"prettier": "^3.0.0", | ||
"semantic-release": "^19.0.5", | ||
"tsx": "^4.19.2", | ||
"typescript": "^5.7.2" | ||
"semantic-release": "^19.0.5" | ||
}, | ||
@@ -46,0 +51,0 @@ "files": [ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20077
17
5
422