mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 23:16:59 +00:00
* Moved everything * Figuring out how to make imports work * Updated exports * Added missing exports * Added @vercel/commerce-local to `site` * Updated commerce config * Updated exports and commerce config * Updated commerce hoc * Fixed exports in local * Added publish config * Updated imports in site * It's actually working * Don't use debugger in dev for better speeds * Improved DX when editing packages * Set up eslint with husky * Updated prettier config * Added prettier setup to every package * Moved bigcommerce * Moved Bigcommerce to src and package updates * Updated setup of bigcommerce * Moved definitions script * Moved commercejs * Move to src * Fixed types in commercejs * Moved kibocommerce * Moved kibocommerce to src * Added package/tsconfig to kibocommerce * Fixed imports and other things * Moved ordercloud * Moved ordercloud to src * Fixed imports * Added missing prettier files * Moved Saleor * Moved Saleor to src * Fixed imports * Replaced all imports to @commerce * Added prettierignore/rc to all providers * Moved shopify to src * Build shopify in packages * Moved Spree * Moved spree to src * Updated spree * Moved swell * Moved swell to src * Fixed type imports in swell * Moved Vendure to packages * Moved vendure to src * Fixed imports in vendure * Added codegen to saleor * Updated codegen setup for shopify * Added codegen to vendure * Added codegen to kibocommerce * Added all packages to site's deps * Updated codegen setup in bigcommerce * Minor fixes * Updated providers' names in site * Updated packages based on Bel's changes * Updated turbo to latest * Fixed ts complains * Set npm engine in root * New lockfile install * remove engines * Regen lockfile * Switched from npm to yarn * Updated typesVersions in all packages * Moved dep * Updated SWR to the just released 1.2.0 * Removed "isolatedModules" from packages * Updated list of providers and default * Updated swell declaration * Removed next import from kibocommerce * Added COMMERCE_PROVIDER log * Added another log * Updated turbo config * Updated docs * Removed test logs Co-authored-by: Jared Palmer <jared@jaredpalmer.com>
137 lines
3.5 KiB
TypeScript
137 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'
|
|
|
|
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): Product {
|
|
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: definitions['page_Full']): Page {
|
|
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: BigcommerceCart): Cart {
|
|
return {
|
|
id: data.id,
|
|
customerId: String(data.customer_id),
|
|
email: data.email,
|
|
createdAt: data.created_time,
|
|
currency: data.currency,
|
|
taxesIncluded: data.tax_included,
|
|
lineItems: [
|
|
...data.line_items.physical_items.map(normalizeLineItem),
|
|
...data.line_items.digital_items.map(normalizeLineItem),
|
|
],
|
|
lineItemsSubtotalPrice: data.base_amount,
|
|
subtotalPrice: data.base_amount + data.discount_amount,
|
|
totalPrice: data.cart_amount,
|
|
discounts: data.discounts?.map((discount) => ({
|
|
value: discount.discounted_amount,
|
|
})),
|
|
}
|
|
}
|
|
|
|
function normalizeLineItem(item: any): LineItem {
|
|
return {
|
|
id: item.id,
|
|
variantId: String(item.variant_id),
|
|
productId: String(item.product_id),
|
|
name: item.name,
|
|
quantity: item.quantity,
|
|
variant: {
|
|
id: String(item.variant_id),
|
|
sku: item.sku,
|
|
name: item.name,
|
|
image: {
|
|
url: item.image_url,
|
|
},
|
|
requiresShipping: item.is_require_shipping,
|
|
price: item.sale_price,
|
|
listPrice: item.list_price,
|
|
},
|
|
options: item.options,
|
|
path: item.url.split('/')[3],
|
|
discounts: item.discounts.map((discount: any) => ({
|
|
value: discount.discounted_amount,
|
|
})),
|
|
}
|
|
}
|
|
|
|
export function normalizeCategory(category: BCCategory): Category {
|
|
return {
|
|
id: `${category.entityId}`,
|
|
name: category.name,
|
|
slug: getSlug(category.path),
|
|
path: category.path,
|
|
}
|
|
}
|