mirror of
https://github.com/vercel/commerce.git
synced 2025-06-09 09:36:58 +00:00
* Add dynamic API endpoints * Add missing dependency * Update api handlers * Updates * Fix build errors * Update package.json * Add checkout endpoint parser & update errors * Update tsconfig.json * Update cart.ts * Update parser * Update errors.ts * Update errors.ts * Move to Edge runtime * Revert to local * Fix switchable runtimes * Make nodejs default runtime * Update pnpm-lock.yaml * Update handlers * Fix build errors * Change headers
36 lines
861 B
TypeScript
36 lines
861 B
TypeScript
import { useMemo } from 'react'
|
|
import { SWRHook } from '@vercel/commerce/utils/types'
|
|
import useCart, { UseCart } from '@vercel/commerce/cart/use-cart'
|
|
|
|
export default useCart as UseCart<typeof handler>
|
|
|
|
export const handler: SWRHook<any> = {
|
|
fetchOptions: {
|
|
method: 'GET',
|
|
url: '/api/commerce/cart',
|
|
},
|
|
async fetcher({ options, fetch }) {
|
|
return await fetch({ ...options })
|
|
},
|
|
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]
|
|
)
|
|
},
|
|
}
|