4
0
forked from crowetic/commerce
commerce/framework/shopify/cart/utils/checkout-to-cart.ts
2021-02-18 15:04:46 +02:00

23 lines
604 B
TypeScript

import { Cart } from '@commerce/types'
import { ValidationError } from '@commerce/utils/errors'
import { normalizeCart } from '@framework/utils/normalize'
import { Checkout, UserError } from '@framework/schema'
const checkoutToCart = (checkoutResponse: {
checkout: Checkout
userErrors?: UserError[]
}): Cart => {
const checkout = checkoutResponse?.checkout
const userErrors = checkoutResponse?.userErrors
if (userErrors && userErrors.length) {
throw new ValidationError({
message: userErrors[0].message,
})
}
return normalizeCart(checkout)
}
export default checkoutToCart