diff --git a/framework/bigcommerce/api/cart/handlers/get-cart.ts b/framework/bigcommerce/api/cart/handlers/get-cart.ts index 9fb42d730..031bbb63f 100644 --- a/framework/bigcommerce/api/cart/handlers/get-cart.ts +++ b/framework/bigcommerce/api/cart/handlers/get-cart.ts @@ -1,6 +1,7 @@ +import type { BigcommerceCart } from '../../../types' import { BigcommerceApiError } from '../../utils/errors' import getCartCookie from '../../utils/get-cart-cookie' -import type { Cart, CartHandlers } from '..' +import type { CartHandlers } from '../' // Return current cart info const getCart: CartHandlers['getCart'] = async ({ @@ -8,7 +9,7 @@ const getCart: CartHandlers['getCart'] = async ({ body: { cartId }, config, }) => { - let result: { data?: Cart } = {} + let result: { data?: BigcommerceCart } = {} if (cartId) { try { diff --git a/framework/bigcommerce/api/cart/handlers/update-item.ts b/framework/bigcommerce/api/cart/handlers/update-item.ts index b0ccc710b..df9ccaee8 100644 --- a/framework/bigcommerce/api/cart/handlers/update-item.ts +++ b/framework/bigcommerce/api/cart/handlers/update-item.ts @@ -14,9 +14,6 @@ const updateItem: CartHandlers['updateItem'] = async ({ }) } - console.log('ITEM', item) - console.log('AFTER', parseCartItem(item)) - const { data } = await config.storeApiFetch( `/v3/carts/${cartId}/items/${itemId}`, { diff --git a/framework/bigcommerce/api/cart/index.ts b/framework/bigcommerce/api/cart/index.ts index 0f0365cc3..7b5be31c3 100644 --- a/framework/bigcommerce/api/cart/index.ts +++ b/framework/bigcommerce/api/cart/index.ts @@ -8,7 +8,11 @@ import getCart from './handlers/get-cart' import addItem from './handlers/add-item' import updateItem from './handlers/update-item' import removeItem from './handlers/remove-item' -import type { Cart, UpdateCartItemHandlerBody } from '../../types' +import type { + BigcommerceCart, + GetCartHandlerBody, + UpdateCartItemHandlerBody, +} from '../../types' type OptionSelections = { option_id: Number @@ -27,11 +31,14 @@ export type AddItemBody = { item: ItemBody } export type RemoveItemBody = { itemId: string } export type CartHandlers = { - getCart: BigcommerceHandler - addItem: BigcommerceHandler> - updateItem: BigcommerceHandler + getCart: BigcommerceHandler + addItem: BigcommerceHandler< + BigcommerceCart, + { cartId?: string } & Partial + > + updateItem: BigcommerceHandler removeItem: BigcommerceHandler< - Cart, + BigcommerceCart, { cartId?: string } & Partial > } diff --git a/framework/bigcommerce/cart/use-cart.tsx b/framework/bigcommerce/cart/use-cart.tsx index 8e008c65a..afa37ec98 100644 --- a/framework/bigcommerce/cart/use-cart.tsx +++ b/framework/bigcommerce/cart/use-cart.tsx @@ -3,7 +3,7 @@ import type { SwrOptions } from '@commerce/utils/use-data' import useResponse from '@commerce/utils/use-response' import useCommerceCart, { CartInput } from '@commerce/cart/use-cart' import { normalizeCart } from '../lib/normalize' -import type { Cart as BigcommerceCart } from '../api/cart' +import type { Cart, BigcommerceCart } from '../types' const defaultOpts = { url: '/api/bigcommerce/cart', diff --git a/framework/bigcommerce/cart/use-update-item.tsx b/framework/bigcommerce/cart/use-update-item.tsx index 6592b090f..d1870c818 100644 --- a/framework/bigcommerce/cart/use-update-item.tsx +++ b/framework/bigcommerce/cart/use-update-item.tsx @@ -2,11 +2,12 @@ import { useCallback } from 'react' import debounce from 'lodash.debounce' import type { HookFetcher } from '@commerce/utils/types' import { ValidationError } from '@commerce/utils/errors' -import useCartUpdateItem from '@commerce/cart/use-update-item' +import useCartUpdateItem, { + UpdateItemInput as UseUpdateItemInput, +} from '@commerce/cart/use-update-item' import { normalizeCart } from '../lib/normalize' import type { UpdateCartItemBody, - UpdateCartItemInput, Cart, BigcommerceCart, LineItem, @@ -19,6 +20,10 @@ const defaultOpts = { method: 'PUT', } +export type UpdateItemInput = T extends LineItem + ? Partial> + : UseUpdateItemInput + export const fetcher: HookFetcher = async ( options, { itemId, item }, @@ -55,31 +60,24 @@ function extendHook(customFetcher: typeof fetcher, cfg?: { wait?: number }) { ) return useCallback( - debounce( - async ( - input: T extends LineItem - ? Partial - : UpdateCartItemInput - ) => { - const itemId = input.id ?? item?.id - const productId = input.productId ?? item?.productId - const variantId = input.productId ?? item?.variantId + debounce(async (input: UpdateItemInput) => { + const itemId = input.id ?? item?.id + const productId = input.productId ?? item?.productId + const variantId = input.productId ?? item?.variantId - if (!itemId || !productId || !variantId) { - throw new ValidationError({ - message: 'Invalid input used for this operation', - }) - } - - const data = await fn({ - itemId, - item: { productId, variantId, quantity: input.quantity }, + if (!itemId || !productId || !variantId) { + throw new ValidationError({ + message: 'Invalid input used for this operation', }) - await mutate(data, false) - return data - }, - cfg?.wait ?? 500 - ), + } + + const data = await fn({ + itemId, + item: { productId, variantId, quantity: input.quantity }, + }) + await mutate(data, false) + return data + }, cfg?.wait ?? 500), [fn, mutate] ) } diff --git a/framework/bigcommerce/types.ts b/framework/bigcommerce/types.ts index 766f2dbe9..26d093bfe 100644 --- a/framework/bigcommerce/types.ts +++ b/framework/bigcommerce/types.ts @@ -43,12 +43,10 @@ export interface CartItemBody extends Core.CartItemBody { optionSelections?: OptionSelections } -export interface UpdateCartItemBody extends Core.UpdateCartItemBody { - item: CartItemBody -} +export interface GetCartHandlerBody extends Core.GetCartHandlerBody {} -export interface UpdateCartItemInput - extends Core.UpdateCartItemInput {} +export interface UpdateCartItemBody + extends Core.UpdateCartItemBody {} export interface UpdateCartItemHandlerBody - extends Core.UpdateCartItemHandlerBody {} + extends Core.UpdateCartItemHandlerBody {} diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index 94ccb73fc..0a7ba49ee 100644 --- a/framework/commerce/cart/use-cart.tsx +++ b/framework/commerce/cart/use-cart.tsx @@ -6,6 +6,7 @@ import { useCommerce } from '..' export type CartResponse = ResponseState & { isEmpty?: boolean } +// Input expected by the `useCart` hook export type CartInput = { cartId?: Cart['id'] } diff --git a/framework/commerce/cart/use-update-item.tsx b/framework/commerce/cart/use-update-item.tsx index 1c6261054..e1adcb5fb 100644 --- a/framework/commerce/cart/use-update-item.tsx +++ b/framework/commerce/cart/use-update-item.tsx @@ -1,4 +1,10 @@ import useAction from '../utils/use-action' +import type { CartItemBody } from '../types' + +// Input expected by the action returned by the `useUpdateItem` hook +export type UpdateItemInput = T & { + id: string +} const useUpdateItem = useAction diff --git a/framework/commerce/types.ts b/framework/commerce/types.ts index c662077a1..61bf0d5e7 100644 --- a/framework/commerce/types.ts +++ b/framework/commerce/types.ts @@ -94,18 +94,19 @@ export interface CartItemBody { quantity?: number } -// Body by the update operation -export interface UpdateCartItemBody { - itemId: string - item: CartItemBody +// Body used by the `getCart` operation handler +export interface GetCartHandlerBody { + cartId?: string } -// Input expected by the `useUpdateItem` hook -export type UpdateCartItemInput = T & { - id: string +// Body used by the update operation +export interface UpdateCartItemBody { + itemId: string + item: T } // Body expected by the update operation handler -export interface UpdateCartItemHandlerBody extends Partial { +export interface UpdateCartItemHandlerBody + extends Partial> { cartId?: string }