Skip to content

Commit 12202ba

Browse files
committed
fixes
1 parent fa9e4f2 commit 12202ba

File tree

4 files changed

+50
-31
lines changed

4 files changed

+50
-31
lines changed

dist/strapi.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/strapi.mjs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,61 @@
1-
const f = (t, e) => {
1+
const d = (t, n) => {
22
if (typeof t != "object" || t === null)
33
return t;
44
const a = {};
5-
for (const n in t)
6-
a[n] = f(t[n]);
7-
return typeof a.url == "string" && a.url.startsWith("/") && (e || (e = process.env.CMS_URL), a.url = `${e}${a.url}`), a;
8-
}, d = (t, e) => {
5+
for (const e in t) {
6+
if (typeof t[e] == "object" && t[e] !== null && "data" in t[e] && t[e].data !== null && "id" in t[e].data && "attributes" in t[e].data) {
7+
const i = r(t[e], n);
8+
a[e] = { id: t[e].data.id, ...i };
9+
continue;
10+
}
11+
a[e] = d(t[e], n);
12+
}
13+
return typeof a.url == "string" && a.url.startsWith("/") && (n || (n = process.env.CMS_URL), a.url = `${n}${a.url}`), a;
14+
}, u = (t, n) => {
915
const a = {};
10-
for (const n in t) {
11-
const o = t[n];
12-
if (typeof o == "object" && o !== null && "data" in o && o.data !== null && "id" in o.data && "attributes" in o.data) {
13-
const i = s(o, e);
14-
a[n] = { id: o.data.id, ...i };
16+
for (const e in t) {
17+
const i = t[e];
18+
if (typeof i == "object" && i !== null && "data" in i && i.data !== null && "id" in i.data && "attributes" in i.data) {
19+
const l = r(i, n);
20+
a[e] = { id: i.data.id, ...l };
1521
} else
16-
Array.isArray(o) ? a[n] = o.map((i) => d(i, e)) : a[n] = f(o, e);
22+
Array.isArray(i) ? a[e] = i.map((l) => u(l, n)) : a[e] = d(i, n);
1723
}
1824
return a;
19-
}, s = (t, e) => {
25+
}, r = (t, n) => {
2026
if (Array.isArray(t))
2127
return {
22-
data: t.map((n) => s(n, e))
28+
data: t.map((e) => r(e, n))
2329
};
2430
if (t == null || typeof t == "object" && !Object.keys(t).length)
2531
return null;
2632
let a = { ...t };
2733
return t.attributes && (a = {
2834
...a,
29-
...d(t.attributes, e)
30-
}, delete a.attributes), t.data && (a = s(t.data, e)), a.url?.startsWith("/") && (e || (e = process.env.CMS_URL), a.url = `${e}${a.url}`), a;
31-
}, m = (t, e) => {
32-
const a = t?.meta?.pagination?.total, n = t?.meta?.pagination?.limit, o = t?.meta?.pagination?.start;
33-
let i = 0, r = 1;
34-
a && n && (i = Math.ceil(a / n)), o && n && (r = Math.floor(o / n) + 1);
35-
const u = r < i, c = r >= 2;
36-
let l = s(t.data, e);
37-
return l.data && (l = l.data), {
35+
...u(t.attributes, n)
36+
}, delete a.attributes), t.data && (a = r(t.data, n)), a.url?.startsWith("/") && (n || (n = process.env.CMS_URL), a.url = `${n}${a.url}`), a;
37+
}, m = (t, n) => {
38+
const a = t?.meta?.pagination?.total, e = t?.meta?.pagination?.limit, i = t?.meta?.pagination?.start;
39+
let l = 0, o = 1;
40+
a && e && (l = Math.ceil(a / e)), i && e && (o = Math.floor(i / e) + 1);
41+
const f = o < l, c = o >= 2;
42+
let s = r(t.data, n);
43+
return s.data && (s = s.data), {
3844
meta: {
3945
...t?.meta,
4046
pagination: {
4147
...t?.meta?.pagination,
42-
...i && { pages: i },
43-
...r && { page: r },
44-
hasNextPage: u,
48+
...l && { pages: l },
49+
...o && { page: o },
50+
hasNextPage: f,
4551
hasPrevPage: c
4652
}
4753
},
48-
data: l
54+
data: s
4955
};
5056
};
5157
export {
5258
m as normalize,
53-
s as normalizeContent,
54-
d as normalizeNestedAttributes
59+
r as normalizeContent,
60+
u as normalizeNestedAttributes
5561
};

dist/strapi/index.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/strapi/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,19 @@ const toPlainObject = (obj: any, strapiUrl?: string): any => {
2323
const plainObj: Record<string, unknown> = {}
2424

2525
for (const key in obj) {
26-
plainObj[key] = toPlainObject(obj[key])
26+
if (
27+
typeof obj[key] === 'object' &&
28+
obj[key] !== null &&
29+
'data' in obj[key] &&
30+
obj[key].data !== null &&
31+
'id' in obj[key].data &&
32+
'attributes' in obj[key].data
33+
) {
34+
const normalizedValue = normalizeContent(obj[key], strapiUrl)
35+
plainObj[key] = { id: obj[key].data.id, ...normalizedValue }
36+
continue
37+
}
38+
plainObj[key] = toPlainObject(obj[key], strapiUrl)
2739
}
2840

2941
if (typeof plainObj['url'] === 'string' && plainObj['url'].startsWith('/')) {
@@ -54,6 +66,7 @@ export const normalizeNestedAttributes = (
5466
'attributes' in value.data
5567
) {
5668
const normalizedValue = normalizeContent(value, strapiUrl)
69+
5770
output[key] = { id: value.data.id, ...normalizedValue }
5871
} else if (Array.isArray(value)) {
5972
output[key] = value.map((el: any) => {

0 commit comments

Comments
 (0)