4
0
forked from crowetic/commerce
2021-01-11 15:03:04 -03:00

57 lines
1.2 KiB
TypeScript

import { Product as BCProduct } from '@framework/schema'
export function normalizeProduct(productNode: BCProduct): Product {
const {
entityId: id,
images,
variants,
productOptions,
prices,
path,
id: _,
options: _0,
...rest
} = productNode
return {
id,
path,
slug: path?.replace(/^\/+|\/+$/g, ''),
images: images.edges
? images.edges.map(
({ node: { urlOriginal, altText, ...rest } }: any) => ({
url: urlOriginal,
alt: altText,
...rest,
})
)
: [],
variants: variants.edges
? variants.edges.map(({ node: { entityId, ...rest } }: any) => ({
id: entityId,
...rest,
}))
: [],
options: productOptions.edges
? productOptions.edges.map(
({
node: {
entityId,
values: { edges },
...rest
},
}: any) => ({
id: entityId,
values: edges.map(({ node }: any) => node),
...rest,
})
)
: [],
price: {
value: prices?.price.value,
currencyCode: prices?.price.currencyCode,
},
...rest,
}
}