|
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; |
21 | 10 | } |
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; |
72 | 14 | } |
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; |
88 | 20 | } |
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 | +}; |
0 commit comments