Skip to content

Commit

Permalink
Merge pull request #30 from teleporthq/fix/strapi-relative-url
Browse files Browse the repository at this point in the history
Fix/strapi relative url
  • Loading branch information
mihaisampaleanu authored Sep 9, 2024
2 parents d0afd24 + 12202ba commit c8e3bc3
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 47 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.

64 changes: 35 additions & 29 deletions dist/strapi.mjs
Original file line number Diff line number Diff line change
@@ -1,55 +1,61 @@
const s = (t) => {
const d = (t, n) => {
if (typeof t != "object" || t === null)
return t;
const a = {};
for (const r in t)
a[r] = s(t[r]);
return a;
}, d = (t) => {
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 r in t) {
const e = t[r];
if (typeof e == "object" && e !== null && "data" in e && e.data !== null && "id" in e.data && "attributes" in e.data) {
const i = l(e);
a[r] = { id: e.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(e) ? a[r] = e.map((i) => d(i)) : a[r] = s(e);
Array.isArray(i) ? a[e] = i.map((l) => u(l, n)) : a[e] = d(i, n);
}
return a;
}, l = (t) => {
}, r = (t, n) => {
if (Array.isArray(t))
return {
data: t.map(l)
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)
}, delete a.attributes), t.data && (a = l(t.data)), a;
}, m = (t) => {
const a = t?.meta?.pagination?.total, r = t?.meta?.pagination?.limit, e = t?.meta?.pagination?.start;
let i = 0, n = 1;
a && r && (i = Math.ceil(a / r)), e && r && (n = Math.floor(e / r) + 1);
const c = n < i, f = n >= 2;
let o = l(t.data);
return o.data && (o = o.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 },
...n && { page: n },
hasNextPage: c,
hasPrevPage: f
...l && { pages: l },
...o && { page: o },
hasNextPage: f,
hasPrevPage: c
}
},
data: o
data: s
};
};
export {
m as normalize,
l as normalizeContent,
d as normalizeNestedAttributes
r as normalizeContent,
u as normalizeNestedAttributes
};
6 changes: 3 additions & 3 deletions dist/strapi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type NormalizedContent = {
};
data: unknown[] | unknown;
};
export declare const normalizeNestedAttributes: (attributes: Record<string, any>) => Record<string, unknown>;
export declare const normalizeContent: (input: any) => any;
export declare const normalize: (content: any) => NormalizedContent;
export declare const normalizeNestedAttributes: (attributes: Record<string, any>, strapiUrl?: string) => Record<string, unknown>;
export declare const normalizeContent: (input: any, strapiUrl?: string) => any;
export declare const normalize: (content: any, strapiUrl?: string) => NormalizedContent;
export {};
//# sourceMappingURL=index.d.ts.map
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@teleporthq/cms-mappers",
"version": "1.0.15",
"version": "1.0.16",
"main": "dist/index.js",
"module": "dist/contentful.mjs",
"types": "dist/contentful/index.d.ts",
Expand Down
52 changes: 40 additions & 12 deletions src/strapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,42 @@ type NormalizedContent = {
data: unknown[] | unknown
}

const toPlainObject = (obj: any): any => {
const toPlainObject = (obj: any, strapiUrl?: string): any => {

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

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type

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

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
if (typeof obj !== 'object' || obj === null) {
return obj
}

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('/')) {
if (!strapiUrl) {
strapiUrl = process.env.CMS_URL
}
plainObj['url'] = `${strapiUrl}${plainObj['url']}`
}

return plainObj
}

export const normalizeNestedAttributes = (
attributes: Record<string, any>
attributes: Record<string, any>,

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

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
strapiUrl?: string
): Record<string, unknown> => {
const output: Record<string, unknown> = {}

Expand All @@ -45,24 +65,25 @@ export const normalizeNestedAttributes = (
'id' in value.data &&
'attributes' in value.data
) {
const normalizedValue = normalizeContent(value)
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 / publish

Unexpected any. Specify a different type
return normalizeNestedAttributes(el)
return normalizeNestedAttributes(el, strapiUrl)
})
} else {
output[key] = toPlainObject(value)
output[key] = toPlainObject(value, strapiUrl)
}
}

return output
}

export const normalizeContent = (input: any): any => {
export const normalizeContent = (input: any, strapiUrl?: string): any => {

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

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type

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

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
if (Array.isArray(input)) {
return {
data: input.map(normalizeContent),
data: input.map((value) => normalizeContent(value, strapiUrl)),
}
}

Expand All @@ -79,19 +100,26 @@ export const normalizeContent = (input: any): any => {
if (input.attributes) {
output = {
...output,
...normalizeNestedAttributes(input.attributes),
...normalizeNestedAttributes(input.attributes, strapiUrl),
}
delete output.attributes
}

if (input.data) {
output = normalizeContent(input.data)
output = normalizeContent(input.data, strapiUrl)
}

if (output.url?.startsWith('/')) {
if (!strapiUrl) {
strapiUrl = process.env.CMS_URL
}
output.url = `${strapiUrl}${output.url}`
}

return output
}

export const normalize = (content: any): NormalizedContent => {
export const normalize = (content: any, strapiUrl?: string): NormalizedContent => {

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

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
const total = content?.meta?.pagination?.total
const limit = content?.meta?.pagination?.limit
const start = content?.meta?.pagination?.start
Expand All @@ -109,7 +137,7 @@ export const normalize = (content: any): NormalizedContent => {
const hasNextPage = page < pages
const hasPrevPage = page >= 2

let normalizedContent = normalizeContent(content.data)
let normalizedContent = normalizeContent(content.data, strapiUrl)
// We need to make sure that we do not have nested data.data
if (normalizedContent.data) {
normalizedContent = normalizedContent.data
Expand Down

0 comments on commit c8e3bc3

Please sign in to comment.