Skip to content

Commit 5a10eab

Browse files
committed
Bump package to v3.0.0
1 parent 3bb9372 commit 5a10eab

File tree

3 files changed

+65
-216
lines changed

3 files changed

+65
-216
lines changed

dist/alchemy-json_api.js

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

dist/deserialize.js

Lines changed: 41 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,45 @@
1-
import structuredClone from '@ungap/structured-clone';
2-
3-
function deserialize(originalResponse, options = {}) {
4-
const response = structuredClone(originalResponse);
5-
if (!options) {
6-
options = {};
7-
}
8-
9-
const included = response.included || [];
10-
11-
if (Array.isArray(response.data)) {
12-
return response.data.map((data) => {
13-
return parseJsonApiSimpleResourceData(data, included, false)
14-
})
15-
} else {
16-
return parseJsonApiSimpleResourceData(
17-
response.data,
18-
included,
19-
false)
20-
}
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+
);
2111
}
22-
23-
function parseJsonApiSimpleResourceData(data, included, useCache, options) {
24-
if (!included.cached) {
25-
included.cached = {};
26-
}
27-
28-
if (!(data.type in included.cached)) {
29-
included.cached[data.type] = {};
30-
}
31-
32-
if (useCache && data.id in included.cached[data.type]) {
33-
return included.cached[data.type][data.id]
34-
}
35-
36-
const attributes = data.attributes || {};
37-
38-
const resource = attributes;
39-
resource.id = data.id;
40-
41-
included.cached[data.type][data.id] = resource;
42-
43-
if (data.relationships) {
44-
for (const relationName of Object.keys(data.relationships)) {
45-
const relationRef = data.relationships[relationName];
46-
47-
if (Array.isArray(relationRef.data)) {
48-
const items = [];
49-
50-
relationRef.data.forEach((relationData) => {
51-
const item = findJsonApiIncluded(
52-
included,
53-
relationData.type,
54-
relationData.id);
55-
56-
items.push(item);
57-
});
58-
59-
resource[relationName] = items;
60-
} else if (relationRef && relationRef.data) {
61-
resource[relationName] = findJsonApiIncluded(
62-
included,
63-
relationRef.data.type,
64-
relationRef.data.id);
65-
} else {
66-
resource[relationName] = null;
67-
}
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;
6834
}
69-
}
70-
71-
return resource
35+
return t;
7236
}
73-
74-
function findJsonApiIncluded(included, type, id, options) {
75-
let found = null;
76-
77-
included.forEach((item) => {
78-
if (item.type === type && item.id === id) {
79-
found = parseJsonApiSimpleResourceData(item, included, true);
80-
}
81-
});
82-
83-
if (!found) {
84-
found = { id };
85-
}
86-
87-
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;
8842
}
89-
90-
export { deserialize };
43+
export {
44+
A as deserialize
45+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alchemy_cms/json_api",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"description": "JSON API deserializer for AlchemyCMS",
55
"license": "BSD-3-Clause",
66
"repository": {
@@ -14,6 +14,7 @@
1414
"headless"
1515
],
1616
"authors": [
17+
"Lasse Diercks",
1718
"Martin Meyerhoff",
1819
"Thomas von Deyen"
1920
],

0 commit comments

Comments
 (0)