contentful-resolve-response 1.9.8 → 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/README.md +5 -0
- package/dist/cjs/index.js +66 -109
- package/dist/esm/index.js +42 -57
- package/package.json +12 -8
package/README.md
CHANGED
|
@@ -48,3 +48,8 @@ Note that:
|
|
|
48
48
|
|
|
49
49
|
- Multiple links to the same resource will point to the same object
|
|
50
50
|
- Circular references are possible, still!!
|
|
51
|
+
|
|
52
|
+
## For AI Agents
|
|
53
|
+
|
|
54
|
+
<!-- Generated by seed-golden-context | Last updated: 2026-05-07 -->
|
|
55
|
+
If you are an AI coding agent working in this repository, read [AGENTS.md](./AGENTS.md) first. It tells you where to find architectural context, development setup, decision records, and repo-specific rules.
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,38 +1,26 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var _fastCopy2 = _interopRequireDefault(_fastCopy);
|
|
12
|
-
|
|
13
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
|
-
|
|
15
|
-
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
|
16
|
-
|
|
17
|
-
var UNRESOLVED_LINK = {}; // unique object to avoid polyfill bloat using Symbol()
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _fastCopy = _interopRequireDefault(require("fast-copy"));
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
const UNRESOLVED_LINK = {}; // unique object to avoid polyfill bloat using Symbol()
|
|
18
10
|
|
|
19
11
|
/**
|
|
20
12
|
* isLink Function
|
|
21
13
|
* Checks if the object has sys.type "Link"
|
|
22
14
|
* @param object
|
|
23
15
|
*/
|
|
24
|
-
|
|
25
|
-
return object && object.sys && object.sys.type === 'Link';
|
|
26
|
-
};
|
|
16
|
+
const isLink = object => object && object.sys && object.sys.type === 'Link';
|
|
27
17
|
|
|
28
18
|
/**
|
|
29
19
|
* isResourceLink Function
|
|
30
20
|
* Checks if the object has sys.type "ResourceLink"
|
|
31
21
|
* @param object
|
|
32
22
|
*/
|
|
33
|
-
|
|
34
|
-
return object && object.sys && object.sys.type === 'ResourceLink';
|
|
35
|
-
};
|
|
23
|
+
const isResourceLink = object => object && object.sys && object.sys.type === 'ResourceLink';
|
|
36
24
|
|
|
37
25
|
/**
|
|
38
26
|
* Creates a key with spaceId and a key without for entityMap
|
|
@@ -45,11 +33,10 @@ var isResourceLink = function isResourceLink(object) {
|
|
|
45
33
|
* @param {String} sys.space.id
|
|
46
34
|
* @return {string[]}
|
|
47
35
|
*/
|
|
48
|
-
|
|
36
|
+
const makeEntityMapKeys = sys => {
|
|
49
37
|
if (sys.space && sys.environment) {
|
|
50
38
|
return [`${sys.type}!${sys.id}`, `${sys.space.sys.id}!${sys.environment.sys.id}!${sys.type}!${sys.id}`];
|
|
51
39
|
}
|
|
52
|
-
|
|
53
40
|
return [`${sys.type}!${sys.id}`];
|
|
54
41
|
};
|
|
55
42
|
|
|
@@ -64,38 +51,31 @@ var makeEntityMapKeys = function makeEntityMapKeys(sys) {
|
|
|
64
51
|
* @param {String} linkData.urn
|
|
65
52
|
* @return {String}
|
|
66
53
|
*/
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
78
64
|
return entityMap.get(`${linkType}!${entryId}`);
|
|
79
65
|
};
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
var regExp = /.*:spaces\/([^/]+)(?:\/environments\/([^/]+))?\/entries\/([^/]+)$/;
|
|
83
|
-
|
|
66
|
+
const getIdsFromUrn = urn => {
|
|
67
|
+
const regExp = /.*:spaces\/([^/]+)(?:\/environments\/([^/]+))?\/entries\/([^/]+)$/;
|
|
84
68
|
if (!regExp.test(urn)) {
|
|
85
69
|
return undefined;
|
|
86
70
|
}
|
|
87
71
|
|
|
88
72
|
// eslint-disable-next-line no-unused-vars
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
environmentId = _urn$match2$ === undefined ? 'master' : _urn$match2$,
|
|
96
|
-
entryId = _urn$match2[3];
|
|
97
|
-
|
|
98
|
-
return { spaceId, environmentId, entryId };
|
|
73
|
+
const [_, spaceId, environmentId = 'master', entryId] = urn.match(regExp);
|
|
74
|
+
return {
|
|
75
|
+
spaceId,
|
|
76
|
+
environmentId,
|
|
77
|
+
entryId
|
|
78
|
+
};
|
|
99
79
|
};
|
|
100
80
|
|
|
101
81
|
/**
|
|
@@ -105,36 +85,38 @@ var getIdsFromUrn = function getIdsFromUrn(urn) {
|
|
|
105
85
|
* @param link
|
|
106
86
|
* @return {undefined}
|
|
107
87
|
*/
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
88
|
+
const getResolvedLink = (entityMap, link) => {
|
|
89
|
+
const {
|
|
90
|
+
type,
|
|
91
|
+
linkType
|
|
92
|
+
} = link.sys;
|
|
113
93
|
if (type === 'ResourceLink') {
|
|
114
94
|
if (!linkType.startsWith('Contentful:')) {
|
|
115
95
|
return link;
|
|
116
96
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
97
|
+
const {
|
|
98
|
+
urn
|
|
99
|
+
} = link.sys;
|
|
100
|
+
const {
|
|
101
|
+
spaceId,
|
|
102
|
+
environmentId,
|
|
103
|
+
entryId
|
|
104
|
+
} = getIdsFromUrn(urn);
|
|
105
|
+
const extractedLinkType = linkType.split(':')[1];
|
|
127
106
|
return lookupInEntityMap(entityMap, {
|
|
128
107
|
linkType: extractedLinkType,
|
|
129
|
-
entryId
|
|
108
|
+
entryId,
|
|
130
109
|
spaceId,
|
|
131
110
|
environmentId
|
|
132
111
|
}) || UNRESOLVED_LINK;
|
|
133
112
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return lookupInEntityMap(entityMap, {
|
|
113
|
+
const {
|
|
114
|
+
id: entryId
|
|
115
|
+
} = link.sys;
|
|
116
|
+
return lookupInEntityMap(entityMap, {
|
|
117
|
+
linkType,
|
|
118
|
+
entryId
|
|
119
|
+
}) || UNRESOLVED_LINK;
|
|
138
120
|
};
|
|
139
121
|
|
|
140
122
|
/**
|
|
@@ -143,13 +125,11 @@ var getResolvedLink = function getResolvedLink(entityMap, link) {
|
|
|
143
125
|
*
|
|
144
126
|
* @param {Object[]|Object} input
|
|
145
127
|
*/
|
|
146
|
-
|
|
128
|
+
const cleanUpLinks = input => {
|
|
147
129
|
if (Array.isArray(input)) {
|
|
148
|
-
return input.filter(
|
|
149
|
-
return val !== UNRESOLVED_LINK;
|
|
150
|
-
});
|
|
130
|
+
return input.filter(val => val !== UNRESOLVED_LINK);
|
|
151
131
|
}
|
|
152
|
-
for (
|
|
132
|
+
for (const key in input) {
|
|
153
133
|
if (input[key] === UNRESOLVED_LINK) {
|
|
154
134
|
delete input[key];
|
|
155
135
|
}
|
|
@@ -165,13 +145,12 @@ var cleanUpLinks = function cleanUpLinks(input) {
|
|
|
165
145
|
* @param removeUnresolved
|
|
166
146
|
* @return {*}
|
|
167
147
|
*/
|
|
168
|
-
|
|
148
|
+
const walkMutate = (input, predicate, mutator, removeUnresolved) => {
|
|
169
149
|
if (predicate(input)) {
|
|
170
150
|
return mutator(input);
|
|
171
151
|
}
|
|
172
|
-
|
|
173
152
|
if (input && typeof input === 'object') {
|
|
174
|
-
for (
|
|
153
|
+
for (const key in input) {
|
|
175
154
|
// eslint-disable-next-line no-prototype-builtins
|
|
176
155
|
if (input.hasOwnProperty(key)) {
|
|
177
156
|
input[key] = walkMutate(input[key], predicate, mutator, removeUnresolved);
|
|
@@ -183,25 +162,19 @@ var walkMutate = function walkMutate(input, predicate, mutator, removeUnresolved
|
|
|
183
162
|
}
|
|
184
163
|
return input;
|
|
185
164
|
};
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
var resolvedLink = getResolvedLink(entityMap, link);
|
|
165
|
+
const normalizeLink = (entityMap, link, removeUnresolved) => {
|
|
166
|
+
const resolvedLink = getResolvedLink(entityMap, link);
|
|
189
167
|
if (resolvedLink === UNRESOLVED_LINK) {
|
|
190
168
|
return removeUnresolved ? resolvedLink : link;
|
|
191
169
|
}
|
|
192
170
|
return resolvedLink;
|
|
193
171
|
};
|
|
194
|
-
|
|
195
|
-
var makeEntryObject = function makeEntryObject(item, itemEntryPoints) {
|
|
172
|
+
const makeEntryObject = (item, itemEntryPoints) => {
|
|
196
173
|
if (!Array.isArray(itemEntryPoints)) {
|
|
197
174
|
return item;
|
|
198
175
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
return itemEntryPoints.indexOf(ownKey) !== -1;
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
return entryPoints.reduce(function (entryObj, entryPoint) {
|
|
176
|
+
const entryPoints = Object.keys(item).filter(ownKey => itemEntryPoints.indexOf(ownKey) !== -1);
|
|
177
|
+
return entryPoints.reduce((entryObj, entryPoint) => {
|
|
205
178
|
entryObj[entryPoint] = item[entryPoint];
|
|
206
179
|
return entryObj;
|
|
207
180
|
}, {});
|
|
@@ -216,40 +189,24 @@ var makeEntryObject = function makeEntryObject(item, itemEntryPoints) {
|
|
|
216
189
|
* @param {Array<String>} options.itemEntryPoints - Resolve links only in those item properties
|
|
217
190
|
* @return {Object}
|
|
218
191
|
*/
|
|
219
|
-
|
|
192
|
+
const resolveResponse = (response, options) => {
|
|
220
193
|
options = options || {};
|
|
221
194
|
if (!response.items) {
|
|
222
195
|
return [];
|
|
223
196
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
return Boolean(entity.sys);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
var entityMap = new Map(allEntries.reduce(function (acc, entity) {
|
|
234
|
-
var entries = makeEntityMapKeys(entity.sys).map(function (key) {
|
|
235
|
-
return [key, entity];
|
|
236
|
-
});
|
|
237
|
-
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);
|
|
238
203
|
return acc;
|
|
239
204
|
}, []));
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
Object.assign(item, walkMutate(entryObject, function (x) {
|
|
245
|
-
return isLink(x) || isResourceLink(x);
|
|
246
|
-
}, function (link) {
|
|
247
|
-
return normalizeLink(entityMap, link, options.removeUnresolved);
|
|
248
|
-
}, 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));
|
|
249
208
|
});
|
|
250
|
-
|
|
251
209
|
return responseClone.items;
|
|
252
210
|
};
|
|
253
|
-
|
|
254
|
-
exports.default = resolveResponse;
|
|
211
|
+
var _default = exports.default = resolveResponse;
|
|
255
212
|
module.exports = exports.default;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function
|
|
6
|
-
|
|
1
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
2
|
+
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."); }
|
|
3
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
4
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
5
|
+
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); }
|
|
6
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
7
|
+
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."); }
|
|
8
|
+
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; } }
|
|
9
|
+
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; }
|
|
10
|
+
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; } }
|
|
11
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
7
12
|
import copy from 'fast-copy';
|
|
8
|
-
|
|
9
13
|
var UNRESOLVED_LINK = {}; // unique object to avoid polyfill bloat using Symbol()
|
|
10
14
|
|
|
11
15
|
/**
|
|
@@ -39,10 +43,9 @@ var isResourceLink = function isResourceLink(object) {
|
|
|
39
43
|
*/
|
|
40
44
|
var makeEntityMapKeys = function makeEntityMapKeys(sys) {
|
|
41
45
|
if (sys.space && sys.environment) {
|
|
42
|
-
return [sys.type
|
|
46
|
+
return ["".concat(sys.type, "!").concat(sys.id), "".concat(sys.space.sys.id, "!").concat(sys.environment.sys.id, "!").concat(sys.type, "!").concat(sys.id)];
|
|
43
47
|
}
|
|
44
|
-
|
|
45
|
-
return [sys.type + '!' + sys.id];
|
|
48
|
+
return ["".concat(sys.type, "!").concat(sys.id)];
|
|
46
49
|
};
|
|
47
50
|
|
|
48
51
|
/**
|
|
@@ -58,36 +61,33 @@ var makeEntityMapKeys = function makeEntityMapKeys(sys) {
|
|
|
58
61
|
*/
|
|
59
62
|
var lookupInEntityMap = function lookupInEntityMap(entityMap, linkData) {
|
|
60
63
|
var entryId = linkData.entryId,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
linkType = linkData.linkType,
|
|
65
|
+
spaceId = linkData.spaceId,
|
|
66
|
+
environmentId = linkData.environmentId;
|
|
66
67
|
if (spaceId && environmentId) {
|
|
67
|
-
return entityMap.get(spaceId
|
|
68
|
+
return entityMap.get("".concat(spaceId, "!").concat(environmentId, "!").concat(linkType, "!").concat(entryId));
|
|
68
69
|
}
|
|
69
|
-
|
|
70
|
-
return entityMap.get(linkType + '!' + entryId);
|
|
70
|
+
return entityMap.get("".concat(linkType, "!").concat(entryId));
|
|
71
71
|
};
|
|
72
|
-
|
|
73
72
|
var getIdsFromUrn = function getIdsFromUrn(urn) {
|
|
74
73
|
var regExp = /.*:spaces\/([^/]+)(?:\/environments\/([^/]+))?\/entries\/([^/]+)$/;
|
|
75
|
-
|
|
76
74
|
if (!regExp.test(urn)) {
|
|
77
75
|
return undefined;
|
|
78
76
|
}
|
|
79
77
|
|
|
80
78
|
// eslint-disable-next-line no-unused-vars
|
|
81
|
-
|
|
82
79
|
var _urn$match = urn.match(regExp),
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
80
|
+
_urn$match2 = _slicedToArray(_urn$match, 4),
|
|
81
|
+
_ = _urn$match2[0],
|
|
82
|
+
spaceId = _urn$match2[1],
|
|
83
|
+
_urn$match2$ = _urn$match2[2],
|
|
84
|
+
environmentId = _urn$match2$ === void 0 ? 'master' : _urn$match2$,
|
|
85
|
+
entryId = _urn$match2[3];
|
|
86
|
+
return {
|
|
87
|
+
spaceId: spaceId,
|
|
88
|
+
environmentId: environmentId,
|
|
89
|
+
entryId: entryId
|
|
90
|
+
};
|
|
91
91
|
};
|
|
92
92
|
|
|
93
93
|
/**
|
|
@@ -99,23 +99,18 @@ var getIdsFromUrn = function getIdsFromUrn(urn) {
|
|
|
99
99
|
*/
|
|
100
100
|
var getResolvedLink = function getResolvedLink(entityMap, link) {
|
|
101
101
|
var _link$sys = link.sys,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
type = _link$sys.type,
|
|
103
|
+
linkType = _link$sys.linkType;
|
|
105
104
|
if (type === 'ResourceLink') {
|
|
106
105
|
if (!linkType.startsWith('Contentful:')) {
|
|
107
106
|
return link;
|
|
108
107
|
}
|
|
109
|
-
|
|
110
108
|
var urn = link.sys.urn;
|
|
111
|
-
|
|
112
109
|
var _getIdsFromUrn = getIdsFromUrn(urn),
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
110
|
+
spaceId = _getIdsFromUrn.spaceId,
|
|
111
|
+
environmentId = _getIdsFromUrn.environmentId,
|
|
112
|
+
_entryId = _getIdsFromUrn.entryId;
|
|
117
113
|
var extractedLinkType = linkType.split(':')[1];
|
|
118
|
-
|
|
119
114
|
return lookupInEntityMap(entityMap, {
|
|
120
115
|
linkType: extractedLinkType,
|
|
121
116
|
entryId: _entryId,
|
|
@@ -123,10 +118,11 @@ var getResolvedLink = function getResolvedLink(entityMap, link) {
|
|
|
123
118
|
environmentId: environmentId
|
|
124
119
|
}) || UNRESOLVED_LINK;
|
|
125
120
|
}
|
|
126
|
-
|
|
127
121
|
var entryId = link.sys.id;
|
|
128
|
-
|
|
129
|
-
|
|
122
|
+
return lookupInEntityMap(entityMap, {
|
|
123
|
+
linkType: linkType,
|
|
124
|
+
entryId: entryId
|
|
125
|
+
}) || UNRESOLVED_LINK;
|
|
130
126
|
};
|
|
131
127
|
|
|
132
128
|
/**
|
|
@@ -157,16 +153,15 @@ var cleanUpLinks = function cleanUpLinks(input) {
|
|
|
157
153
|
* @param removeUnresolved
|
|
158
154
|
* @return {*}
|
|
159
155
|
*/
|
|
160
|
-
var
|
|
156
|
+
var _walkMutate = function walkMutate(input, predicate, mutator, removeUnresolved) {
|
|
161
157
|
if (predicate(input)) {
|
|
162
158
|
return mutator(input);
|
|
163
159
|
}
|
|
164
|
-
|
|
165
|
-
if (input && (typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object') {
|
|
160
|
+
if (input && _typeof(input) === 'object') {
|
|
166
161
|
for (var key in input) {
|
|
167
162
|
// eslint-disable-next-line no-prototype-builtins
|
|
168
163
|
if (input.hasOwnProperty(key)) {
|
|
169
|
-
input[key] =
|
|
164
|
+
input[key] = _walkMutate(input[key], predicate, mutator, removeUnresolved);
|
|
170
165
|
}
|
|
171
166
|
}
|
|
172
167
|
if (removeUnresolved) {
|
|
@@ -175,7 +170,6 @@ var walkMutate = function walkMutate(input, predicate, mutator, removeUnresolved
|
|
|
175
170
|
}
|
|
176
171
|
return input;
|
|
177
172
|
};
|
|
178
|
-
|
|
179
173
|
var normalizeLink = function normalizeLink(entityMap, link, removeUnresolved) {
|
|
180
174
|
var resolvedLink = getResolvedLink(entityMap, link);
|
|
181
175
|
if (resolvedLink === UNRESOLVED_LINK) {
|
|
@@ -183,16 +177,13 @@ var normalizeLink = function normalizeLink(entityMap, link, removeUnresolved) {
|
|
|
183
177
|
}
|
|
184
178
|
return resolvedLink;
|
|
185
179
|
};
|
|
186
|
-
|
|
187
180
|
var makeEntryObject = function makeEntryObject(item, itemEntryPoints) {
|
|
188
181
|
if (!Array.isArray(itemEntryPoints)) {
|
|
189
182
|
return item;
|
|
190
183
|
}
|
|
191
|
-
|
|
192
184
|
var entryPoints = Object.keys(item).filter(function (ownKey) {
|
|
193
185
|
return itemEntryPoints.indexOf(ownKey) !== -1;
|
|
194
186
|
});
|
|
195
|
-
|
|
196
187
|
return entryPoints.reduce(function (entryObj, entryPoint) {
|
|
197
188
|
entryObj[entryPoint] = item[entryPoint];
|
|
198
189
|
return entryObj;
|
|
@@ -217,11 +208,9 @@ var resolveResponse = function resolveResponse(response, options) {
|
|
|
217
208
|
var allIncludes = Object.keys(responseClone.includes || {}).reduce(function (all, type) {
|
|
218
209
|
return [].concat(_toConsumableArray(all), _toConsumableArray(responseClone.includes[type]));
|
|
219
210
|
}, []);
|
|
220
|
-
|
|
221
211
|
var allEntries = [].concat(_toConsumableArray(responseClone.items), _toConsumableArray(allIncludes)).filter(function (entity) {
|
|
222
212
|
return Boolean(entity.sys);
|
|
223
213
|
});
|
|
224
|
-
|
|
225
214
|
var entityMap = new Map(allEntries.reduce(function (acc, entity) {
|
|
226
215
|
var entries = makeEntityMapKeys(entity.sys).map(function (key) {
|
|
227
216
|
return [key, entity];
|
|
@@ -229,18 +218,14 @@ var resolveResponse = function resolveResponse(response, options) {
|
|
|
229
218
|
acc.push.apply(acc, _toConsumableArray(entries));
|
|
230
219
|
return acc;
|
|
231
220
|
}, []));
|
|
232
|
-
|
|
233
221
|
allEntries.forEach(function (item) {
|
|
234
222
|
var entryObject = makeEntryObject(item, options.itemEntryPoints);
|
|
235
|
-
|
|
236
|
-
Object.assign(item, walkMutate(entryObject, function (x) {
|
|
223
|
+
Object.assign(item, _walkMutate(entryObject, function (x) {
|
|
237
224
|
return isLink(x) || isResourceLink(x);
|
|
238
225
|
}, function (link) {
|
|
239
226
|
return normalizeLink(entityMap, link, options.removeUnresolved);
|
|
240
227
|
}, options.removeUnresolved));
|
|
241
228
|
});
|
|
242
|
-
|
|
243
229
|
return responseClone.items;
|
|
244
230
|
};
|
|
245
|
-
|
|
246
231
|
export default resolveResponse;
|
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",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "BABEL_ENV=cjs babel index.js -d dist/cjs/ && BABEL_ENV=esm babel index.js -d dist/esm/",
|
|
10
10
|
"lint": "eslint index.js test",
|
|
11
|
-
"test": "BABEL_ENV=cjs mocha --require babel
|
|
11
|
+
"test": "BABEL_ENV=cjs mocha --require @babel/register 'test/**/*-test.js'",
|
|
12
12
|
"test-watch": "npm run test -- --watch",
|
|
13
13
|
"format:fix": "npm run format:check -- --write",
|
|
14
14
|
"format:check": "prettier --check \"{*.js,**/*.js,*.ts,**/*.ts,*.json,**/*.json}\"",
|
|
@@ -20,27 +20,27 @@
|
|
|
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"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@commitlint/cli": "^
|
|
29
|
+
"@commitlint/cli": "^21.0.0",
|
|
30
30
|
"@commitlint/config-conventional": "^18.0.0",
|
|
31
31
|
"@eslint/js": "^10.0.1",
|
|
32
32
|
"@semantic-release/changelog": "^6.0.1",
|
|
33
33
|
"@semantic-release/git": "^10.0.1",
|
|
34
|
-
"babel
|
|
34
|
+
"@babel/cli": "^7.0.0",
|
|
35
|
+
"@babel/preset-env": "^7.0.0",
|
|
36
|
+
"@babel/register": "^7.0.0",
|
|
35
37
|
"babel-plugin-add-module-exports": "^1.0.2",
|
|
36
|
-
"babel-preset-env": "^1.7.0",
|
|
37
|
-
"babel-register": "^6.26.0",
|
|
38
38
|
"chai": "^4.3.6",
|
|
39
39
|
"dirty-chai": "^2.0.1",
|
|
40
40
|
"eslint": "^10.0.0",
|
|
41
41
|
"eslint-plugin-mocha": "^11.0.0",
|
|
42
42
|
"husky": "^9.0.6",
|
|
43
|
-
"lint-staged": "^
|
|
43
|
+
"lint-staged": "^17.0.2",
|
|
44
44
|
"mocha": "^11.0.1",
|
|
45
45
|
"prettier": "^3.0.0",
|
|
46
46
|
"semantic-release": "^25.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",
|