mirror of
https://github.com/vercel/commerce.git
synced 2025-03-27 15:55:54 +00:00
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import useCart, { UseCart } from '@commerce/cart/use-cart'
|
|
import { SWRHook } from '@commerce/utils/types'
|
|
import { useMemo } from 'react'
|
|
import { normalizeCart } from '../utils/normalize'
|
|
import { checkoutCreate, checkoutToCart } from './utils'
|
|
import type { GetCartHook } from '@commerce/types/cart'
|
|
|
|
export default useCart as UseCart<typeof handler>
|
|
|
|
export const handler: SWRHook<GetCartHook> = {
|
|
fetchOptions: {
|
|
query: 'cart',
|
|
method: 'get',
|
|
},
|
|
async fetcher({ fetch }) {
|
|
const cart = await checkoutCreate(fetch)
|
|
|
|
return cart ? normalizeCart(cart) : null
|
|
},
|
|
useHook:
|
|
({ useData }) =>
|
|
(input) => {
|
|
const response = useData({
|
|
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
|
|
})
|
|
return useMemo(
|
|
() =>
|
|
Object.create(response, {
|
|
isEmpty: {
|
|
get() {
|
|
return (response.data?.lineItems.length ?? 0) <= 0
|
|
},
|
|
enumerable: true,
|
|
},
|
|
}),
|
|
[response]
|
|
)
|
|
},
|
|
}
|