mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 19:51:23 +00:00
Moved handler to each hook
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { SearchProductsInput } from '@framework/product/use-search'
|
||||
import getSortVariables from './get-sort-variables'
|
||||
import type { SearchProductsInput } from '@framework/product/use-search'
|
||||
|
||||
export const getSearchVariables = ({
|
||||
categoryId,
|
||||
|
@@ -3,7 +3,9 @@ export { default as getSearchVariables } from './get-search-variables'
|
||||
export { default as getSortVariables } from './get-sort-variables'
|
||||
export { default as getVendors } from './get-vendors'
|
||||
export { default as getCategories } from './get-categories'
|
||||
export { default as getCheckoutId } from './get-checkout-id'
|
||||
|
||||
export * from './customer-token'
|
||||
export * from './queries'
|
||||
export * from './mutations'
|
||||
export * from './normalize'
|
||||
export * from './customer-token'
|
||||
|
@@ -1,8 +1,10 @@
|
||||
export { default as createCustomerMutation } from './customer-create'
|
||||
export { default as checkoutCreateMutation } from './checkout-create'
|
||||
|
||||
export { default as checkoutLineItemAddMutation } from './checkout-line-item-add'
|
||||
export { default as checkoutLineItemUpdateMutation } from './checkout-create'
|
||||
export { default as checkoutLineItemRemoveMutation } from './checkout-line-item-remove'
|
||||
|
||||
export { default as customerCreateMutation } from './customer-create'
|
||||
export { default as customerAccessTokenCreateMutation } from './customer-access-token-create'
|
||||
export { default as customerAccessTokenDeleteMutation } from './customer-access-token-delete'
|
||||
|
125
framework/shopify/utils/normalize.ts
Normal file
125
framework/shopify/utils/normalize.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
import {
|
||||
Product as ShopifyProduct,
|
||||
Checkout,
|
||||
CheckoutLineItemEdge,
|
||||
SelectedOption,
|
||||
ImageConnection,
|
||||
ProductVariantConnection,
|
||||
ProductOption,
|
||||
MoneyV2,
|
||||
} from '@framework/schema'
|
||||
|
||||
import type { Cart, LineItem } from '../types'
|
||||
|
||||
const money = ({ amount, currencyCode }: MoneyV2) => {
|
||||
return {
|
||||
value: +amount,
|
||||
currencyCode,
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeProductOption = ({
|
||||
name: displayName,
|
||||
values,
|
||||
...rest
|
||||
}: ProductOption) => {
|
||||
return {
|
||||
__typename: 'MultipleChoiceOption',
|
||||
displayName,
|
||||
values: values.map((value) => ({
|
||||
label: value,
|
||||
hexColors: displayName === 'Color' ? [value] : null,
|
||||
})),
|
||||
...rest,
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeProductImages = ({ edges }: ImageConnection) =>
|
||||
edges?.map(({ node: { originalSrc: url, ...rest } }) => ({
|
||||
url,
|
||||
...rest,
|
||||
}))
|
||||
|
||||
const normalizeProductVariants = ({ edges }: ProductVariantConnection) =>
|
||||
edges?.map(({ node: { id, selectedOptions } }) => ({
|
||||
id,
|
||||
options: selectedOptions.map(({ name, value }: SelectedOption) =>
|
||||
normalizeProductOption({
|
||||
id,
|
||||
name,
|
||||
values: [value],
|
||||
})
|
||||
),
|
||||
}))
|
||||
|
||||
export function normalizeProduct(productNode: ShopifyProduct): any {
|
||||
const {
|
||||
id,
|
||||
title: name,
|
||||
vendor,
|
||||
images,
|
||||
variants,
|
||||
description,
|
||||
handle,
|
||||
priceRange,
|
||||
options,
|
||||
...rest
|
||||
} = productNode
|
||||
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
vendor,
|
||||
description,
|
||||
path: `/${handle}`,
|
||||
slug: handle?.replace(/^\/+|\/+$/g, ''),
|
||||
price: money(priceRange?.minVariantPrice),
|
||||
images: normalizeProductImages(images),
|
||||
variants: variants ? normalizeProductVariants(variants) : null,
|
||||
options: options ? options.map((o) => normalizeProductOption(o)) : [],
|
||||
...rest,
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeCart(data: Checkout): Cart {
|
||||
return {
|
||||
id: data.id,
|
||||
customerId: '',
|
||||
email: '',
|
||||
createdAt: data.createdAt,
|
||||
currency: {
|
||||
code: data.currencyCode,
|
||||
},
|
||||
taxesIncluded: data.taxesIncluded,
|
||||
lineItems: data.lineItems?.edges.map(normalizeLineItem),
|
||||
lineItemsSubtotalPrice: data.subtotalPrice,
|
||||
subtotalPrice: data.subtotalPrice,
|
||||
totalPrice: data.totalPrice,
|
||||
discounts: [],
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeLineItem({
|
||||
node: { id, title, variant, quantity },
|
||||
}: CheckoutLineItemEdge): LineItem {
|
||||
return {
|
||||
id,
|
||||
variantId: String(variant?.id),
|
||||
productId: String(variant?.id),
|
||||
name: `${title} - ${variant?.title}`,
|
||||
quantity: quantity,
|
||||
variant: {
|
||||
id: String(variant?.id),
|
||||
sku: variant?.sku ?? '',
|
||||
name: variant?.title!,
|
||||
image: {
|
||||
url: variant?.image?.originalSrc,
|
||||
},
|
||||
requiresShipping: variant?.requiresShipping ?? false,
|
||||
price: variant?.price,
|
||||
listPrice: variant?.compareAtPrice,
|
||||
},
|
||||
path: '',
|
||||
discounts: [],
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user