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