mirror of
https://github.com/vercel/commerce.git
synced 2025-07-09 22:31:21 +00:00
131 lines
3.5 KiB
TypeScript
131 lines
3.5 KiB
TypeScript
// import type { Product } from '../types/product'
|
|
// import type { Cart, BigcommerceCart, LineItem } from '../types/cart'
|
|
// import type { Page } from '../types/page'
|
|
// import type { BCCategory, Category } from '../types/site'
|
|
// import { definitions } from '../api/definitions/store-content'
|
|
import update from './immutability'
|
|
import getSlug from './get-slug'
|
|
import {PrCategory} from '../schema'
|
|
|
|
function normalizeProductOption(productOption: any) {
|
|
const {
|
|
node: {
|
|
entityId,
|
|
values: { edges = [] } = {},
|
|
...rest
|
|
},
|
|
} = productOption
|
|
|
|
return {
|
|
id: entityId,
|
|
values: edges?.map(({ node }: any) => node),
|
|
...rest,
|
|
}
|
|
}
|
|
|
|
export function normalizeProduct(productNode: any): any {
|
|
const product = {
|
|
id: productNode.productCode,
|
|
name: productNode.content.productName,
|
|
vendor: "",
|
|
path: `/${productNode.productCode}/${productNode.content.seoFriendlyUrl}`,
|
|
// slug: `${productNode.productCode}/${productNode.content.seoFriendlyUrl}`,
|
|
slug: productNode.content.seoFriendlyUrl,
|
|
price: { value: productNode.price.price, "currencyCode": "USD" },
|
|
descriptionHtml: productNode.content.productShortDescription,
|
|
|
|
images: productNode.content.productImages.map((p: any)=> ({
|
|
url: `http:${p.imageUrl}`,
|
|
altText: p.imageLabel,
|
|
})),
|
|
|
|
variants: productNode.variations?.map((v:any) => ({
|
|
id: v.productCode,
|
|
options: v.options.map((o:any) => ({
|
|
["__typename"]: o["__typename"],
|
|
id: o.attributeFQN,
|
|
displayName: o.attributeFQN.split('~')[1].toUpperCase(),
|
|
values: [{label: o.value}]
|
|
}))
|
|
})) || [],
|
|
|
|
options:productNode.options?.map((o:any)=> ({
|
|
id: o.attributeFQN,
|
|
displayName: o.attributeDetail.name,
|
|
values: o.values.map( (v:any)=> ({
|
|
label: v.value,
|
|
hexColors: ""
|
|
}))
|
|
})) || []
|
|
}
|
|
|
|
return product;
|
|
}
|
|
|
|
export function normalizePage(page: any): any {
|
|
return {
|
|
id: String(page.id),
|
|
name: page.name,
|
|
is_visible: page.is_visible,
|
|
sort_order: page.sort_order,
|
|
body: page.body,
|
|
}
|
|
}
|
|
|
|
export function normalizeCart(data: any): any {
|
|
return {
|
|
id: data.id,
|
|
customerId: data.userId,
|
|
email: data?.email,
|
|
createdAt: data?.created_time,
|
|
currency: {
|
|
code: 'USD',
|
|
},
|
|
taxesIncluded: true,
|
|
lineItems: data.items.map(normalizeLineItem),
|
|
lineItemsSubtotalPrice: data?.items.reduce(
|
|
(acc: number, obj: { subtotal: number }) => acc + obj.subtotal,
|
|
0
|
|
),
|
|
subtotalPrice: data?.subtotal,
|
|
totalPrice: data?.total,
|
|
discounts: data.orderDiscounts?.map((discount: any) => ({
|
|
value: discount.impact,
|
|
})),
|
|
}
|
|
}
|
|
|
|
function normalizeLineItem(item: any): any {
|
|
return {
|
|
id: item.id,
|
|
variantId: item.product.variationProductCode,
|
|
productId: String(item.product.productCode),
|
|
name: item.product.name,
|
|
quantity: item.quantity,
|
|
variant: {
|
|
id: item.product.variationProductCode,
|
|
sku: item.product?.sku,
|
|
name: item.product.name,
|
|
image: {
|
|
url: item?.product.imageUrl,
|
|
},
|
|
requiresShipping: item?.is_require_shipping,
|
|
price: item?.unitPrice.extendedAmount,
|
|
listPrice: 0,
|
|
},
|
|
path: `${item.product.productCode}/na`,
|
|
discounts: item?.discounts?.map((discount: any) => ({
|
|
value: discount.discounted_amount,
|
|
})),
|
|
}
|
|
}
|
|
|
|
export function normalizeCategory(category: PrCategory): any {
|
|
return {
|
|
id: category?.categoryCode,
|
|
name: category?.content?.name,
|
|
slug: category?.content?.slug,
|
|
path: `/${category?.content?.slug}`
|
|
}
|
|
}
|