4
0
forked from crowetic/commerce
2021-01-12 16:59:07 -03:00

79 lines
1.6 KiB
TypeScript

import { Product as BCProduct } from '@framework/schema'
function normalizeProductOption({
node: {
entityId,
values: { edges },
...rest
},
}: any) {
return {
id: entityId,
values: edges?.map(({ node }: any) => node),
...rest,
}
}
export function normalizeProduct(productNode: BCProduct): Product {
const {
entityId: id,
images,
variants,
productOptions,
prices,
path,
id: _,
options: _0,
brand,
...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, productOptions, ...rest } }: any) => ({
id: entityId,
options: productOptions?.edges
? productOptions.edges.map(normalizeProductOption)
: [],
...rest,
})
)
: [],
options: productOptions.edges
? productOptions?.edges.map(normalizeProductOption)
: [],
brand: {
id: brand?.entityId ? brand?.entityId : null,
...brand,
},
price: {
value: prices?.price.value,
currencyCode: prices?.price.currencyCode,
},
...rest,
}
}
export function normalizeCart({ data, ...rest }: any) {
return {
...rest,
data: {
products: data?.line_items?.physical_items ?? [],
...data,
},
}
}