mirror of
https://github.com/vercel/commerce.git
synced 2025-03-28 08:15:54 +00:00
* Minimal list/detail views working with Vendure * Implement useCart/useAddItem * Implement useUpdateItem & useRemoveItem * Implement useSearch * Add operations codegen, tidy up * Dummy checkout page * Implement auth/customer hooks * Use env var for Shop API url * Add some documentation * Improve error handling * Optimize preview image size * Fix accidental change * Update Vendure provider to latest changes * Vendure provider: split out gql operations, remove unused files * Update Vendure provider readme * Add local next.config to Vendure provider, update docs * Update to use demo server * Fix build errors * Use proxy for vendure api * Simplify instructions in Vendure readme * Refactor Vendure checkout api handler * Improve image quality
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { Cart, Product } from '@commerce/types'
|
|
import { CartFragment, SearchResultFragment } from '../schema'
|
|
|
|
export function normalizeSearchResult(item: SearchResultFragment): Product {
|
|
return {
|
|
id: item.productId,
|
|
name: item.productName,
|
|
description: item.description,
|
|
slug: item.slug,
|
|
path: item.slug,
|
|
images: [{ url: item.productAsset?.preview + '?w=800&mode=crop' || '' }],
|
|
variants: [],
|
|
price: {
|
|
value: (item.priceWithTax as any).min / 100,
|
|
currencyCode: item.currencyCode,
|
|
},
|
|
options: [],
|
|
sku: item.sku,
|
|
}
|
|
}
|
|
|
|
export function normalizeCart(order: CartFragment): Cart {
|
|
return {
|
|
id: order.id.toString(),
|
|
createdAt: order.createdAt,
|
|
taxesIncluded: true,
|
|
lineItemsSubtotalPrice: order.subTotalWithTax / 100,
|
|
currency: { code: order.currencyCode },
|
|
subtotalPrice: order.subTotalWithTax / 100,
|
|
totalPrice: order.totalWithTax / 100,
|
|
customerId: order.customer?.id,
|
|
lineItems: order.lines?.map((l) => ({
|
|
id: l.id,
|
|
name: l.productVariant.name,
|
|
quantity: l.quantity,
|
|
url: l.productVariant.product.slug,
|
|
variantId: l.productVariant.id,
|
|
productId: l.productVariant.productId,
|
|
images: [{ url: l.featuredAsset?.preview + '?preset=thumb' || '' }],
|
|
discounts: l.discounts.map((d) => ({ value: d.amount / 100 })),
|
|
path: '',
|
|
variant: {
|
|
id: l.productVariant.id,
|
|
name: l.productVariant.name,
|
|
sku: l.productVariant.sku,
|
|
price: l.discountedLinePriceWithTax / 100,
|
|
listPrice: l.linePriceWithTax / 100,
|
|
image: {
|
|
url: l.featuredAsset?.preview + '?preset=thumb' || '',
|
|
},
|
|
requiresShipping: true,
|
|
},
|
|
})),
|
|
}
|
|
}
|