Skip to content

Commit e70d77b

Browse files
authored
Merge pull request #379 from contentful/revert-377-fix/nested-link-resolution
fix: Revert nested link resolution change
2 parents 418a4de + c11112d commit e70d77b

File tree

6 files changed

+3631
-1278
lines changed

6 files changed

+3631
-1278
lines changed

index.js

Lines changed: 31 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import copy from 'fast-copy'
22

3-
const UNRESOLVED_LINK = {} // Unique object to avoid polyfill bloat using Symbol()
3+
const UNRESOLVED_LINK = {} // unique object to avoid polyfill bloat using Symbol()
44

55
/**
6-
* IsLink Function
6+
* isLink Function
77
* Checks if the object has sys.type "Link"
88
* @param object
99
*/
1010
const isLink = (object) => object && object.sys && object.sys.type === 'Link'
1111

1212
/**
13-
* IsResourceLink Function
13+
* isResourceLink Function
1414
* Checks if the object has sys.type "ResourceLink"
1515
* @param object
1616
*/
@@ -69,7 +69,7 @@ const getIdsFromUrn = (urn) => {
6969
}
7070

7171
/**
72-
* GetResolvedLink Function
72+
* getResolvedLink Function
7373
*
7474
* @param entityMap
7575
* @param link
@@ -101,12 +101,12 @@ const getResolvedLink = (entityMap, link) => {
101101
}
102102

103103
/**
104-
* CleanUpUnresolvedLinks Function
104+
* cleanUpLinks Function
105105
* - Removes unresolvable links from Arrays and Objects
106106
*
107107
* @param {Object[]|Object} input
108108
*/
109-
const cleanUpUnresolvedLinks = (input) => {
109+
const cleanUpLinks = (input) => {
110110
if (Array.isArray(input)) {
111111
return input.filter((val) => val !== UNRESOLVED_LINK)
112112
}
@@ -118,34 +118,8 @@ const cleanUpUnresolvedLinks = (input) => {
118118
return input
119119
}
120120

121-
const normalizeLink = (entityMap, link, removeUnresolved) => {
122-
const resolvedLink = getResolvedLink(entityMap, link)
123-
if (resolvedLink === UNRESOLVED_LINK) {
124-
return removeUnresolved ? resolvedLink : link
125-
}
126-
return resolvedLink
127-
}
128-
129-
const maybeNormalizeLink = (entityMap, maybeLink, removeUnresolved) => {
130-
if (Array.isArray(maybeLink)) {
131-
return maybeLink.reduce((acc, link) => {
132-
const normalizedLink = maybeNormalizeLink(entityMap, link, removeUnresolved)
133-
if (removeUnresolved && normalizedLink === UNRESOLVED_LINK) {
134-
return acc
135-
}
136-
acc.push(normalizedLink)
137-
return acc
138-
}, [])
139-
} else if (typeof maybeLink === 'object') {
140-
if (isLink(maybeLink) || isResourceLink(maybeLink)) {
141-
return normalizeLink(entityMap, maybeLink, removeUnresolved)
142-
}
143-
}
144-
return maybeLink
145-
}
146-
147121
/**
148-
* WalkMutate Function
122+
* walkMutate Function
149123
* @param input
150124
* @param predicate
151125
* @param mutator
@@ -165,12 +139,20 @@ const walkMutate = (input, predicate, mutator, removeUnresolved) => {
165139
}
166140
}
167141
if (removeUnresolved) {
168-
input = cleanUpUnresolvedLinks(input)
142+
input = cleanUpLinks(input)
169143
}
170144
}
171145
return input
172146
}
173147

148+
const normalizeLink = (entityMap, link, removeUnresolved) => {
149+
const resolvedLink = getResolvedLink(entityMap, link)
150+
if (resolvedLink === UNRESOLVED_LINK) {
151+
return removeUnresolved ? resolvedLink : link
152+
}
153+
return resolvedLink
154+
}
155+
174156
const makeEntryObject = (item, itemEntryPoints) => {
175157
if (!Array.isArray(itemEntryPoints)) {
176158
return item
@@ -185,30 +167,7 @@ const makeEntryObject = (item, itemEntryPoints) => {
185167
}
186168

187169
/**
188-
* Only normalize the top level properties of the entrypoint (e.g. item.fields),
189-
* as JSON fields can contain values that are objects that look like links, but are not.
190-
*/
191-
const normalizeFromEntryPoint = (entityMap, entryPoint, removeUnresolved) => {
192-
if (!entryPoint) {
193-
return undefined
194-
}
195-
196-
if (Array.isArray(entryPoint)) {
197-
return maybeNormalizeLink(entityMap, entryPoint, removeUnresolved)
198-
} else if (typeof entryPoint === 'object') {
199-
return Object.entries(entryPoint).reduce((acc, [key, val]) => {
200-
const normalizedLink = maybeNormalizeLink(entityMap, val, removeUnresolved)
201-
if (removeUnresolved && normalizedLink === UNRESOLVED_LINK) {
202-
return acc
203-
}
204-
acc[key] = normalizedLink
205-
return acc
206-
}, {})
207-
}
208-
}
209-
210-
/**
211-
* ResolveResponse Function
170+
* resolveResponse Function
212171
* Resolves contentful response to normalized form.
213172
* @param {Object} response Contentful response
214173
* @param {{removeUnresolved: Boolean, itemEntryPoints: Array<String>}|{}} options
@@ -217,7 +176,7 @@ const normalizeFromEntryPoint = (entityMap, entryPoint, removeUnresolved) => {
217176
* @return {Object}
218177
*/
219178
const resolveResponse = (response, options) => {
220-
options ||= {}
179+
options = options || {}
221180
if (!response.items) {
222181
return []
223182
}
@@ -226,7 +185,9 @@ const resolveResponse = (response, options) => {
226185
(all, type) => [...all, ...response.includes[type]],
227186
[],
228187
)
188+
229189
const allEntries = [...responseClone.items, ...allIncludes].filter((entity) => Boolean(entity.sys))
190+
230191
const entityMap = new Map(
231192
allEntries.reduce((acc, entity) => {
232193
const entries = makeEntityMapKeys(entity.sys).map((key) => [key, entity])
@@ -236,23 +197,17 @@ const resolveResponse = (response, options) => {
236197
)
237198

238199
allEntries.forEach((item) => {
239-
if (options.itemEntryPoints && options.itemEntryPoints.length) {
240-
for (const entryPoint of options.itemEntryPoints) {
241-
item[entryPoint] = normalizeFromEntryPoint(entityMap, item[entryPoint], options.removeUnresolved)
242-
}
243-
} else {
244-
const entryObject = makeEntryObject(item, options.itemEntryPoints)
245-
246-
Object.assign(
247-
item,
248-
walkMutate(
249-
entryObject,
250-
(x) => isLink(x) || isResourceLink(x),
251-
(link) => normalizeLink(entityMap, link, options.removeUnresolved),
252-
options.removeUnresolved,
253-
),
254-
)
255-
}
200+
const entryObject = makeEntryObject(item, options.itemEntryPoints)
201+
202+
Object.assign(
203+
item,
204+
walkMutate(
205+
entryObject,
206+
(x) => isLink(x) || isResourceLink(x),
207+
(link) => normalizeLink(entityMap, link, options.removeUnresolved),
208+
options.removeUnresolved,
209+
),
210+
)
256211
})
257212

258213
return responseClone.items

0 commit comments

Comments
 (0)