Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

contentful-resolve-response

Package Overview
Dependencies
Maintainers
4
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

contentful-resolve-response - npm Package Compare versions

Comparing version 1.9.0 to 1.9.1

dist/cjs/index.d.ts

332

dist/cjs/index.js

@@ -1,37 +0,17 @@

'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()
"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()
/**
* isLink Function
* IsLink Function
* Checks if the object has sys.type "Link"
* @param object
*/
var isLink = function isLink(object) {
return object && object.sys && object.sys.type === 'Link';
};
const isLink = (object) => object && object.sys && object.sys.type === 'Link';
/**
* isResourceLink Function
* IsResourceLink Function
* Checks if the object has sys.type "ResourceLink"
* @param object
*/
var isResourceLink = function isResourceLink(object) {
return object && object.sys && object.sys.type === 'ResourceLink';
};
const isResourceLink = (object) => object && object.sys && object.sys.type === 'ResourceLink';
/**

@@ -48,10 +28,8 @@ * Creates a key with spaceId and a key without for entityMap

*/
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}`];
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}`];
};
/**

@@ -68,36 +46,20 @@ * Looks up in entityMap

*/
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 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 getIdsFromUrn = function getIdsFromUrn(urn) {
var regExp = /.*:spaces\/([^/]+)(?:\/environments\/([^/]+))?\/entries\/([^/]+)$/;
if (!regExp.test(urn)) {
return undefined;
}
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 };
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 };
};
/**
* getResolvedLink Function
* GetResolvedLink Function
*

@@ -108,36 +70,23 @@ * @param entityMap

*/
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 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 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;
const { id: entryId } = link.sys;
return lookupInEntityMap(entityMap, { linkType, entryId }) || UNRESOLVED_LINK;
};
/**
* cleanUpLinks Function
* CleanUpUnresolvedLinks Function
* - Removes unresolvable links from Arrays and Objects

@@ -147,18 +96,40 @@ *

*/
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];
const cleanUpUnresolvedLinks = (input) => {
if (Array.isArray(input)) {
return input.filter((val) => val !== UNRESOLVED_LINK);
}
}
return input;
for (const key in input) {
if (input[key] === UNRESOLVED_LINK) {
delete input[key];
}
}
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

@@ -170,46 +141,53 @@ * @param predicate

*/
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);
}
const walkMutate = (input, predicate, mutator, removeUnresolved) => {
if (predicate(input)) {
return mutator(input);
}
if (removeUnresolved) {
input = cleanUpLinks(input);
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);
}
}
}
return input;
return input;
};
var normalizeLink = function normalizeLink(entityMap, link, removeUnresolved) {
var resolvedLink = getResolvedLink(entityMap, link);
if (resolvedLink === UNRESOLVED_LINK) {
return removeUnresolved ? resolvedLink : link;
}
return resolvedLink;
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 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;
}, {});
/**
* 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;
}, {});
}
};
/**
* resolveResponse Function
* ResolveResponse Function
* Resolves contentful response to normalized form.

@@ -222,38 +200,28 @@ * @param {Object} response Contentful response

*/
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];
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));
}
});
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;
return responseClone.items;
};
exports.default = resolveResponse;
module.exports = exports.default;

@@ -1,29 +0,15 @@

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';
var UNRESOLVED_LINK = {}; // unique object to avoid polyfill bloat using Symbol()
const UNRESOLVED_LINK = {}; // Unique object to avoid polyfill bloat using Symbol()
/**
* isLink Function
* IsLink Function
* Checks if the object has sys.type "Link"
* @param object
*/
var isLink = function isLink(object) {
return object && object.sys && object.sys.type === 'Link';
};
const isLink = (object) => object && object.sys && object.sys.type === 'Link';
/**
* isResourceLink Function
* IsResourceLink Function
* Checks if the object has sys.type "ResourceLink"
* @param object
*/
var isResourceLink = function isResourceLink(object) {
return object && object.sys && object.sys.type === 'ResourceLink';
};
const isResourceLink = (object) => object && object.sys && object.sys.type === 'ResourceLink';
/**

@@ -40,10 +26,8 @@ * Creates a key with spaceId and a key without for entityMap

*/
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];
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}`];
};
/**

@@ -60,36 +44,20 @@ * Looks up in entityMap

*/
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 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 getIdsFromUrn = function getIdsFromUrn(urn) {
var regExp = /.*:spaces\/([^/]+)(?:\/environments\/([^/]+))?\/entries\/([^/]+)$/;
if (!regExp.test(urn)) {
return undefined;
}
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 };
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 };
};
/**
* getResolvedLink Function
* GetResolvedLink Function
*

@@ -100,36 +68,23 @@ * @param entityMap

*/
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 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 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;
const { id: entryId } = link.sys;
return lookupInEntityMap(entityMap, { linkType, entryId }) || UNRESOLVED_LINK;
};
/**
* cleanUpLinks Function
* CleanUpUnresolvedLinks Function
* - Removes unresolvable links from Arrays and Objects

@@ -139,18 +94,40 @@ *

*/
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];
const cleanUpUnresolvedLinks = (input) => {
if (Array.isArray(input)) {
return input.filter((val) => val !== UNRESOLVED_LINK);
}
}
return input;
for (const key in input) {
if (input[key] === UNRESOLVED_LINK) {
delete input[key];
}
}
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

@@ -162,46 +139,53 @@ * @param predicate

*/
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);
}
const walkMutate = (input, predicate, mutator, removeUnresolved) => {
if (predicate(input)) {
return mutator(input);
}
if (removeUnresolved) {
input = cleanUpLinks(input);
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);
}
}
}
return input;
return input;
};
var normalizeLink = function normalizeLink(entityMap, link, removeUnresolved) {
var resolvedLink = getResolvedLink(entityMap, link);
if (resolvedLink === UNRESOLVED_LINK) {
return removeUnresolved ? resolvedLink : link;
}
return resolvedLink;
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 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;
}, {});
/**
* 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;
}, {});
}
};
/**
* resolveResponse Function
* ResolveResponse Function
* Resolves contentful response to normalized form.

@@ -214,37 +198,28 @@ * @param {Object} response Contentful response

*/
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];
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));
}
});
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;
return responseClone.items;
};
export default resolveResponse;
export default resolveResponse;
{
"name": "contentful-resolve-response",
"version": "1.9.0",
"description": "",
"version": "1.9.1",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"jsnext:main": "./dist/esm/index.js",
"scripts": {
"build": "BABEL_ENV=cjs babel index.js -d dist/cjs/ && BABEL_ENV=esm babel index.js -d dist/esm/",
"build": "tsc --project tsconfig.cjs.json && tsc --project tsconfig.esm.json",
"lint": "eslint index.js test",
"test": "BABEL_ENV=cjs mocha --require babel-register 'test/**/*-test.js'",
"test": "mocha --import=tsx 'test/**/*-test.js'",
"test-watch": "npm run test -- --watch",

@@ -17,3 +15,2 @@ "precommit": "npm run lint",

"format": "prettier --config ./.prettierrc --write \"{*.js,**/*.js,*.ts,**/*.ts,*.json,**/*.json}\"",
"commitlint-circle": "commitlint-circle",
"semantic-release": "semantic-release"

@@ -32,26 +29,17 @@ },

"devDependencies": {
"@commitlint/cli": "^18.0.0",
"@commitlint/cli": "^19.6.0",
"@commitlint/config-conventional": "^18.0.0",
"@contentful/eslint-config-backend": "^10.0.0",
"@eslint/js": "^9.16.0",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"@timbeyer/commitlint-circle": "^4.1.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": "^8.9.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-mocha": "^10.0.3",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-standard": "^5.0.0",
"eslint": "^9.15.0",
"eslint-plugin-mocha": "^10.5.0",
"husky": "^9.0.6",
"mocha": "^10.0.0",
"mocha": "^11.0.1",
"prettier": "^3.0.0",
"semantic-release": "^19.0.5"
"semantic-release": "^19.0.5",
"tsx": "^4.19.2",
"typescript": "^5.7.2"
},

@@ -58,0 +46,0 @@ "files": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc