@alchemy_cms/json_api 1.3.0 → 3.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 +7 -1
- package/dist/alchemy-json_api.js +22 -101
- package/dist/deserialize.js +41 -60
- package/package.json +8 -11
- package/dist/alchemy-json_api.mjs +0 -100
- package/dist/deserialize.mjs +0 -62
package/README.md
CHANGED
|
@@ -13,11 +13,13 @@ gem 'alchemy-json_api'
|
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
And then execute:
|
|
16
|
+
|
|
16
17
|
```bash
|
|
17
18
|
$ bundle
|
|
18
19
|
```
|
|
19
20
|
|
|
20
21
|
Or install it yourself as:
|
|
22
|
+
|
|
21
23
|
```bash
|
|
22
24
|
$ gem install alchemy-json_api
|
|
23
25
|
```
|
|
@@ -27,9 +29,11 @@ $ gem install alchemy-json_api
|
|
|
27
29
|
Run this in your application:
|
|
28
30
|
|
|
29
31
|
```
|
|
30
|
-
|
|
32
|
+
npm install @alchemy_cms/json_api --save
|
|
31
33
|
```
|
|
32
34
|
|
|
35
|
+
or with the package manager of your choice
|
|
36
|
+
|
|
33
37
|
## Usage
|
|
34
38
|
|
|
35
39
|
### In your Rails app
|
|
@@ -68,7 +72,9 @@ Alchemy::JsonApi.key_transform = :camel_lower
|
|
|
68
72
|
It defaults to `:underscore`.
|
|
69
73
|
|
|
70
74
|
## Contributing
|
|
75
|
+
|
|
71
76
|
Contribution directions go here.
|
|
72
77
|
|
|
73
78
|
## License
|
|
79
|
+
|
|
74
80
|
The gem is available as open source under the terms of the [BSD-3-Clause license](https://opensource.org/licenses/BSD-3-Clause).
|
package/dist/alchemy-json_api.js
CHANGED
|
@@ -1,104 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return parseJsonApiSimpleResourceData(data, included, false);
|
|
11
|
-
});
|
|
12
|
-
} else {
|
|
13
|
-
return parseJsonApiSimpleResourceData(response.data, included, false);
|
|
14
|
-
}
|
|
1
|
+
import { deserialize as d } from "./deserialize.js";
|
|
2
|
+
function n(t) {
|
|
3
|
+
const s = [];
|
|
4
|
+
return t.forEach((e) => {
|
|
5
|
+
var r, i, c;
|
|
6
|
+
((r = e.nested_elements) == null ? void 0 : r.length) > 0 && (e.nested_elements = n(
|
|
7
|
+
e.nested_elements
|
|
8
|
+
)), ((i = e.nestedElements) == null ? void 0 : i.length) > 0 && (e.nestedElements = n(e.nestedElements)), ((c = e.essences) == null ? void 0 : c.length) > 0 && (e.essences = e.essences.filter((f) => !f.deprecated)), e.deprecated || s.push(e);
|
|
9
|
+
}), s;
|
|
15
10
|
}
|
|
16
|
-
function
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
if (!(data.type in included.cached)) {
|
|
21
|
-
included.cached[data.type] = {};
|
|
22
|
-
}
|
|
23
|
-
if (useCache && data.id in included.cached[data.type]) {
|
|
24
|
-
return included.cached[data.type][data.id];
|
|
25
|
-
}
|
|
26
|
-
const attributes = data.attributes || {};
|
|
27
|
-
const resource = attributes;
|
|
28
|
-
resource.id = data.id;
|
|
29
|
-
included.cached[data.type][data.id] = resource;
|
|
30
|
-
if (data.relationships) {
|
|
31
|
-
for (const relationName of Object.keys(data.relationships)) {
|
|
32
|
-
const relationRef = data.relationships[relationName];
|
|
33
|
-
if (Array.isArray(relationRef.data)) {
|
|
34
|
-
const items = [];
|
|
35
|
-
relationRef.data.forEach(relationData => {
|
|
36
|
-
const item = findJsonApiIncluded(included, relationData.type, relationData.id);
|
|
37
|
-
items.push(item);
|
|
38
|
-
});
|
|
39
|
-
resource[relationName] = items;
|
|
40
|
-
} else if (relationRef && relationRef.data) {
|
|
41
|
-
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id);
|
|
42
|
-
} else {
|
|
43
|
-
resource[relationName] = null;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return resource;
|
|
11
|
+
function o(t) {
|
|
12
|
+
const s = d(t);
|
|
13
|
+
return s.elements = n(s.elements), s;
|
|
48
14
|
}
|
|
49
|
-
function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
if (!found) {
|
|
57
|
-
found = {
|
|
58
|
-
id
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
return found;
|
|
15
|
+
function l(t) {
|
|
16
|
+
const s = d(t);
|
|
17
|
+
return s.forEach((e) => {
|
|
18
|
+
e.elements = n(e.elements);
|
|
19
|
+
}), s;
|
|
62
20
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (element.nested_elements?.length > 0) {
|
|
69
|
-
element.nested_elements = filterDeprecatedElements(element.nested_elements);
|
|
70
|
-
}
|
|
71
|
-
if (element.nestedElements?.length > 0) {
|
|
72
|
-
element.nestedElements = filterDeprecatedElements(element.nestedElements);
|
|
73
|
-
}
|
|
74
|
-
if (element.essences?.length > 0) {
|
|
75
|
-
element.essences = element.essences.filter(essence => {
|
|
76
|
-
return !essence.deprecated;
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
if (!element.deprecated) {
|
|
80
|
-
els.push(element);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
return els;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Returns deserialized page without deprecated content
|
|
87
|
-
function deserializePage(pageData) {
|
|
88
|
-
const page = deserialize(pageData);
|
|
89
|
-
page.elements = filterDeprecatedElements(page.elements);
|
|
90
|
-
return page;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Returns deserialized pages without deprecated content
|
|
94
|
-
function deserializePages(pagesData) {
|
|
95
|
-
const pages = deserialize(pagesData);
|
|
96
|
-
pages.forEach(page => {
|
|
97
|
-
page.elements = filterDeprecatedElements(page.elements);
|
|
98
|
-
});
|
|
99
|
-
return pages;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
exports.deserialize = deserialize;
|
|
103
|
-
exports.deserializePage = deserializePage;
|
|
104
|
-
exports.deserializePages = deserializePages;
|
|
21
|
+
export {
|
|
22
|
+
d as deserialize,
|
|
23
|
+
o as deserializePage,
|
|
24
|
+
l as deserializePages
|
|
25
|
+
};
|
package/dist/deserialize.js
CHANGED
|
@@ -1,64 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
});
|
|
12
|
-
} else {
|
|
13
|
-
return parseJsonApiSimpleResourceData(response.data, included, false);
|
|
14
|
-
}
|
|
1
|
+
import y from "@ungap/structured-clone";
|
|
2
|
+
function A(e, r = {}) {
|
|
3
|
+
const s = y(e);
|
|
4
|
+
r || (r = {});
|
|
5
|
+
const n = s.included || [];
|
|
6
|
+
return Array.isArray(s.data) ? s.data.map((i) => f(i, n, !1)) : f(
|
|
7
|
+
s.data,
|
|
8
|
+
n,
|
|
9
|
+
!1
|
|
10
|
+
);
|
|
15
11
|
}
|
|
16
|
-
function
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
});
|
|
39
|
-
resource[relationName] = items;
|
|
40
|
-
} else if (relationRef && relationRef.data) {
|
|
41
|
-
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id);
|
|
42
|
-
} else {
|
|
43
|
-
resource[relationName] = null;
|
|
44
|
-
}
|
|
12
|
+
function f(e, r, s, n) {
|
|
13
|
+
if (r.cached || (r.cached = {}), e.type in r.cached || (r.cached[e.type] = {}), s && e.id in r.cached[e.type])
|
|
14
|
+
return r.cached[e.type][e.id];
|
|
15
|
+
const t = e.attributes || {};
|
|
16
|
+
if (t.id = e.id, r.cached[e.type][e.id] = t, e.relationships)
|
|
17
|
+
for (const c of Object.keys(e.relationships)) {
|
|
18
|
+
const o = e.relationships[c];
|
|
19
|
+
if (Array.isArray(o.data)) {
|
|
20
|
+
const p = [];
|
|
21
|
+
o.data.forEach((a) => {
|
|
22
|
+
const h = u(
|
|
23
|
+
r,
|
|
24
|
+
a.type,
|
|
25
|
+
a.id
|
|
26
|
+
);
|
|
27
|
+
p.push(h);
|
|
28
|
+
}), t[c] = p;
|
|
29
|
+
} else o && o.data ? t[c] = u(
|
|
30
|
+
r,
|
|
31
|
+
o.data.type,
|
|
32
|
+
o.data.id
|
|
33
|
+
) : t[c] = null;
|
|
45
34
|
}
|
|
46
|
-
|
|
47
|
-
return resource;
|
|
35
|
+
return t;
|
|
48
36
|
}
|
|
49
|
-
function
|
|
50
|
-
let
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
if (!found) {
|
|
57
|
-
found = {
|
|
58
|
-
id
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
return found;
|
|
37
|
+
function u(e, r, s, n) {
|
|
38
|
+
let i = null;
|
|
39
|
+
return e.forEach((t) => {
|
|
40
|
+
t.type === r && t.id === s && (i = f(t, e, !0));
|
|
41
|
+
}), i || (i = { id: s }), i;
|
|
62
42
|
}
|
|
63
|
-
|
|
64
|
-
|
|
43
|
+
export {
|
|
44
|
+
A as deserialize
|
|
45
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alchemy_cms/json_api",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "JSON API deserializer for AlchemyCMS",
|
|
5
5
|
"license": "BSD-3-Clause",
|
|
6
6
|
"repository": {
|
|
@@ -14,11 +14,12 @@
|
|
|
14
14
|
"headless"
|
|
15
15
|
],
|
|
16
16
|
"authors": [
|
|
17
|
+
"Lasse Diercks",
|
|
17
18
|
"Martin Meyerhoff",
|
|
18
19
|
"Thomas von Deyen"
|
|
19
20
|
],
|
|
20
21
|
"main": "dist/alchemy-json_api.js",
|
|
21
|
-
"
|
|
22
|
+
"type": "module",
|
|
22
23
|
"files": [
|
|
23
24
|
"dist"
|
|
24
25
|
],
|
|
@@ -26,18 +27,14 @@
|
|
|
26
27
|
"@ungap/structured-clone": "^1.0.1"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"@babel/core": "^7.19.0",
|
|
30
|
-
"@babel/preset-env": "^7.19.0",
|
|
31
|
-
"@rollup/plugin-babel": "^6.0.4",
|
|
32
|
-
"babel-jest": "^29.0.2",
|
|
33
|
-
"jest": "^29.0.2",
|
|
34
30
|
"prettier": "^3.3.3",
|
|
35
|
-
"
|
|
31
|
+
"vite": "^6.3.5",
|
|
32
|
+
"vitest": "^3.2.4"
|
|
36
33
|
},
|
|
37
34
|
"scripts": {
|
|
38
|
-
"build": "
|
|
35
|
+
"build": "vite build",
|
|
39
36
|
"lint": "prettier --check 'src/**/*.js'",
|
|
40
|
-
"test": "
|
|
37
|
+
"test": "vitest"
|
|
41
38
|
},
|
|
42
|
-
"
|
|
39
|
+
"packageManager": "pnpm@10.12.1"
|
|
43
40
|
}
|
|
@@ -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 };
|