Files
commerce/framework/woocommerce/utils/normalize.ts
2021-09-18 22:09:54 +03:00

59 lines
1.2 KiB
TypeScript

import type { Product, ProductImage } from '../types/product'
import { SimpleProduct, ProductToMediaItemConnection } from '../schema'
const normalizeProductImages = ({
edges,
}: ProductToMediaItemConnection): ProductImage[] => {
const edges_ =
edges
?.filter((edge) => edge?.node)
.map((edge) => {
return {
url: edge?.node?.sourceUrl ?? '',
alt: edge?.node?.altText ?? edge?.node?.title ?? '',
}
}) ?? []
return edges_
}
export function normalizeProduct({
id,
name,
sku,
description,
shortDescription,
slug,
image,
galleryImages,
price,
...rest
}: SimpleProduct): Product {
const images: ProductToMediaItemConnection = galleryImages ?? { edges: [] }
if (image) {
images.edges?.push({ node: image })
}
const product = {
id,
options: [],
variants: [],
name: name ?? id,
sku: sku ?? 'sku',
path: slug ?? id,
slug: slug?.replace(/^\/+|\/+$/g, ''),
images: normalizeProductImages(images),
price: { value: 0, currencyCode: 'USD' },
description: description ?? shortDescription ?? '',
descriptionHtml: description ?? shortDescription ?? '',
}
if (price) {
product.price.value = Number(price.substring(1))
}
return product
}