Moved handler to each hook

This commit is contained in:
cond0r
2021-02-16 10:15:18 +02:00
parent 44081dddb6
commit f698dea698
17 changed files with 195 additions and 170 deletions

View File

@@ -1,6 +1,6 @@
import { Cart } from '@commerce/types'
import { CommerceError, ValidationError } from '@commerce/utils/errors'
import { normalizeCart } from '@framework/lib/normalize'
import { normalizeCart } from '@framework/utils/normalize'
import { Checkout, Maybe, UserError } from '@framework/schema'
const checkoutToCart = (checkoutResponse?: {

View File

@@ -0,0 +1,30 @@
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

View File

@@ -1,2 +1,3 @@
export { default as checkoutToCart } from './checkout-to-cart'
export { default as checkoutCreate } from './checkout-create'
export { default as fetcher } from './fetcher'