Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vladgrecu committed Sep 9, 2024
1 parent fa9e4f2 commit 12202ba
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
2 changes: 1 addition & 1 deletion dist/strapi.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 34 additions & 28 deletions dist/strapi.mjs
Original file line number Diff line number Diff line change
@@ -1,55 +1,61 @@
const f = (t, e) => {
const d = (t, n) => {
if (typeof t != "object" || t === null)
return t;
const a = {};
for (const n in t)
a[n] = f(t[n]);
return typeof a.url == "string" && a.url.startsWith("/") && (e || (e = process.env.CMS_URL), a.url = `${e}${a.url}`), a;
}, d = (t, e) => {
for (const e in t) {
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) {
const i = r(t[e], n);
a[e] = { id: t[e].data.id, ...i };
continue;
}
a[e] = d(t[e], n);
}
return typeof a.url == "string" && a.url.startsWith("/") && (n || (n = process.env.CMS_URL), a.url = `${n}${a.url}`), a;
}, u = (t, n) => {
const a = {};
for (const n in t) {
const o = t[n];
if (typeof o == "object" && o !== null && "data" in o && o.data !== null && "id" in o.data && "attributes" in o.data) {
const i = s(o, e);
a[n] = { id: o.data.id, ...i };
for (const e in t) {
const i = t[e];
if (typeof i == "object" && i !== null && "data" in i && i.data !== null && "id" in i.data && "attributes" in i.data) {
const l = r(i, n);
a[e] = { id: i.data.id, ...l };
} else
Array.isArray(o) ? a[n] = o.map((i) => d(i, e)) : a[n] = f(o, e);
Array.isArray(i) ? a[e] = i.map((l) => u(l, n)) : a[e] = d(i, n);
}
return a;
}, s = (t, e) => {
}, r = (t, n) => {
if (Array.isArray(t))
return {
data: t.map((n) => s(n, e))
data: t.map((e) => r(e, n))
};
if (t == null || typeof t == "object" && !Object.keys(t).length)
return null;
let a = { ...t };
return t.attributes && (a = {
...a,
...d(t.attributes, e)
}, 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;
}, m = (t, e) => {
const a = t?.meta?.pagination?.total, n = t?.meta?.pagination?.limit, o = t?.meta?.pagination?.start;
let i = 0, r = 1;
a && n && (i = Math.ceil(a / n)), o && n && (r = Math.floor(o / n) + 1);
const u = r < i, c = r >= 2;
let l = s(t.data, e);
return l.data && (l = l.data), {
...u(t.attributes, n)
}, 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;
}, m = (t, n) => {
const a = t?.meta?.pagination?.total, e = t?.meta?.pagination?.limit, i = t?.meta?.pagination?.start;
let l = 0, o = 1;
a && e && (l = Math.ceil(a / e)), i && e && (o = Math.floor(i / e) + 1);
const f = o < l, c = o >= 2;
let s = r(t.data, n);
return s.data && (s = s.data), {
meta: {
...t?.meta,
pagination: {
...t?.meta?.pagination,
...i && { pages: i },
...r && { page: r },
hasNextPage: u,
...l && { pages: l },
...o && { page: o },
hasNextPage: f,
hasPrevPage: c
}
},
data: l
data: s
};
};
export {
m as normalize,
s as normalizeContent,
d as normalizeNestedAttributes
r as normalizeContent,
u as normalizeNestedAttributes
};
2 changes: 1 addition & 1 deletion dist/strapi/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion src/strapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ const toPlainObject = (obj: any, strapiUrl?: string): any => {
const plainObj: Record<string, unknown> = {}

for (const key in obj) {
plainObj[key] = toPlainObject(obj[key])
if (
typeof obj[key] === 'object' &&
obj[key] !== null &&
'data' in obj[key] &&
obj[key].data !== null &&
'id' in obj[key].data &&
'attributes' in obj[key].data
) {
const normalizedValue = normalizeContent(obj[key], strapiUrl)
plainObj[key] = { id: obj[key].data.id, ...normalizedValue }
continue
}
plainObj[key] = toPlainObject(obj[key], strapiUrl)
}

if (typeof plainObj['url'] === 'string' && plainObj['url'].startsWith('/')) {
Expand Down Expand Up @@ -54,6 +66,7 @@ export const normalizeNestedAttributes = (
'attributes' in value.data
) {
const normalizedValue = normalizeContent(value, strapiUrl)

output[key] = { id: value.data.id, ...normalizedValue }
} else if (Array.isArray(value)) {
output[key] = value.map((el: any) => {

Check warning on line 72 in src/strapi/index.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
Expand Down

0 comments on commit 12202ba

Please sign in to comment.