4
0
forked from crowetic/commerce
commerce/framework/saleor/utils/checkout-to-cart.ts
2021-11-25 09:17:13 -03:00

49 lines
1.0 KiB
TypeScript

import { Cart } from '../types'
import { CommerceError } from '@commerce/utils/errors'
import {
CheckoutLinesAdd,
CheckoutLinesUpdate,
CheckoutCreate,
CheckoutError,
Checkout,
Maybe,
CheckoutLineDelete,
} 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
| CheckoutLineDelete
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