contentful-resolve-response 1.3.11 → 1.4.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 +70 -15
- package/dist/esm/index.js +70 -15
- package/package.json +2 -2
package/dist/cjs/index.js
CHANGED
|
@@ -24,32 +24,80 @@ var isLink = function isLink(object) {
|
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* isResourceLink Function
|
|
28
|
+
* Checks if the object has sys.type "ResourceLink"
|
|
29
|
+
* @param object
|
|
30
|
+
*/
|
|
31
|
+
var isResourceLink = function isResourceLink(object) {
|
|
32
|
+
return object && object.sys && object.sys.type === 'ResourceLink';
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Creates a key with spaceId and a key without for entityMap
|
|
28
37
|
*
|
|
29
38
|
* @param {*} sys
|
|
30
39
|
* @param {String} sys.type
|
|
31
40
|
* @param {String} sys.id
|
|
41
|
+
* @param {*} sys.space
|
|
42
|
+
* @param {*} sys.space.sys
|
|
43
|
+
* @param {String} sys.space.id
|
|
44
|
+
* @return {string[]}
|
|
45
|
+
*/
|
|
46
|
+
var makeEntityMapKeys = function makeEntityMapKeys(sys) {
|
|
47
|
+
return sys.space ? [`${sys.type}!${sys.id}`, `${sys.space.sys.id}!${sys.type}!${sys.id}`] : [`${sys.type}!${sys.id}`];
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Looks up in entityMap
|
|
52
|
+
*
|
|
53
|
+
* @param entityMap
|
|
54
|
+
* @param {*} linkData
|
|
55
|
+
* @param {String} linkData.type
|
|
56
|
+
* @param {String} linkData.linkType
|
|
57
|
+
* @param {String} linkData.id
|
|
58
|
+
* @param {String} linkData.urn
|
|
32
59
|
* @return {String}
|
|
33
60
|
*/
|
|
34
|
-
var
|
|
35
|
-
|
|
61
|
+
var lookupInEntityMap = function lookupInEntityMap(entityMap, linkData) {
|
|
62
|
+
var entryId = linkData.entryId,
|
|
63
|
+
linkType = linkData.linkType,
|
|
64
|
+
spaceId = linkData.spaceId;
|
|
65
|
+
|
|
66
|
+
if (spaceId) {
|
|
67
|
+
return entityMap.get(`${spaceId}!${linkType}!${entryId}`);
|
|
68
|
+
}
|
|
69
|
+
return entityMap.get(`${linkType}!${entryId}`);
|
|
36
70
|
};
|
|
37
71
|
|
|
38
72
|
/**
|
|
39
|
-
*
|
|
73
|
+
* getResolvedLink Function
|
|
40
74
|
*
|
|
41
|
-
* @param
|
|
75
|
+
* @param entityMap
|
|
42
76
|
* @param link
|
|
43
77
|
* @return {undefined}
|
|
44
78
|
*/
|
|
45
|
-
var
|
|
79
|
+
var getResolvedLink = function getResolvedLink(entityMap, link) {
|
|
46
80
|
var _link$sys = link.sys,
|
|
47
|
-
type = _link$sys.
|
|
48
|
-
|
|
81
|
+
type = _link$sys.type,
|
|
82
|
+
linkType = _link$sys.linkType;
|
|
83
|
+
|
|
84
|
+
if (type === 'ResourceLink') {
|
|
85
|
+
var urn = link.sys.urn;
|
|
49
86
|
|
|
50
|
-
|
|
87
|
+
var regExp = /.*:spaces\/(?<spaceId>[A-Za-z0-9]*)\/entries\/(?<entryId>[A-Za-z0-9]*)/;
|
|
88
|
+
if (!regExp.test(urn)) {
|
|
89
|
+
return UNRESOLVED_LINK;
|
|
90
|
+
}
|
|
91
|
+
var _urn$match$groups = urn.match(regExp).groups,
|
|
92
|
+
spaceId = _urn$match$groups.spaceId,
|
|
93
|
+
_entryId = _urn$match$groups.entryId;
|
|
94
|
+
|
|
95
|
+
var extractedLinkType = linkType.split(':')[1];
|
|
96
|
+
return lookupInEntityMap(entityMap, { linkType: extractedLinkType, entryId: _entryId, spaceId }) || UNRESOLVED_LINK;
|
|
97
|
+
}
|
|
98
|
+
var entryId = link.sys.id;
|
|
51
99
|
|
|
52
|
-
return entityMap
|
|
100
|
+
return lookupInEntityMap(entityMap, { linkType, entryId }) || UNRESOLVED_LINK;
|
|
53
101
|
};
|
|
54
102
|
|
|
55
103
|
/**
|
|
@@ -77,6 +125,7 @@ var cleanUpLinks = function cleanUpLinks(input) {
|
|
|
77
125
|
* @param input
|
|
78
126
|
* @param predicate
|
|
79
127
|
* @param mutator
|
|
128
|
+
* @param removeUnresolved
|
|
80
129
|
* @return {*}
|
|
81
130
|
*/
|
|
82
131
|
var walkMutate = function walkMutate(input, predicate, mutator, removeUnresolved) {
|
|
@@ -99,7 +148,7 @@ var walkMutate = function walkMutate(input, predicate, mutator, removeUnresolved
|
|
|
99
148
|
};
|
|
100
149
|
|
|
101
150
|
var normalizeLink = function normalizeLink(entityMap, link, removeUnresolved) {
|
|
102
|
-
var resolvedLink =
|
|
151
|
+
var resolvedLink = getResolvedLink(entityMap, link);
|
|
103
152
|
if (resolvedLink === UNRESOLVED_LINK) {
|
|
104
153
|
return removeUnresolved ? resolvedLink : link;
|
|
105
154
|
}
|
|
@@ -142,14 +191,20 @@ var resolveResponse = function resolveResponse(response, options) {
|
|
|
142
191
|
|
|
143
192
|
var allEntries = [].concat(_toConsumableArray(responseClone.items), _toConsumableArray(allIncludes));
|
|
144
193
|
|
|
145
|
-
var entityMap = new Map(allEntries.
|
|
146
|
-
|
|
147
|
-
|
|
194
|
+
var entityMap = new Map(allEntries.reduce(function (acc, entity) {
|
|
195
|
+
var entries = makeEntityMapKeys(entity.sys).map(function (key) {
|
|
196
|
+
return [key, entity];
|
|
197
|
+
});
|
|
198
|
+
acc.push.apply(acc, _toConsumableArray(entries));
|
|
199
|
+
return acc;
|
|
200
|
+
}, []));
|
|
148
201
|
|
|
149
202
|
allEntries.forEach(function (item) {
|
|
150
203
|
var entryObject = makeEntryObject(item, options.itemEntryPoints);
|
|
151
204
|
|
|
152
|
-
Object.assign(item, walkMutate(entryObject,
|
|
205
|
+
Object.assign(item, walkMutate(entryObject, function (x) {
|
|
206
|
+
return isLink(x) || isResourceLink(x);
|
|
207
|
+
}, function (link) {
|
|
153
208
|
return normalizeLink(entityMap, link, options.removeUnresolved);
|
|
154
209
|
}, options.removeUnresolved));
|
|
155
210
|
});
|
package/dist/esm/index.js
CHANGED
|
@@ -16,32 +16,80 @@ var isLink = function isLink(object) {
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
19
|
+
* isResourceLink Function
|
|
20
|
+
* Checks if the object has sys.type "ResourceLink"
|
|
21
|
+
* @param object
|
|
22
|
+
*/
|
|
23
|
+
var isResourceLink = function isResourceLink(object) {
|
|
24
|
+
return object && object.sys && object.sys.type === 'ResourceLink';
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates a key with spaceId and a key without for entityMap
|
|
20
29
|
*
|
|
21
30
|
* @param {*} sys
|
|
22
31
|
* @param {String} sys.type
|
|
23
32
|
* @param {String} sys.id
|
|
33
|
+
* @param {*} sys.space
|
|
34
|
+
* @param {*} sys.space.sys
|
|
35
|
+
* @param {String} sys.space.id
|
|
36
|
+
* @return {string[]}
|
|
37
|
+
*/
|
|
38
|
+
var makeEntityMapKeys = function makeEntityMapKeys(sys) {
|
|
39
|
+
return sys.space ? [sys.type + '!' + sys.id, sys.space.sys.id + '!' + sys.type + '!' + sys.id] : [sys.type + '!' + sys.id];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Looks up in entityMap
|
|
44
|
+
*
|
|
45
|
+
* @param entityMap
|
|
46
|
+
* @param {*} linkData
|
|
47
|
+
* @param {String} linkData.type
|
|
48
|
+
* @param {String} linkData.linkType
|
|
49
|
+
* @param {String} linkData.id
|
|
50
|
+
* @param {String} linkData.urn
|
|
24
51
|
* @return {String}
|
|
25
52
|
*/
|
|
26
|
-
var
|
|
27
|
-
|
|
53
|
+
var lookupInEntityMap = function lookupInEntityMap(entityMap, linkData) {
|
|
54
|
+
var entryId = linkData.entryId,
|
|
55
|
+
linkType = linkData.linkType,
|
|
56
|
+
spaceId = linkData.spaceId;
|
|
57
|
+
|
|
58
|
+
if (spaceId) {
|
|
59
|
+
return entityMap.get(spaceId + '!' + linkType + '!' + entryId);
|
|
60
|
+
}
|
|
61
|
+
return entityMap.get(linkType + '!' + entryId);
|
|
28
62
|
};
|
|
29
63
|
|
|
30
64
|
/**
|
|
31
|
-
*
|
|
65
|
+
* getResolvedLink Function
|
|
32
66
|
*
|
|
33
|
-
* @param
|
|
67
|
+
* @param entityMap
|
|
34
68
|
* @param link
|
|
35
69
|
* @return {undefined}
|
|
36
70
|
*/
|
|
37
|
-
var
|
|
71
|
+
var getResolvedLink = function getResolvedLink(entityMap, link) {
|
|
38
72
|
var _link$sys = link.sys,
|
|
39
|
-
type = _link$sys.
|
|
40
|
-
|
|
73
|
+
type = _link$sys.type,
|
|
74
|
+
linkType = _link$sys.linkType;
|
|
75
|
+
|
|
76
|
+
if (type === 'ResourceLink') {
|
|
77
|
+
var urn = link.sys.urn;
|
|
41
78
|
|
|
42
|
-
|
|
79
|
+
var regExp = /.*:spaces\/(?<spaceId>[A-Za-z0-9]*)\/entries\/(?<entryId>[A-Za-z0-9]*)/;
|
|
80
|
+
if (!regExp.test(urn)) {
|
|
81
|
+
return UNRESOLVED_LINK;
|
|
82
|
+
}
|
|
83
|
+
var _urn$match$groups = urn.match(regExp).groups,
|
|
84
|
+
spaceId = _urn$match$groups.spaceId,
|
|
85
|
+
_entryId = _urn$match$groups.entryId;
|
|
86
|
+
|
|
87
|
+
var extractedLinkType = linkType.split(':')[1];
|
|
88
|
+
return lookupInEntityMap(entityMap, { linkType: extractedLinkType, entryId: _entryId, spaceId: spaceId }) || UNRESOLVED_LINK;
|
|
89
|
+
}
|
|
90
|
+
var entryId = link.sys.id;
|
|
43
91
|
|
|
44
|
-
return entityMap
|
|
92
|
+
return lookupInEntityMap(entityMap, { linkType: linkType, entryId: entryId }) || UNRESOLVED_LINK;
|
|
45
93
|
};
|
|
46
94
|
|
|
47
95
|
/**
|
|
@@ -69,6 +117,7 @@ var cleanUpLinks = function cleanUpLinks(input) {
|
|
|
69
117
|
* @param input
|
|
70
118
|
* @param predicate
|
|
71
119
|
* @param mutator
|
|
120
|
+
* @param removeUnresolved
|
|
72
121
|
* @return {*}
|
|
73
122
|
*/
|
|
74
123
|
var walkMutate = function walkMutate(input, predicate, mutator, removeUnresolved) {
|
|
@@ -91,7 +140,7 @@ var walkMutate = function walkMutate(input, predicate, mutator, removeUnresolved
|
|
|
91
140
|
};
|
|
92
141
|
|
|
93
142
|
var normalizeLink = function normalizeLink(entityMap, link, removeUnresolved) {
|
|
94
|
-
var resolvedLink =
|
|
143
|
+
var resolvedLink = getResolvedLink(entityMap, link);
|
|
95
144
|
if (resolvedLink === UNRESOLVED_LINK) {
|
|
96
145
|
return removeUnresolved ? resolvedLink : link;
|
|
97
146
|
}
|
|
@@ -134,14 +183,20 @@ var resolveResponse = function resolveResponse(response, options) {
|
|
|
134
183
|
|
|
135
184
|
var allEntries = [].concat(_toConsumableArray(responseClone.items), _toConsumableArray(allIncludes));
|
|
136
185
|
|
|
137
|
-
var entityMap = new Map(allEntries.
|
|
138
|
-
|
|
139
|
-
|
|
186
|
+
var entityMap = new Map(allEntries.reduce(function (acc, entity) {
|
|
187
|
+
var entries = makeEntityMapKeys(entity.sys).map(function (key) {
|
|
188
|
+
return [key, entity];
|
|
189
|
+
});
|
|
190
|
+
acc.push.apply(acc, _toConsumableArray(entries));
|
|
191
|
+
return acc;
|
|
192
|
+
}, []));
|
|
140
193
|
|
|
141
194
|
allEntries.forEach(function (item) {
|
|
142
195
|
var entryObject = makeEntryObject(item, options.itemEntryPoints);
|
|
143
196
|
|
|
144
|
-
Object.assign(item, walkMutate(entryObject,
|
|
197
|
+
Object.assign(item, walkMutate(entryObject, function (x) {
|
|
198
|
+
return isLink(x) || isResourceLink(x);
|
|
199
|
+
}, function (link) {
|
|
145
200
|
return normalizeLink(entityMap, link, options.removeUnresolved);
|
|
146
201
|
}, options.removeUnresolved));
|
|
147
202
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "contentful-resolve-response",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"node": ">=4.7.2"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"fast-copy": "^
|
|
28
|
+
"fast-copy": "^2.1.7"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@commitlint/cli": "^16.2.1",
|