Skip to content

Commit

Permalink
Merge pull request #379 from contentful/revert-377-fix/nested-link-re…
Browse files Browse the repository at this point in the history
…solution

fix: Revert nested link resolution change
  • Loading branch information
t-col authored Dec 9, 2024
2 parents 418a4de + c11112d commit e70d77b
Show file tree
Hide file tree
Showing 6 changed files with 3,631 additions and 1,278 deletions.
107 changes: 31 additions & 76 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import copy from 'fast-copy'

const 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
*/
const isLink = (object) => object && object.sys && object.sys.type === 'Link'

/**
* IsResourceLink Function
* isResourceLink Function
* Checks if the object has sys.type "ResourceLink"
* @param object
*/
Expand Down Expand Up @@ -69,7 +69,7 @@ const getIdsFromUrn = (urn) => {
}

/**
* GetResolvedLink Function
* getResolvedLink Function
*
* @param entityMap
* @param link
Expand Down Expand Up @@ -101,12 +101,12 @@ const getResolvedLink = (entityMap, link) => {
}

/**
* CleanUpUnresolvedLinks Function
* cleanUpLinks Function
* - Removes unresolvable links from Arrays and Objects
*
* @param {Object[]|Object} input
*/
const cleanUpUnresolvedLinks = (input) => {
const cleanUpLinks = (input) => {
if (Array.isArray(input)) {
return input.filter((val) => val !== UNRESOLVED_LINK)
}
Expand All @@ -118,34 +118,8 @@ const cleanUpUnresolvedLinks = (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
* @param predicate
* @param mutator
Expand All @@ -165,12 +139,20 @@ const walkMutate = (input, predicate, mutator, removeUnresolved) => {
}
}
if (removeUnresolved) {
input = cleanUpUnresolvedLinks(input)
input = cleanUpLinks(input)
}
}
return input
}

const normalizeLink = (entityMap, link, removeUnresolved) => {
const resolvedLink = getResolvedLink(entityMap, link)
if (resolvedLink === UNRESOLVED_LINK) {
return removeUnresolved ? resolvedLink : link
}
return resolvedLink
}

const makeEntryObject = (item, itemEntryPoints) => {
if (!Array.isArray(itemEntryPoints)) {
return item
Expand All @@ -185,30 +167,7 @@ const makeEntryObject = (item, itemEntryPoints) => {
}

/**
* 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.
* @param {Object} response Contentful response
* @param {{removeUnresolved: Boolean, itemEntryPoints: Array<String>}|{}} options
Expand All @@ -217,7 +176,7 @@ const normalizeFromEntryPoint = (entityMap, entryPoint, removeUnresolved) => {
* @return {Object}
*/
const resolveResponse = (response, options) => {
options ||= {}
options = options || {}
if (!response.items) {
return []
}
Expand All @@ -226,7 +185,9 @@ const resolveResponse = (response, options) => {
(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])
Expand All @@ -236,23 +197,17 @@ const resolveResponse = (response, options) => {
)

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,
),
)
}
const entryObject = makeEntryObject(item, options.itemEntryPoints)

Object.assign(
item,
walkMutate(
entryObject,
(x) => isLink(x) || isResourceLink(x),
(link) => normalizeLink(entityMap, link, options.removeUnresolved),
options.removeUnresolved,
),
)
})

return responseClone.items
Expand Down
Loading

0 comments on commit e70d77b

Please sign in to comment.