diff --git a/framework/shopify/cart/use-add-item.tsx b/framework/shopify/cart/use-add-item.tsx index c17462cc0..6dc3b8d6a 100644 --- a/framework/shopify/cart/use-add-item.tsx +++ b/framework/shopify/cart/use-add-item.tsx @@ -3,18 +3,10 @@ import { CommerceError } from '@commerce/utils/errors' import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item' import useCart from './use-cart' import { ShopifyProvider } from '..' -import { AddCartItemBody, CartItemBody } from '@commerce/types' -import { Cart } from '@framework/types' -import { - checkoutLineItemAddMutation, - getCheckoutId, - getCheckoutQuery, -} from '@framework/utils' +import { Cart, AddCartItemBody, CartItemBody } from '../types' +import { checkoutLineItemAddMutation, getCheckoutId } from '../utils' import { checkoutToCart } from './utils' - -const defaultOpts = { - query: checkoutLineItemAddMutation, -} +import { Mutation } from '../schema' export default useAddItem as UseAddItem @@ -22,8 +14,7 @@ export const handler: MutationHandler = { fetchOptions: { query: checkoutLineItemAddMutation, }, - async fetcher({ input, options, fetch }) { - const item = input.item ?? input + async fetcher({ input: { item }, options, fetch }) { if ( item.quantity && (!Number.isInteger(item.quantity) || item.quantity! < 1) @@ -33,8 +24,7 @@ export const handler: MutationHandler = { }) } - const data = await fetch({ - ...defaultOpts, + const { checkoutLineItemsAdd }: Mutation = await fetch({ ...options, variables: { lineItems: [ @@ -47,7 +37,7 @@ export const handler: MutationHandler = { }, }) - return checkoutToCart(data.checkoutLineItemsAdd) + return checkoutToCart(checkoutLineItemsAdd) }, useHook() { const { mutate } = useCart() diff --git a/framework/shopify/cart/utils/checkout-to-cart.ts b/framework/shopify/cart/utils/checkout-to-cart.ts index 104240220..42d797652 100644 --- a/framework/shopify/cart/utils/checkout-to-cart.ts +++ b/framework/shopify/cart/utils/checkout-to-cart.ts @@ -1,14 +1,26 @@ import { Cart } from '@commerce/types' -import { ValidationError } from '@commerce/utils/errors' -import { normalizeCart } from '@framework/utils/normalize' -import { Checkout, UserError } from '@framework/schema' +import { CommerceError, ValidationError } from '@commerce/utils/errors' -const checkoutToCart = (checkoutResponse: { - checkout: Checkout - userErrors?: UserError[] -}): Cart => { - const checkout = checkoutResponse?.checkout - const userErrors = checkoutResponse?.userErrors +import { + CheckoutLineItemsAddPayload, + CheckoutLineItemsUpdatePayload, + Maybe, +} from '@framework/schema' +import { normalizeCart } from '@framework/utils' + +export type CheckoutPayload = + | CheckoutLineItemsAddPayload + | CheckoutLineItemsUpdatePayload + +const checkoutToCart = (checkoutPayload?: Maybe): Cart => { + if (!checkoutPayload || !checkoutPayload?.checkout) { + throw new CommerceError({ + message: 'Invalid response from Shopify', + }) + } + + const checkout = checkoutPayload?.checkout + const userErrors = checkoutPayload?.userErrors if (userErrors && userErrors.length) { throw new ValidationError({ diff --git a/framework/shopify/types.ts b/framework/shopify/types.ts index 7f207b0c7..6ebfdf01d 100644 --- a/framework/shopify/types.ts +++ b/framework/shopify/types.ts @@ -23,25 +23,24 @@ export type OptionSelections = { option_value: number | string } -export interface CartItemBody extends Core.CartItemBody { +export type CartItemBody = Core.CartItemBody & { productId: string // The product id is always required for BC optionSelections?: OptionSelections } -export interface GetCartHandlerBody extends Core.GetCartHandlerBody {} +type X = Core.CartItemBody extends CartItemBody ? any : never +type Y = CartItemBody extends Core.CartItemBody ? any : never -export interface AddCartItemBody extends Core.AddCartItemBody {} +export type GetCartHandlerBody = Core.GetCartHandlerBody -export interface AddCartItemHandlerBody - extends Core.AddCartItemHandlerBody {} +export type AddCartItemBody = Core.AddCartItemBody -export interface UpdateCartItemBody - extends Core.UpdateCartItemBody {} +export type AddCartItemHandlerBody = Core.AddCartItemHandlerBody -export interface UpdateCartItemHandlerBody - extends Core.UpdateCartItemHandlerBody {} +export type UpdateCartItemBody = Core.UpdateCartItemBody -export interface RemoveCartItemBody extends Core.RemoveCartItemBody {} +export type UpdateCartItemHandlerBody = Core.UpdateCartItemHandlerBody -export interface RemoveCartItemHandlerBody - extends Core.RemoveCartItemHandlerBody {} +export type RemoveCartItemBody = Core.RemoveCartItemBody + +export type RemoveCartItemHandlerBody = Core.RemoveCartItemHandlerBody diff --git a/framework/shopify/utils/normalize.ts b/framework/shopify/utils/normalize.ts index edd295ddd..79074bf24 100644 --- a/framework/shopify/utils/normalize.ts +++ b/framework/shopify/utils/normalize.ts @@ -81,20 +81,20 @@ export function normalizeProduct(productNode: ShopifyProduct): any { } } -export function normalizeCart(data: Checkout): Cart { +export function normalizeCart(checkout: Checkout): Cart { return { - id: data.id, + id: checkout.id, customerId: '', email: '', - createdAt: data.createdAt, + createdAt: checkout.createdAt, currency: { - code: data.currencyCode, + code: checkout.currencyCode, }, - taxesIncluded: data.taxesIncluded, - lineItems: data.lineItems?.edges.map(normalizeLineItem), - lineItemsSubtotalPrice: data.subtotalPrice, - subtotalPrice: data.subtotalPrice, - totalPrice: data.totalPrice, + taxesIncluded: checkout.taxesIncluded, + lineItems: checkout.lineItems?.edges.map(normalizeLineItem), + lineItemsSubtotalPrice: checkout.subtotalPrice, + subtotalPrice: checkout.subtotalPrice, + totalPrice: checkout.totalPrice, discounts: [], } }