mirror of
https://github.com/vercel/commerce.git
synced 2025-06-20 06:01:21 +00:00
36 lines
961 B
TypeScript
36 lines
961 B
TypeScript
import { Cart } from '../types'
|
|
import { CommerceError } from '@commerce/utils/errors'
|
|
|
|
import { CheckoutLinesAdd, CheckoutLinesUpdate, CheckoutCreate, CheckoutError, Checkout, Maybe } from '../schema'
|
|
|
|
import { normalizeCart } from './normalize'
|
|
import throwUserErrors from './throw-user-errors'
|
|
|
|
export type CheckoutQuery = {
|
|
checkout: Checkout
|
|
errors?: Array<CheckoutError>
|
|
}
|
|
|
|
export type CheckoutPayload = CheckoutLinesAdd | CheckoutLinesUpdate | CheckoutCreate | CheckoutQuery
|
|
|
|
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
|
if (!checkoutPayload) {
|
|
throw new CommerceError({
|
|
message: 'Missing checkout payload from response',
|
|
})
|
|
}
|
|
|
|
const checkout = checkoutPayload?.checkout
|
|
throwUserErrors(checkoutPayload?.errors)
|
|
|
|
if (!checkout) {
|
|
throw new CommerceError({
|
|
message: 'Missing checkout object from response',
|
|
})
|
|
}
|
|
|
|
return normalizeCart(checkout)
|
|
}
|
|
|
|
export default checkoutToCart
|