4
0
forked from crowetic/commerce
commerce/lib/bigcommerce/cart/use-add-item.tsx
2020-10-04 19:44:11 -05:00

24 lines
650 B
TypeScript

import type { Fetcher } from '@lib/commerce'
import { default as useCartAddItem } from '@lib/commerce/cart/use-add-item'
import type { Item } from '../api/cart'
import { Cart } from '.'
export type { Item }
function fetcher(fetch: Fetcher<Cart>, { item }: { item: Item }) {
if (
item.quantity &&
(!Number.isInteger(item.quantity) || item.quantity! < 1)
) {
throw new Error(
'The item quantity has to be a valid integer greater than 0'
)
}
return fetch({ url: '/api/bigcommerce/cart', method: 'POST', body: { item } })
}
export default function useAddItem() {
return useCartAddItem<Cart, { item: Item }>(fetcher)
}