forked from crowetic/commerce
* 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>
227 lines
4.7 KiB
TypeScript
227 lines
4.7 KiB
TypeScript
import { Customer } from '../types/customer'
|
|
import { Product, ProductOption } from '../types/product'
|
|
import { MoneyV2 } from '../../schema'
|
|
|
|
import type {
|
|
Cart,
|
|
CartLineItem,
|
|
SwellCustomer,
|
|
SwellProduct,
|
|
SwellImage,
|
|
SwellVariant,
|
|
ProductOptionValue,
|
|
SwellProductOptionValue,
|
|
SwellCart,
|
|
LineItem,
|
|
} from '../types'
|
|
|
|
const money = ({ amount, currencyCode }: MoneyV2) => {
|
|
return {
|
|
value: +amount,
|
|
currencyCode,
|
|
}
|
|
}
|
|
|
|
type swellProductOption = {
|
|
id: string
|
|
name: string
|
|
values: any[]
|
|
}
|
|
|
|
type normalizedProductOption = {
|
|
id: string
|
|
displayName: string
|
|
values: ProductOptionValue[]
|
|
}
|
|
|
|
const normalizeProductOption = ({
|
|
id,
|
|
name: displayName = '',
|
|
values = [],
|
|
}: swellProductOption): ProductOption => {
|
|
let returnValues = values.map((value) => {
|
|
let output: any = {
|
|
label: value.name,
|
|
// id: value?.id || id,
|
|
}
|
|
if (displayName.match(/colou?r/gi)) {
|
|
output = {
|
|
...output,
|
|
hexColors: [value.name],
|
|
}
|
|
}
|
|
return output
|
|
})
|
|
return {
|
|
__typename: 'MultipleChoiceOption',
|
|
id,
|
|
displayName,
|
|
values: returnValues,
|
|
}
|
|
}
|
|
|
|
const normalizeProductImages = (images: SwellImage[]) => {
|
|
if (!images || images.length < 1) {
|
|
return [{ url: '/' }]
|
|
}
|
|
return images?.map(({ file, ...rest }: SwellImage) => ({
|
|
url: file?.url + '',
|
|
height: Number(file?.height),
|
|
width: Number(file?.width),
|
|
...rest,
|
|
}))
|
|
}
|
|
|
|
const normalizeProductVariants = (
|
|
variants: SwellVariant[],
|
|
productOptions: swellProductOption[]
|
|
) => {
|
|
return variants?.map(
|
|
({ id, name, price, option_value_ids: optionValueIds = [] }) => {
|
|
const values = name
|
|
.split(',')
|
|
.map((i) => ({ name: i.trim(), label: i.trim() }))
|
|
|
|
const options = optionValueIds.map((id) => {
|
|
const matchingOption = productOptions.find((option) => {
|
|
return option.values.find(
|
|
(value: SwellProductOptionValue) => value.id == id
|
|
)
|
|
})
|
|
return normalizeProductOption({
|
|
id,
|
|
name: matchingOption?.name ?? '',
|
|
values,
|
|
})
|
|
})
|
|
|
|
return {
|
|
id,
|
|
// name,
|
|
// sku: sku ?? id,
|
|
// price: price ?? null,
|
|
// listPrice: price ?? null,
|
|
// requiresShipping: true,
|
|
options,
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
export function normalizeProduct(swellProduct: SwellProduct): Product {
|
|
const {
|
|
id,
|
|
name,
|
|
description,
|
|
images,
|
|
options,
|
|
slug,
|
|
variants,
|
|
price: value,
|
|
currency: currencyCode,
|
|
} = swellProduct
|
|
// ProductView accesses variants for each product
|
|
const emptyVariants = [{ options: [], id, name }]
|
|
|
|
const productOptions = options
|
|
? options.map((o) => normalizeProductOption(o))
|
|
: []
|
|
const productVariants = variants
|
|
? normalizeProductVariants(variants, options)
|
|
: []
|
|
|
|
const productImages = normalizeProductImages(images)
|
|
const product = {
|
|
...swellProduct,
|
|
description,
|
|
id,
|
|
vendor: '',
|
|
path: `/${slug}`,
|
|
images: productImages,
|
|
variants:
|
|
productVariants && productVariants.length
|
|
? productVariants
|
|
: emptyVariants,
|
|
options: productOptions,
|
|
price: {
|
|
value,
|
|
currencyCode,
|
|
},
|
|
}
|
|
return product
|
|
}
|
|
|
|
export function normalizeCart({
|
|
id,
|
|
account_id,
|
|
date_created,
|
|
currency,
|
|
tax_included_total,
|
|
items,
|
|
sub_total,
|
|
grand_total,
|
|
discounts,
|
|
}: SwellCart) {
|
|
const cart: Cart = {
|
|
id: id,
|
|
customerId: account_id + '',
|
|
email: '',
|
|
createdAt: date_created,
|
|
currency: { code: currency },
|
|
taxesIncluded: tax_included_total > 0,
|
|
lineItems: items?.map(normalizeLineItem) ?? [],
|
|
lineItemsSubtotalPrice: +sub_total,
|
|
subtotalPrice: +sub_total,
|
|
totalPrice: grand_total,
|
|
discounts: discounts?.map((discount) => ({ value: discount.amount })),
|
|
}
|
|
return cart
|
|
}
|
|
|
|
export function normalizeCustomer(customer: SwellCustomer): Customer {
|
|
const { first_name: firstName, last_name: lastName } = customer
|
|
return {
|
|
...customer,
|
|
firstName,
|
|
lastName,
|
|
}
|
|
}
|
|
|
|
function normalizeLineItem({
|
|
id,
|
|
product,
|
|
price,
|
|
variant,
|
|
quantity,
|
|
}: CartLineItem): LineItem {
|
|
const item = {
|
|
id,
|
|
variantId: variant?.id,
|
|
productId: product.id ?? '',
|
|
name: product?.name ?? '',
|
|
quantity,
|
|
variant: {
|
|
id: variant?.id ?? '',
|
|
sku: variant?.sku ?? '',
|
|
name: variant?.name!,
|
|
image: {
|
|
url:
|
|
product?.images && product.images.length > 0
|
|
? product?.images[0].file.url
|
|
: '/',
|
|
},
|
|
requiresShipping: false,
|
|
price: price,
|
|
listPrice: price,
|
|
},
|
|
path: '',
|
|
discounts: [],
|
|
options: [
|
|
{
|
|
value: variant?.name,
|
|
},
|
|
],
|
|
}
|
|
return item
|
|
}
|