@alchemy_cms/json_api 1.2.0 → 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/alchemy-json_api.js +57 -35
- package/dist/deserialize.js +45 -25
- package/package.json +6 -5
- package/dist/alchemy-json_api.mjs +0 -100
- package/dist/deserialize.mjs +0 -62
package/dist/alchemy-json_api.js
CHANGED
|
@@ -1,110 +1,132 @@
|
|
|
1
|
-
|
|
1
|
+
import structuredClone from '@ungap/structured-clone';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var structuredClone__default = /*#__PURE__*/_interopDefaultLegacy(structuredClone);
|
|
3
|
+
function deserialize(originalResponse, options = {}) {
|
|
4
|
+
const response = structuredClone(originalResponse);
|
|
5
|
+
if (!options) {
|
|
6
|
+
options = {};
|
|
7
|
+
}
|
|
10
8
|
|
|
11
|
-
function deserialize(originalResponse) {
|
|
12
|
-
const response = structuredClone__default["default"](originalResponse);
|
|
13
9
|
const included = response.included || [];
|
|
10
|
+
|
|
14
11
|
if (Array.isArray(response.data)) {
|
|
15
|
-
return response.data.map(data => {
|
|
16
|
-
return parseJsonApiSimpleResourceData(data, included, false)
|
|
17
|
-
})
|
|
12
|
+
return response.data.map((data) => {
|
|
13
|
+
return parseJsonApiSimpleResourceData(data, included, false)
|
|
14
|
+
})
|
|
18
15
|
} else {
|
|
19
|
-
return parseJsonApiSimpleResourceData(
|
|
16
|
+
return parseJsonApiSimpleResourceData(
|
|
17
|
+
response.data,
|
|
18
|
+
included,
|
|
19
|
+
false)
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
+
|
|
22
23
|
function parseJsonApiSimpleResourceData(data, included, useCache, options) {
|
|
23
24
|
if (!included.cached) {
|
|
24
25
|
included.cached = {};
|
|
25
26
|
}
|
|
27
|
+
|
|
26
28
|
if (!(data.type in included.cached)) {
|
|
27
29
|
included.cached[data.type] = {};
|
|
28
30
|
}
|
|
31
|
+
|
|
29
32
|
if (useCache && data.id in included.cached[data.type]) {
|
|
30
|
-
return included.cached[data.type][data.id]
|
|
33
|
+
return included.cached[data.type][data.id]
|
|
31
34
|
}
|
|
35
|
+
|
|
32
36
|
const attributes = data.attributes || {};
|
|
37
|
+
|
|
33
38
|
const resource = attributes;
|
|
34
39
|
resource.id = data.id;
|
|
40
|
+
|
|
35
41
|
included.cached[data.type][data.id] = resource;
|
|
42
|
+
|
|
36
43
|
if (data.relationships) {
|
|
37
44
|
for (const relationName of Object.keys(data.relationships)) {
|
|
38
45
|
const relationRef = data.relationships[relationName];
|
|
46
|
+
|
|
39
47
|
if (Array.isArray(relationRef.data)) {
|
|
40
48
|
const items = [];
|
|
41
|
-
|
|
42
|
-
|
|
49
|
+
|
|
50
|
+
relationRef.data.forEach((relationData) => {
|
|
51
|
+
const item = findJsonApiIncluded(
|
|
52
|
+
included,
|
|
53
|
+
relationData.type,
|
|
54
|
+
relationData.id);
|
|
55
|
+
|
|
43
56
|
items.push(item);
|
|
44
57
|
});
|
|
58
|
+
|
|
45
59
|
resource[relationName] = items;
|
|
46
60
|
} else if (relationRef && relationRef.data) {
|
|
47
|
-
resource[relationName] = findJsonApiIncluded(
|
|
61
|
+
resource[relationName] = findJsonApiIncluded(
|
|
62
|
+
included,
|
|
63
|
+
relationRef.data.type,
|
|
64
|
+
relationRef.data.id);
|
|
48
65
|
} else {
|
|
49
66
|
resource[relationName] = null;
|
|
50
67
|
}
|
|
51
68
|
}
|
|
52
69
|
}
|
|
53
|
-
|
|
70
|
+
|
|
71
|
+
return resource
|
|
54
72
|
}
|
|
73
|
+
|
|
55
74
|
function findJsonApiIncluded(included, type, id, options) {
|
|
56
75
|
let found = null;
|
|
57
|
-
|
|
76
|
+
|
|
77
|
+
included.forEach((item) => {
|
|
58
78
|
if (item.type === type && item.id === id) {
|
|
59
79
|
found = parseJsonApiSimpleResourceData(item, included, true);
|
|
60
80
|
}
|
|
61
81
|
});
|
|
82
|
+
|
|
62
83
|
if (!found) {
|
|
63
|
-
found = {
|
|
64
|
-
id
|
|
65
|
-
};
|
|
84
|
+
found = { id };
|
|
66
85
|
}
|
|
67
|
-
|
|
86
|
+
|
|
87
|
+
return found
|
|
68
88
|
}
|
|
69
89
|
|
|
70
90
|
// Recursively filters all deprecated elements and essences from collection
|
|
71
91
|
function filterDeprecatedElements(elements) {
|
|
72
92
|
const els = [];
|
|
73
|
-
|
|
93
|
+
|
|
94
|
+
elements.forEach((element) => {
|
|
74
95
|
if (element.nested_elements?.length > 0) {
|
|
75
|
-
element.nested_elements = filterDeprecatedElements(
|
|
96
|
+
element.nested_elements = filterDeprecatedElements(
|
|
97
|
+
element.nested_elements
|
|
98
|
+
);
|
|
76
99
|
}
|
|
77
100
|
if (element.nestedElements?.length > 0) {
|
|
78
101
|
element.nestedElements = filterDeprecatedElements(element.nestedElements);
|
|
79
102
|
}
|
|
80
103
|
if (element.essences?.length > 0) {
|
|
81
|
-
element.essences = element.essences.filter(essence => {
|
|
82
|
-
return !essence.deprecated
|
|
104
|
+
element.essences = element.essences.filter((essence) => {
|
|
105
|
+
return !essence.deprecated
|
|
83
106
|
});
|
|
84
107
|
}
|
|
85
108
|
if (!element.deprecated) {
|
|
86
109
|
els.push(element);
|
|
87
110
|
}
|
|
88
111
|
});
|
|
89
|
-
|
|
112
|
+
|
|
113
|
+
return els
|
|
90
114
|
}
|
|
91
115
|
|
|
92
116
|
// Returns deserialized page without deprecated content
|
|
93
117
|
function deserializePage(pageData) {
|
|
94
118
|
const page = deserialize(pageData);
|
|
95
119
|
page.elements = filterDeprecatedElements(page.elements);
|
|
96
|
-
return page
|
|
120
|
+
return page
|
|
97
121
|
}
|
|
98
122
|
|
|
99
123
|
// Returns deserialized pages without deprecated content
|
|
100
124
|
function deserializePages(pagesData) {
|
|
101
125
|
const pages = deserialize(pagesData);
|
|
102
|
-
pages.forEach(page => {
|
|
126
|
+
pages.forEach((page) => {
|
|
103
127
|
page.elements = filterDeprecatedElements(page.elements);
|
|
104
128
|
});
|
|
105
|
-
return pages
|
|
129
|
+
return pages
|
|
106
130
|
}
|
|
107
131
|
|
|
108
|
-
|
|
109
|
-
exports.deserializePage = deserializePage;
|
|
110
|
-
exports.deserializePages = deserializePages;
|
|
132
|
+
export { deserialize, deserializePage, deserializePages };
|
package/dist/deserialize.js
CHANGED
|
@@ -1,70 +1,90 @@
|
|
|
1
|
-
|
|
1
|
+
import structuredClone from '@ungap/structured-clone';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var structuredClone__default = /*#__PURE__*/_interopDefaultLegacy(structuredClone);
|
|
3
|
+
function deserialize(originalResponse, options = {}) {
|
|
4
|
+
const response = structuredClone(originalResponse);
|
|
5
|
+
if (!options) {
|
|
6
|
+
options = {};
|
|
7
|
+
}
|
|
10
8
|
|
|
11
|
-
function deserialize(originalResponse) {
|
|
12
|
-
const response = structuredClone__default["default"](originalResponse);
|
|
13
9
|
const included = response.included || [];
|
|
10
|
+
|
|
14
11
|
if (Array.isArray(response.data)) {
|
|
15
|
-
return response.data.map(data => {
|
|
16
|
-
return parseJsonApiSimpleResourceData(data, included, false)
|
|
17
|
-
})
|
|
12
|
+
return response.data.map((data) => {
|
|
13
|
+
return parseJsonApiSimpleResourceData(data, included, false)
|
|
14
|
+
})
|
|
18
15
|
} else {
|
|
19
|
-
return parseJsonApiSimpleResourceData(
|
|
16
|
+
return parseJsonApiSimpleResourceData(
|
|
17
|
+
response.data,
|
|
18
|
+
included,
|
|
19
|
+
false)
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
+
|
|
22
23
|
function parseJsonApiSimpleResourceData(data, included, useCache, options) {
|
|
23
24
|
if (!included.cached) {
|
|
24
25
|
included.cached = {};
|
|
25
26
|
}
|
|
27
|
+
|
|
26
28
|
if (!(data.type in included.cached)) {
|
|
27
29
|
included.cached[data.type] = {};
|
|
28
30
|
}
|
|
31
|
+
|
|
29
32
|
if (useCache && data.id in included.cached[data.type]) {
|
|
30
|
-
return included.cached[data.type][data.id]
|
|
33
|
+
return included.cached[data.type][data.id]
|
|
31
34
|
}
|
|
35
|
+
|
|
32
36
|
const attributes = data.attributes || {};
|
|
37
|
+
|
|
33
38
|
const resource = attributes;
|
|
34
39
|
resource.id = data.id;
|
|
40
|
+
|
|
35
41
|
included.cached[data.type][data.id] = resource;
|
|
42
|
+
|
|
36
43
|
if (data.relationships) {
|
|
37
44
|
for (const relationName of Object.keys(data.relationships)) {
|
|
38
45
|
const relationRef = data.relationships[relationName];
|
|
46
|
+
|
|
39
47
|
if (Array.isArray(relationRef.data)) {
|
|
40
48
|
const items = [];
|
|
41
|
-
|
|
42
|
-
|
|
49
|
+
|
|
50
|
+
relationRef.data.forEach((relationData) => {
|
|
51
|
+
const item = findJsonApiIncluded(
|
|
52
|
+
included,
|
|
53
|
+
relationData.type,
|
|
54
|
+
relationData.id);
|
|
55
|
+
|
|
43
56
|
items.push(item);
|
|
44
57
|
});
|
|
58
|
+
|
|
45
59
|
resource[relationName] = items;
|
|
46
60
|
} else if (relationRef && relationRef.data) {
|
|
47
|
-
resource[relationName] = findJsonApiIncluded(
|
|
61
|
+
resource[relationName] = findJsonApiIncluded(
|
|
62
|
+
included,
|
|
63
|
+
relationRef.data.type,
|
|
64
|
+
relationRef.data.id);
|
|
48
65
|
} else {
|
|
49
66
|
resource[relationName] = null;
|
|
50
67
|
}
|
|
51
68
|
}
|
|
52
69
|
}
|
|
53
|
-
|
|
70
|
+
|
|
71
|
+
return resource
|
|
54
72
|
}
|
|
73
|
+
|
|
55
74
|
function findJsonApiIncluded(included, type, id, options) {
|
|
56
75
|
let found = null;
|
|
57
|
-
|
|
76
|
+
|
|
77
|
+
included.forEach((item) => {
|
|
58
78
|
if (item.type === type && item.id === id) {
|
|
59
79
|
found = parseJsonApiSimpleResourceData(item, included, true);
|
|
60
80
|
}
|
|
61
81
|
});
|
|
82
|
+
|
|
62
83
|
if (!found) {
|
|
63
|
-
found = {
|
|
64
|
-
id
|
|
65
|
-
};
|
|
84
|
+
found = { id };
|
|
66
85
|
}
|
|
67
|
-
|
|
86
|
+
|
|
87
|
+
return found
|
|
68
88
|
}
|
|
69
89
|
|
|
70
|
-
|
|
90
|
+
export { deserialize };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alchemy_cms/json_api",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "JSON API deserializer for AlchemyCMS",
|
|
5
5
|
"license": "BSD-3-Clause",
|
|
6
6
|
"repository": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"Thomas von Deyen"
|
|
19
19
|
],
|
|
20
20
|
"main": "dist/alchemy-json_api.js",
|
|
21
|
-
"
|
|
21
|
+
"type": "module",
|
|
22
22
|
"files": [
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
@@ -28,14 +28,15 @@
|
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@babel/core": "^7.19.0",
|
|
30
30
|
"@babel/preset-env": "^7.19.0",
|
|
31
|
-
"@rollup/plugin-babel": "^
|
|
31
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
32
32
|
"babel-jest": "^29.0.2",
|
|
33
33
|
"jest": "^29.0.2",
|
|
34
|
-
"prettier": "^
|
|
35
|
-
"rollup": "^
|
|
34
|
+
"prettier": "^3.3.3",
|
|
35
|
+
"rollup": "^4.24.4"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "rollup --config",
|
|
39
|
+
"lint": "prettier --check 'src/**/*.js'",
|
|
39
40
|
"test": "jest"
|
|
40
41
|
},
|
|
41
42
|
"browserslist": "> 0.25%, not dead"
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import structuredClone from '@ungap/structured-clone';
|
|
2
|
-
|
|
3
|
-
function deserialize(originalResponse) {
|
|
4
|
-
const response = structuredClone(originalResponse);
|
|
5
|
-
const included = response.included || [];
|
|
6
|
-
if (Array.isArray(response.data)) {
|
|
7
|
-
return response.data.map(data => {
|
|
8
|
-
return parseJsonApiSimpleResourceData(data, included, false);
|
|
9
|
-
});
|
|
10
|
-
} else {
|
|
11
|
-
return parseJsonApiSimpleResourceData(response.data, included, false);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function parseJsonApiSimpleResourceData(data, included, useCache, options) {
|
|
15
|
-
if (!included.cached) {
|
|
16
|
-
included.cached = {};
|
|
17
|
-
}
|
|
18
|
-
if (!(data.type in included.cached)) {
|
|
19
|
-
included.cached[data.type] = {};
|
|
20
|
-
}
|
|
21
|
-
if (useCache && data.id in included.cached[data.type]) {
|
|
22
|
-
return included.cached[data.type][data.id];
|
|
23
|
-
}
|
|
24
|
-
const attributes = data.attributes || {};
|
|
25
|
-
const resource = attributes;
|
|
26
|
-
resource.id = data.id;
|
|
27
|
-
included.cached[data.type][data.id] = resource;
|
|
28
|
-
if (data.relationships) {
|
|
29
|
-
for (const relationName of Object.keys(data.relationships)) {
|
|
30
|
-
const relationRef = data.relationships[relationName];
|
|
31
|
-
if (Array.isArray(relationRef.data)) {
|
|
32
|
-
const items = [];
|
|
33
|
-
relationRef.data.forEach(relationData => {
|
|
34
|
-
const item = findJsonApiIncluded(included, relationData.type, relationData.id);
|
|
35
|
-
items.push(item);
|
|
36
|
-
});
|
|
37
|
-
resource[relationName] = items;
|
|
38
|
-
} else if (relationRef && relationRef.data) {
|
|
39
|
-
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id);
|
|
40
|
-
} else {
|
|
41
|
-
resource[relationName] = null;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return resource;
|
|
46
|
-
}
|
|
47
|
-
function findJsonApiIncluded(included, type, id, options) {
|
|
48
|
-
let found = null;
|
|
49
|
-
included.forEach(item => {
|
|
50
|
-
if (item.type === type && item.id === id) {
|
|
51
|
-
found = parseJsonApiSimpleResourceData(item, included, true);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
if (!found) {
|
|
55
|
-
found = {
|
|
56
|
-
id
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
return found;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Recursively filters all deprecated elements and essences from collection
|
|
63
|
-
function filterDeprecatedElements(elements) {
|
|
64
|
-
const els = [];
|
|
65
|
-
elements.forEach(element => {
|
|
66
|
-
if (element.nested_elements?.length > 0) {
|
|
67
|
-
element.nested_elements = filterDeprecatedElements(element.nested_elements);
|
|
68
|
-
}
|
|
69
|
-
if (element.nestedElements?.length > 0) {
|
|
70
|
-
element.nestedElements = filterDeprecatedElements(element.nestedElements);
|
|
71
|
-
}
|
|
72
|
-
if (element.essences?.length > 0) {
|
|
73
|
-
element.essences = element.essences.filter(essence => {
|
|
74
|
-
return !essence.deprecated;
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
if (!element.deprecated) {
|
|
78
|
-
els.push(element);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
return els;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Returns deserialized page without deprecated content
|
|
85
|
-
function deserializePage(pageData) {
|
|
86
|
-
const page = deserialize(pageData);
|
|
87
|
-
page.elements = filterDeprecatedElements(page.elements);
|
|
88
|
-
return page;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Returns deserialized pages without deprecated content
|
|
92
|
-
function deserializePages(pagesData) {
|
|
93
|
-
const pages = deserialize(pagesData);
|
|
94
|
-
pages.forEach(page => {
|
|
95
|
-
page.elements = filterDeprecatedElements(page.elements);
|
|
96
|
-
});
|
|
97
|
-
return pages;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export { deserialize, deserializePage, deserializePages };
|
package/dist/deserialize.mjs
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import structuredClone from '@ungap/structured-clone';
|
|
2
|
-
|
|
3
|
-
function deserialize(originalResponse) {
|
|
4
|
-
const response = structuredClone(originalResponse);
|
|
5
|
-
const included = response.included || [];
|
|
6
|
-
if (Array.isArray(response.data)) {
|
|
7
|
-
return response.data.map(data => {
|
|
8
|
-
return parseJsonApiSimpleResourceData(data, included, false);
|
|
9
|
-
});
|
|
10
|
-
} else {
|
|
11
|
-
return parseJsonApiSimpleResourceData(response.data, included, false);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function parseJsonApiSimpleResourceData(data, included, useCache, options) {
|
|
15
|
-
if (!included.cached) {
|
|
16
|
-
included.cached = {};
|
|
17
|
-
}
|
|
18
|
-
if (!(data.type in included.cached)) {
|
|
19
|
-
included.cached[data.type] = {};
|
|
20
|
-
}
|
|
21
|
-
if (useCache && data.id in included.cached[data.type]) {
|
|
22
|
-
return included.cached[data.type][data.id];
|
|
23
|
-
}
|
|
24
|
-
const attributes = data.attributes || {};
|
|
25
|
-
const resource = attributes;
|
|
26
|
-
resource.id = data.id;
|
|
27
|
-
included.cached[data.type][data.id] = resource;
|
|
28
|
-
if (data.relationships) {
|
|
29
|
-
for (const relationName of Object.keys(data.relationships)) {
|
|
30
|
-
const relationRef = data.relationships[relationName];
|
|
31
|
-
if (Array.isArray(relationRef.data)) {
|
|
32
|
-
const items = [];
|
|
33
|
-
relationRef.data.forEach(relationData => {
|
|
34
|
-
const item = findJsonApiIncluded(included, relationData.type, relationData.id);
|
|
35
|
-
items.push(item);
|
|
36
|
-
});
|
|
37
|
-
resource[relationName] = items;
|
|
38
|
-
} else if (relationRef && relationRef.data) {
|
|
39
|
-
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id);
|
|
40
|
-
} else {
|
|
41
|
-
resource[relationName] = null;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return resource;
|
|
46
|
-
}
|
|
47
|
-
function findJsonApiIncluded(included, type, id, options) {
|
|
48
|
-
let found = null;
|
|
49
|
-
included.forEach(item => {
|
|
50
|
-
if (item.type === type && item.id === id) {
|
|
51
|
-
found = parseJsonApiSimpleResourceData(item, included, true);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
if (!found) {
|
|
55
|
-
found = {
|
|
56
|
-
id
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
return found;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export { deserialize };
|