mirror of
https://github.com/vercel/commerce.git
synced 2025-04-10 05:25:54 +00:00
29 lines
766 B
TypeScript
29 lines
766 B
TypeScript
import { Cart } from '@commerce/types'
|
|
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
|
import { normalizeCart } from '@framework/utils/normalize'
|
|
import { Checkout, Maybe, UserError } from '@framework/schema'
|
|
|
|
const checkoutToCart = (checkoutResponse?: {
|
|
checkout: Checkout
|
|
userErrors?: UserError[]
|
|
}): Maybe<Cart> => {
|
|
const checkout = checkoutResponse?.checkout
|
|
const userErrors = checkoutResponse?.userErrors
|
|
|
|
if (userErrors && userErrors.length) {
|
|
throw new ValidationError({
|
|
message: userErrors[0].message,
|
|
})
|
|
}
|
|
|
|
if (!checkout) {
|
|
throw new CommerceError({
|
|
message: 'Missing checkout details from response cart Response',
|
|
})
|
|
}
|
|
|
|
return normalizeCart(checkout)
|
|
}
|
|
|
|
export default checkoutToCart
|