mirror of
https://github.com/vercel/commerce.git
synced 2025-03-28 08:15:54 +00:00
28 lines
717 B
TypeScript
28 lines
717 B
TypeScript
import useCart, { UseCart } from '@commerce/cart/use-cart'
|
|
import { Cart } from '@commerce/types'
|
|
import { SWRHook } from '@commerce/utils/types'
|
|
import { normalizeCart } from '../utils/normalize'
|
|
import { checkoutCreate, checkoutToCart } from './utils'
|
|
|
|
export default useCart as UseCart<typeof handler>
|
|
|
|
export const handler: SWRHook<Cart | null> = {
|
|
fetchOptions: {
|
|
query: 'cart',
|
|
method: 'get',
|
|
},
|
|
async fetcher({ options, fetch }) {
|
|
const cart = await checkoutCreate(fetch)
|
|
|
|
return cart ? normalizeCart(cart) : null
|
|
},
|
|
useHook: ({ useData }) => (input) => {
|
|
return useData({
|
|
swrOptions: {
|
|
revalidateOnFocus: false,
|
|
...input?.swrOptions,
|
|
},
|
|
})
|
|
},
|
|
}
|