contentful-resolve-response 1.9.9 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +54 -81
- package/package.json +6 -2
package/dist/cjs/index.js
CHANGED
|
@@ -6,36 +6,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
var _fastCopy = _interopRequireDefault(require("fast-copy"));
|
|
8
8
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
-
|
|
10
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
11
|
-
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
12
|
-
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
13
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
14
|
-
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
15
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
16
|
-
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
17
|
-
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
18
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
19
|
-
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
20
|
-
var UNRESOLVED_LINK = {}; // unique object to avoid polyfill bloat using Symbol()
|
|
9
|
+
const UNRESOLVED_LINK = {}; // unique object to avoid polyfill bloat using Symbol()
|
|
21
10
|
|
|
22
11
|
/**
|
|
23
12
|
* isLink Function
|
|
24
13
|
* Checks if the object has sys.type "Link"
|
|
25
14
|
* @param object
|
|
26
15
|
*/
|
|
27
|
-
|
|
28
|
-
return object && object.sys && object.sys.type === 'Link';
|
|
29
|
-
};
|
|
16
|
+
const isLink = object => object && object.sys && object.sys.type === 'Link';
|
|
30
17
|
|
|
31
18
|
/**
|
|
32
19
|
* isResourceLink Function
|
|
33
20
|
* Checks if the object has sys.type "ResourceLink"
|
|
34
21
|
* @param object
|
|
35
22
|
*/
|
|
36
|
-
|
|
37
|
-
return object && object.sys && object.sys.type === 'ResourceLink';
|
|
38
|
-
};
|
|
23
|
+
const isResourceLink = object => object && object.sys && object.sys.type === 'ResourceLink';
|
|
39
24
|
|
|
40
25
|
/**
|
|
41
26
|
* Creates a key with spaceId and a key without for entityMap
|
|
@@ -48,7 +33,7 @@ var isResourceLink = function isResourceLink(object) {
|
|
|
48
33
|
* @param {String} sys.space.id
|
|
49
34
|
* @return {string[]}
|
|
50
35
|
*/
|
|
51
|
-
|
|
36
|
+
const makeEntityMapKeys = sys => {
|
|
52
37
|
if (sys.space && sys.environment) {
|
|
53
38
|
return [`${sys.type}!${sys.id}`, `${sys.space.sys.id}!${sys.environment.sys.id}!${sys.type}!${sys.id}`];
|
|
54
39
|
}
|
|
@@ -66,30 +51,26 @@ var makeEntityMapKeys = function makeEntityMapKeys(sys) {
|
|
|
66
51
|
* @param {String} linkData.urn
|
|
67
52
|
* @return {String}
|
|
68
53
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
54
|
+
const lookupInEntityMap = (entityMap, linkData) => {
|
|
55
|
+
const {
|
|
56
|
+
entryId,
|
|
57
|
+
linkType,
|
|
58
|
+
spaceId,
|
|
59
|
+
environmentId
|
|
60
|
+
} = linkData;
|
|
74
61
|
if (spaceId && environmentId) {
|
|
75
62
|
return entityMap.get(`${spaceId}!${environmentId}!${linkType}!${entryId}`);
|
|
76
63
|
}
|
|
77
64
|
return entityMap.get(`${linkType}!${entryId}`);
|
|
78
65
|
};
|
|
79
|
-
|
|
80
|
-
|
|
66
|
+
const getIdsFromUrn = urn => {
|
|
67
|
+
const regExp = /.*:spaces\/([^/]+)(?:\/environments\/([^/]+))?\/entries\/([^/]+)$/;
|
|
81
68
|
if (!regExp.test(urn)) {
|
|
82
69
|
return undefined;
|
|
83
70
|
}
|
|
84
71
|
|
|
85
72
|
// eslint-disable-next-line no-unused-vars
|
|
86
|
-
|
|
87
|
-
_urn$match2 = _slicedToArray(_urn$match, 4),
|
|
88
|
-
_ = _urn$match2[0],
|
|
89
|
-
spaceId = _urn$match2[1],
|
|
90
|
-
_urn$match2$ = _urn$match2[2],
|
|
91
|
-
environmentId = _urn$match2$ === void 0 ? 'master' : _urn$match2$,
|
|
92
|
-
entryId = _urn$match2[3];
|
|
73
|
+
const [_, spaceId, environmentId = 'master', entryId] = urn.match(regExp);
|
|
93
74
|
return {
|
|
94
75
|
spaceId,
|
|
95
76
|
environmentId,
|
|
@@ -104,28 +85,34 @@ var getIdsFromUrn = function getIdsFromUrn(urn) {
|
|
|
104
85
|
* @param link
|
|
105
86
|
* @return {undefined}
|
|
106
87
|
*/
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
type
|
|
110
|
-
linkType
|
|
88
|
+
const getResolvedLink = (entityMap, link) => {
|
|
89
|
+
const {
|
|
90
|
+
type,
|
|
91
|
+
linkType
|
|
92
|
+
} = link.sys;
|
|
111
93
|
if (type === 'ResourceLink') {
|
|
112
94
|
if (!linkType.startsWith('Contentful:')) {
|
|
113
95
|
return link;
|
|
114
96
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
97
|
+
const {
|
|
98
|
+
urn
|
|
99
|
+
} = link.sys;
|
|
100
|
+
const {
|
|
101
|
+
spaceId,
|
|
102
|
+
environmentId,
|
|
103
|
+
entryId
|
|
104
|
+
} = getIdsFromUrn(urn);
|
|
105
|
+
const extractedLinkType = linkType.split(':')[1];
|
|
121
106
|
return lookupInEntityMap(entityMap, {
|
|
122
107
|
linkType: extractedLinkType,
|
|
123
|
-
entryId
|
|
108
|
+
entryId,
|
|
124
109
|
spaceId,
|
|
125
110
|
environmentId
|
|
126
111
|
}) || UNRESOLVED_LINK;
|
|
127
112
|
}
|
|
128
|
-
|
|
113
|
+
const {
|
|
114
|
+
id: entryId
|
|
115
|
+
} = link.sys;
|
|
129
116
|
return lookupInEntityMap(entityMap, {
|
|
130
117
|
linkType,
|
|
131
118
|
entryId
|
|
@@ -138,13 +125,11 @@ var getResolvedLink = function getResolvedLink(entityMap, link) {
|
|
|
138
125
|
*
|
|
139
126
|
* @param {Object[]|Object} input
|
|
140
127
|
*/
|
|
141
|
-
|
|
128
|
+
const cleanUpLinks = input => {
|
|
142
129
|
if (Array.isArray(input)) {
|
|
143
|
-
return input.filter(
|
|
144
|
-
return val !== UNRESOLVED_LINK;
|
|
145
|
-
});
|
|
130
|
+
return input.filter(val => val !== UNRESOLVED_LINK);
|
|
146
131
|
}
|
|
147
|
-
for (
|
|
132
|
+
for (const key in input) {
|
|
148
133
|
if (input[key] === UNRESOLVED_LINK) {
|
|
149
134
|
delete input[key];
|
|
150
135
|
}
|
|
@@ -160,15 +145,15 @@ var cleanUpLinks = function cleanUpLinks(input) {
|
|
|
160
145
|
* @param removeUnresolved
|
|
161
146
|
* @return {*}
|
|
162
147
|
*/
|
|
163
|
-
|
|
148
|
+
const walkMutate = (input, predicate, mutator, removeUnresolved) => {
|
|
164
149
|
if (predicate(input)) {
|
|
165
150
|
return mutator(input);
|
|
166
151
|
}
|
|
167
|
-
if (input &&
|
|
168
|
-
for (
|
|
152
|
+
if (input && typeof input === 'object') {
|
|
153
|
+
for (const key in input) {
|
|
169
154
|
// eslint-disable-next-line no-prototype-builtins
|
|
170
155
|
if (input.hasOwnProperty(key)) {
|
|
171
|
-
input[key] =
|
|
156
|
+
input[key] = walkMutate(input[key], predicate, mutator, removeUnresolved);
|
|
172
157
|
}
|
|
173
158
|
}
|
|
174
159
|
if (removeUnresolved) {
|
|
@@ -177,21 +162,19 @@ var _walkMutate = function walkMutate(input, predicate, mutator, removeUnresolve
|
|
|
177
162
|
}
|
|
178
163
|
return input;
|
|
179
164
|
};
|
|
180
|
-
|
|
181
|
-
|
|
165
|
+
const normalizeLink = (entityMap, link, removeUnresolved) => {
|
|
166
|
+
const resolvedLink = getResolvedLink(entityMap, link);
|
|
182
167
|
if (resolvedLink === UNRESOLVED_LINK) {
|
|
183
168
|
return removeUnresolved ? resolvedLink : link;
|
|
184
169
|
}
|
|
185
170
|
return resolvedLink;
|
|
186
171
|
};
|
|
187
|
-
|
|
172
|
+
const makeEntryObject = (item, itemEntryPoints) => {
|
|
188
173
|
if (!Array.isArray(itemEntryPoints)) {
|
|
189
174
|
return item;
|
|
190
175
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
});
|
|
194
|
-
return entryPoints.reduce(function (entryObj, entryPoint) {
|
|
176
|
+
const entryPoints = Object.keys(item).filter(ownKey => itemEntryPoints.indexOf(ownKey) !== -1);
|
|
177
|
+
return entryPoints.reduce((entryObj, entryPoint) => {
|
|
195
178
|
entryObj[entryPoint] = item[entryPoint];
|
|
196
179
|
return entryObj;
|
|
197
180
|
}, {});
|
|
@@ -206,32 +189,22 @@ var makeEntryObject = function makeEntryObject(item, itemEntryPoints) {
|
|
|
206
189
|
* @param {Array<String>} options.itemEntryPoints - Resolve links only in those item properties
|
|
207
190
|
* @return {Object}
|
|
208
191
|
*/
|
|
209
|
-
|
|
192
|
+
const resolveResponse = (response, options) => {
|
|
210
193
|
options = options || {};
|
|
211
194
|
if (!response.items) {
|
|
212
195
|
return [];
|
|
213
196
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
});
|
|
221
|
-
var entityMap = new Map(allEntries.reduce(function (acc, entity) {
|
|
222
|
-
var entries = makeEntityMapKeys(entity.sys).map(function (key) {
|
|
223
|
-
return [key, entity];
|
|
224
|
-
});
|
|
225
|
-
acc.push.apply(acc, _toConsumableArray(entries));
|
|
197
|
+
const responseClone = (0, _fastCopy.default)(response);
|
|
198
|
+
const allIncludes = Object.keys(responseClone.includes || {}).reduce((all, type) => [...all, ...responseClone.includes[type]], []);
|
|
199
|
+
const allEntries = [...responseClone.items, ...allIncludes].filter(entity => Boolean(entity.sys));
|
|
200
|
+
const entityMap = new Map(allEntries.reduce((acc, entity) => {
|
|
201
|
+
const entries = makeEntityMapKeys(entity.sys).map(key => [key, entity]);
|
|
202
|
+
acc.push(...entries);
|
|
226
203
|
return acc;
|
|
227
204
|
}, []));
|
|
228
|
-
allEntries.forEach(
|
|
229
|
-
|
|
230
|
-
Object.assign(item,
|
|
231
|
-
return isLink(x) || isResourceLink(x);
|
|
232
|
-
}, function (link) {
|
|
233
|
-
return normalizeLink(entityMap, link, options.removeUnresolved);
|
|
234
|
-
}, options.removeUnresolved));
|
|
205
|
+
allEntries.forEach(item => {
|
|
206
|
+
const entryObject = makeEntryObject(item, options.itemEntryPoints);
|
|
207
|
+
Object.assign(item, walkMutate(entryObject, x => isLink(x) || isResourceLink(x), link => normalizeLink(entityMap, link, options.removeUnresolved), options.removeUnresolved));
|
|
235
208
|
});
|
|
236
209
|
return responseClone.items;
|
|
237
210
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "contentful-resolve-response",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"url": "git@github.com:contentful/contentful-resolve-response.git"
|
|
21
21
|
},
|
|
22
22
|
"engines": {
|
|
23
|
-
"node": ">=
|
|
23
|
+
"node": ">=22"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"fast-copy": "^3.0.2"
|
|
@@ -87,6 +87,10 @@
|
|
|
87
87
|
"@semantic-release/commit-analyzer",
|
|
88
88
|
{
|
|
89
89
|
"releaseRules": [
|
|
90
|
+
{
|
|
91
|
+
"breaking": true,
|
|
92
|
+
"release": "major"
|
|
93
|
+
},
|
|
90
94
|
{
|
|
91
95
|
"type": "build",
|
|
92
96
|
"scope": "deps",
|