-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
216 lines (187 loc) · 5.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import copy from 'fast-copy'
const UNRESOLVED_LINK = {} // unique object to avoid polyfill bloat using Symbol()
/**
* isLink Function
* Checks if the object has sys.type "Link"
* @param object
*/
const isLink = (object) => object && object.sys && object.sys.type === 'Link'
/**
* isResourceLink Function
* Checks if the object has sys.type "ResourceLink"
* @param object
*/
const isResourceLink = (object) => object && object.sys && object.sys.type === 'ResourceLink'
/**
* Creates a key with spaceId and a key without for entityMap
*
* @param {*} sys
* @param {String} sys.type
* @param {String} sys.id
* @param {*} sys.space
* @param {*} sys.space.sys
* @param {String} sys.space.id
* @return {string[]}
*/
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}`]
}
/**
* 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}
*/
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}`)
}
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
*
* @param entityMap
* @param link
* @return {undefined}
*/
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
)
}
const { id: entryId } = link.sys
return lookupInEntityMap(entityMap, { linkType, entryId }) || UNRESOLVED_LINK
}
/**
* cleanUpLinks Function
* - Removes unresolvable links from Arrays and Objects
*
* @param {Object[]|Object} input
*/
const cleanUpLinks = (input) => {
if (Array.isArray(input)) {
return input.filter((val) => val !== UNRESOLVED_LINK)
}
for (const key in input) {
if (input[key] === UNRESOLVED_LINK) {
delete input[key]
}
}
return input
}
/**
* walkMutate Function
* @param input
* @param predicate
* @param mutator
* @param removeUnresolved
* @return {*}
*/
const walkMutate = (input, predicate, mutator, removeUnresolved) => {
if (predicate(input)) {
return mutator(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 = 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
}
const entryPoints = Object.keys(item).filter((ownKey) => itemEntryPoints.indexOf(ownKey) !== -1)
return entryPoints.reduce((entryObj, entryPoint) => {
entryObj[entryPoint] = item[entryPoint]
return entryObj
}, {})
}
/**
* resolveResponse Function
* Resolves contentful response to normalized form.
* @param {Object} response Contentful response
* @param {{removeUnresolved: Boolean, itemEntryPoints: Array<String>}|{}} options
* @param {Boolean} options.removeUnresolved - Remove unresolved links default:false
* @param {Array<String>} options.itemEntryPoints - Resolve links only in those item properties
* @return {Object}
*/
const resolveResponse = (response, options) => {
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) => {
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
}
export default resolveResponse