Skip to content

Commit

Permalink
Merge pull request #28 from teleporthq/task/update-strapi
Browse files Browse the repository at this point in the history
Update strapi mappers for mixed type support
  • Loading branch information
ionutpasca authored Jul 29, 2024
2 parents 910c1ce + 7cf2edf commit ec49b5a
Showing 1 changed file with 45 additions and 25 deletions.
70 changes: 45 additions & 25 deletions src/strapi/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
function toPlainObject(obj) {
type Pagination = {
total?: number
limit?: number
start?: number
pages?: number
page?: number
hasNextPage?: boolean
hasPrevPage?: boolean
}

type Meta = {
pagination?: Pagination
}

type ContentData = {

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

View workflow job for this annotation

GitHub Actions / publish

'ContentData' is defined but never used
id?: string
attributes?: Record<string, unknown>
data?: ContentData | ContentData[]
meta?: Meta
}

type NormalizedContent = {
meta: {
pagination?: Pagination
}
data: unknown[] | unknown
}

const toPlainObject = (obj: any): any => {

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

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type

Check warning on line 29 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 = {}
const plainObj: Record<string, unknown> = {}

for (const key in obj) {
plainObj[key] = toPlainObject(obj[key])
Expand All @@ -12,8 +40,10 @@ function toPlainObject(obj) {
return plainObj
}

const normalizeNestedAttributes = (attributes) => {
const output = {}
export const normalizeNestedAttributes = (
attributes: Record<string, any>

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

View workflow job for this annotation

GitHub Actions / publish

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

for (const key in attributes) {
const value = attributes[key]
Expand All @@ -28,6 +58,10 @@ const normalizeNestedAttributes = (attributes) => {
) {
const normalizedValue = normalizeContent(value)
output[key] = { id: value.data.id, ...normalizedValue }
} else if (Array.isArray(value)) {
output[key] = value.map((el: any) => {

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

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
return normalizeNestedAttributes(el)
})
} else {
output[key] = toPlainObject(value)
}
Expand All @@ -36,7 +70,7 @@ const normalizeNestedAttributes = (attributes) => {
return output
}

export const normalizeContent = (input) => {
export const normalizeContent = (input: any): any => {

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

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type

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

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
if (
input === null ||
input === undefined ||
Expand Down Expand Up @@ -68,22 +102,7 @@ export const normalizeContent = (input) => {
return output
}

export const normalize = (
content
): {
meta: {
pagination?: {
total?: number
limit?: number
start?: number
pages: number
page: number
hasNextPage: boolean
hasPrevPage: boolean
}
}
data: Array<unknown> | unknown
} => {
export const normalize = (content: any): NormalizedContent => {

Check warning on line 105 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 @@ -95,22 +114,23 @@ export const normalize = (
}

if (start && limit) {
page = start / limit + 1
page = Math.floor(start / limit) + 1
}

const hasNextPage = page < pages
const hasPrevPage = page >= 2

return {
meta: {
...content?.meta,
pagination: {
...content?.meta?.pagination,
...(!!pages && { pages }),
...(!!page && { page }),
...(pages && { pages }),
...(page && { page }),
hasNextPage,
hasPrevPage,
},
},
...normalizeContent(content),
data: normalizeContent(content.data),
}
}

0 comments on commit ec49b5a

Please sign in to comment.