mirror of
https://github.com/vercel/commerce.git
synced 2025-07-04 12:11:22 +00:00
* folder and env setup * codegen.json headers removed * use-cart code flow updated * use-cart code flow updated * Implemented get-cart functionality * removed unused file * getAnonymousShopperToken function added * normalization mapping updated * PR points resolved * Anonymous shopper token query added * getAnonymousShopperToken function added * Anonymous shopper token query added Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>
138 lines
3.4 KiB
TypeScript
138 lines
3.4 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'
|
|
|
|
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 {
|
|
entityId: id,
|
|
productOptions,
|
|
prices,
|
|
path,
|
|
id: _,
|
|
options: _0,
|
|
} = productNode
|
|
|
|
return update(productNode, {
|
|
id: { $set: String(id) },
|
|
images: {
|
|
$apply: ({ edges }: any) =>
|
|
edges?.map(({ node: { urlOriginal, altText, ...rest } }: any) => ({
|
|
url: urlOriginal,
|
|
alt: altText,
|
|
...rest,
|
|
})),
|
|
},
|
|
variants: {
|
|
$apply: ({ edges }: any) =>
|
|
edges?.map(({ node: { entityId, productOptions, ...rest } }: any) => ({
|
|
id: entityId,
|
|
options: productOptions?.edges
|
|
? productOptions.edges.map(normalizeProductOption)
|
|
: [],
|
|
...rest,
|
|
})),
|
|
},
|
|
options: {
|
|
$set: productOptions.edges
|
|
? productOptions?.edges.map(normalizeProductOption)
|
|
: [],
|
|
},
|
|
brand: {
|
|
$apply: (brand: any) => (brand?.entityId ? brand?.entityId : null),
|
|
},
|
|
slug: {
|
|
$set: path?.replace(/^\/+|\/+$/g, ''),
|
|
},
|
|
price: {
|
|
$set: {
|
|
value: prices?.price.value,
|
|
currencyCode: prices?.price.currencyCode,
|
|
},
|
|
},
|
|
$unset: ['entityId'],
|
|
})
|
|
}
|
|
|
|
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: any): any {
|
|
return {
|
|
id: `${category.entityId}`,
|
|
name: category.name,
|
|
slug: getSlug(category.path),
|
|
path: category.path,
|
|
}
|
|
}
|