mirror of
https://github.com/vercel/commerce.git
synced 2025-03-31 01:05:53 +00:00
31 lines
637 B
TypeScript
31 lines
637 B
TypeScript
import { HookFetcherFn } from '@commerce/utils/types'
|
|
import { Cart } from '@commerce/types'
|
|
import { checkoutCreate, checkoutToCart } from '.'
|
|
import { FetchCartInput } from '@commerce/cart/use-cart'
|
|
|
|
const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
|
options,
|
|
input: { cartId },
|
|
fetch,
|
|
}) => {
|
|
let checkout
|
|
|
|
if (cartId) {
|
|
const data = await fetch({
|
|
...options,
|
|
variables: {
|
|
cartId,
|
|
},
|
|
})
|
|
checkout = data?.node
|
|
}
|
|
|
|
if (checkout?.completedAt || !cartId) {
|
|
checkout = await checkoutCreate(fetch)
|
|
}
|
|
|
|
return checkoutToCart({ checkout })
|
|
}
|
|
|
|
export default fetcher
|