mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
Add swell provider folder
This commit is contained in:
31
framework/swell/cart/utils/fetcher.ts
Normal file
31
framework/swell/cart/utils/fetcher.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
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: checkoutId },
|
||||
fetch,
|
||||
}) => {
|
||||
let checkout
|
||||
|
||||
if (checkoutId) {
|
||||
const data = await fetch({
|
||||
...options,
|
||||
variables: {
|
||||
checkoutId,
|
||||
},
|
||||
})
|
||||
checkout = data.node
|
||||
}
|
||||
|
||||
if (checkout?.completedAt || !checkoutId) {
|
||||
checkout = await checkoutCreate(fetch)
|
||||
}
|
||||
|
||||
// TODO: Fix this type
|
||||
return checkoutToCart({ checkout } as any)
|
||||
}
|
||||
|
||||
export default fetcher
|
Reference in New Issue
Block a user