contentful-resolve-response
Advanced tools
Comparing version 1.3.12 to 1.4.0
@@ -27,3 +27,12 @@ 'use strict'; | ||
/** | ||
* Creates a string key for lookup in entityMap | ||
* 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'; | ||
}; | ||
/** | ||
* Creates a key with spaceId and a key without for entityMap | ||
* | ||
@@ -33,23 +42,62 @@ * @param {*} sys | ||
* @param {String} sys.id | ||
* @param {*} sys.space | ||
* @param {*} sys.space.sys | ||
* @param {String} sys.space.id | ||
* @return {string[]} | ||
*/ | ||
var makeEntityMapKeys = function makeEntityMapKeys(sys) { | ||
return sys.space ? [`${sys.type}!${sys.id}`, `${sys.space.sys.id}!${sys.type}!${sys.id}`] : [`${sys.type}!${sys.id}`]; | ||
}; | ||
/** | ||
* Looks up in entityMap | ||
* | ||
* @param entityMap | ||
* @param {*} linkData | ||
* @param {String} linkData.type | ||
* @param {String} linkData.linkType | ||
* @param {String} linkData.id | ||
* @param {String} linkData.urn | ||
* @return {String} | ||
*/ | ||
var makeLookupKey = function makeLookupKey(sys) { | ||
return `${sys.type}!${sys.id}`; | ||
var lookupInEntityMap = function lookupInEntityMap(entityMap, linkData) { | ||
var entryId = linkData.entryId, | ||
linkType = linkData.linkType, | ||
spaceId = linkData.spaceId; | ||
if (spaceId) { | ||
return entityMap.get(`${spaceId}!${linkType}!${entryId}`); | ||
} | ||
return entityMap.get(`${linkType}!${entryId}`); | ||
}; | ||
/** | ||
* getLink Function | ||
* getResolvedLink Function | ||
* | ||
* @param response | ||
* @param entityMap | ||
* @param link | ||
* @return {undefined} | ||
*/ | ||
var getLink = function getLink(entityMap, link) { | ||
var getResolvedLink = function getResolvedLink(entityMap, link) { | ||
var _link$sys = link.sys, | ||
type = _link$sys.linkType, | ||
id = _link$sys.id; | ||
type = _link$sys.type, | ||
linkType = _link$sys.linkType; | ||
var lookupKey = makeLookupKey({ type, id }); | ||
if (type === 'ResourceLink') { | ||
var urn = link.sys.urn; | ||
return entityMap.get(lookupKey) || UNRESOLVED_LINK; | ||
var regExp = /.*:spaces\/(?<spaceId>[A-Za-z0-9]*)\/entries\/(?<entryId>[A-Za-z0-9]*)/; | ||
if (!regExp.test(urn)) { | ||
return UNRESOLVED_LINK; | ||
} | ||
var _urn$match$groups = urn.match(regExp).groups, | ||
spaceId = _urn$match$groups.spaceId, | ||
_entryId = _urn$match$groups.entryId; | ||
var extractedLinkType = linkType.split(':')[1]; | ||
return lookupInEntityMap(entityMap, { linkType: extractedLinkType, entryId: _entryId, spaceId }) || UNRESOLVED_LINK; | ||
} | ||
var entryId = link.sys.id; | ||
return lookupInEntityMap(entityMap, { linkType, entryId }) || UNRESOLVED_LINK; | ||
}; | ||
@@ -82,2 +130,3 @@ | ||
* @param mutator | ||
* @param removeUnresolved | ||
* @return {*} | ||
@@ -105,3 +154,3 @@ */ | ||
var normalizeLink = function normalizeLink(entityMap, link, removeUnresolved) { | ||
var resolvedLink = getLink(entityMap, link); | ||
var resolvedLink = getResolvedLink(entityMap, link); | ||
if (resolvedLink === UNRESOLVED_LINK) { | ||
@@ -149,5 +198,9 @@ return removeUnresolved ? resolvedLink : link; | ||
var entityMap = new Map(allEntries.map(function (entity) { | ||
return [makeLookupKey(entity.sys), entity]; | ||
})); | ||
var entityMap = new Map(allEntries.reduce(function (acc, entity) { | ||
var entries = makeEntityMapKeys(entity.sys).map(function (key) { | ||
return [key, entity]; | ||
}); | ||
acc.push.apply(acc, _toConsumableArray(entries)); | ||
return acc; | ||
}, [])); | ||
@@ -157,3 +210,5 @@ allEntries.forEach(function (item) { | ||
Object.assign(item, walkMutate(entryObject, isLink, function (link) { | ||
Object.assign(item, walkMutate(entryObject, function (x) { | ||
return isLink(x) || isResourceLink(x); | ||
}, function (link) { | ||
return normalizeLink(entityMap, link, options.removeUnresolved); | ||
@@ -160,0 +215,0 @@ }, options.removeUnresolved)); |
@@ -19,3 +19,12 @@ 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; }; | ||
/** | ||
* Creates a string key for lookup in entityMap | ||
* 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'; | ||
}; | ||
/** | ||
* Creates a key with spaceId and a key without for entityMap | ||
* | ||
@@ -25,23 +34,62 @@ * @param {*} sys | ||
* @param {String} sys.id | ||
* @param {*} sys.space | ||
* @param {*} sys.space.sys | ||
* @param {String} sys.space.id | ||
* @return {string[]} | ||
*/ | ||
var makeEntityMapKeys = function makeEntityMapKeys(sys) { | ||
return sys.space ? [sys.type + '!' + sys.id, sys.space.sys.id + '!' + sys.type + '!' + sys.id] : [sys.type + '!' + sys.id]; | ||
}; | ||
/** | ||
* Looks up in entityMap | ||
* | ||
* @param entityMap | ||
* @param {*} linkData | ||
* @param {String} linkData.type | ||
* @param {String} linkData.linkType | ||
* @param {String} linkData.id | ||
* @param {String} linkData.urn | ||
* @return {String} | ||
*/ | ||
var makeLookupKey = function makeLookupKey(sys) { | ||
return sys.type + '!' + sys.id; | ||
var lookupInEntityMap = function lookupInEntityMap(entityMap, linkData) { | ||
var entryId = linkData.entryId, | ||
linkType = linkData.linkType, | ||
spaceId = linkData.spaceId; | ||
if (spaceId) { | ||
return entityMap.get(spaceId + '!' + linkType + '!' + entryId); | ||
} | ||
return entityMap.get(linkType + '!' + entryId); | ||
}; | ||
/** | ||
* getLink Function | ||
* getResolvedLink Function | ||
* | ||
* @param response | ||
* @param entityMap | ||
* @param link | ||
* @return {undefined} | ||
*/ | ||
var getLink = function getLink(entityMap, link) { | ||
var getResolvedLink = function getResolvedLink(entityMap, link) { | ||
var _link$sys = link.sys, | ||
type = _link$sys.linkType, | ||
id = _link$sys.id; | ||
type = _link$sys.type, | ||
linkType = _link$sys.linkType; | ||
var lookupKey = makeLookupKey({ type: type, id: id }); | ||
if (type === 'ResourceLink') { | ||
var urn = link.sys.urn; | ||
return entityMap.get(lookupKey) || UNRESOLVED_LINK; | ||
var regExp = /.*:spaces\/(?<spaceId>[A-Za-z0-9]*)\/entries\/(?<entryId>[A-Za-z0-9]*)/; | ||
if (!regExp.test(urn)) { | ||
return UNRESOLVED_LINK; | ||
} | ||
var _urn$match$groups = urn.match(regExp).groups, | ||
spaceId = _urn$match$groups.spaceId, | ||
_entryId = _urn$match$groups.entryId; | ||
var extractedLinkType = linkType.split(':')[1]; | ||
return lookupInEntityMap(entityMap, { linkType: extractedLinkType, entryId: _entryId, spaceId: spaceId }) || UNRESOLVED_LINK; | ||
} | ||
var entryId = link.sys.id; | ||
return lookupInEntityMap(entityMap, { linkType: linkType, entryId: entryId }) || UNRESOLVED_LINK; | ||
}; | ||
@@ -74,2 +122,3 @@ | ||
* @param mutator | ||
* @param removeUnresolved | ||
* @return {*} | ||
@@ -97,3 +146,3 @@ */ | ||
var normalizeLink = function normalizeLink(entityMap, link, removeUnresolved) { | ||
var resolvedLink = getLink(entityMap, link); | ||
var resolvedLink = getResolvedLink(entityMap, link); | ||
if (resolvedLink === UNRESOLVED_LINK) { | ||
@@ -141,5 +190,9 @@ return removeUnresolved ? resolvedLink : link; | ||
var entityMap = new Map(allEntries.map(function (entity) { | ||
return [makeLookupKey(entity.sys), entity]; | ||
})); | ||
var entityMap = new Map(allEntries.reduce(function (acc, entity) { | ||
var entries = makeEntityMapKeys(entity.sys).map(function (key) { | ||
return [key, entity]; | ||
}); | ||
acc.push.apply(acc, _toConsumableArray(entries)); | ||
return acc; | ||
}, [])); | ||
@@ -149,3 +202,5 @@ allEntries.forEach(function (item) { | ||
Object.assign(item, walkMutate(entryObject, isLink, function (link) { | ||
Object.assign(item, walkMutate(entryObject, function (x) { | ||
return isLink(x) || isResourceLink(x); | ||
}, function (link) { | ||
return normalizeLink(entityMap, link, options.removeUnresolved); | ||
@@ -152,0 +207,0 @@ }, options.removeUnresolved)); |
{ | ||
"name": "contentful-resolve-response", | ||
"version": "1.3.12", | ||
"version": "1.4.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/index.js", |
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
17405
364