Gérard Le Cloerec a5de31ae17 feat: disable Wishlist
chore: setup commerce & next config
fix: replace all call to bigcommerce from aquilacms provider
feat add validation to input in signup
2021-04-07 08:46:40 +02:00

41 lines
1.1 KiB
TypeScript

import type { AquilacmsCart } from '../../../types'
import { AquilacmsApiError } from '../../utils/errors'
import getCartCookie from '../../utils/get-cart-cookie'
import type { CartHandlers } from '..'
import { normalizeCart } from '../../../lib/normalize'
// Return current cart info
const getCart: CartHandlers['getCart'] = async ({
res,
body: { cartId },
config,
}) => {
if (cartId) {
try {
let result: AquilacmsCart = await config.storeApiFetch(
`/v2/cart/${cartId}`,
{
method: 'POST',
body: JSON.stringify({
lang: 'en',
PostBody: {
populate: ['items.id'],
},
}),
}
)
return res.status(200).json({ data: normalizeCart(result) })
} catch (error) {
if (error instanceof AquilacmsApiError && error.status === 404) {
// Remove the cookie if it exists but the cart wasn't found
res.setHeader('Set-Cookie', getCartCookie(config.cartCookie))
} else {
throw error
}
}
}
res.status(200).json({ data: null })
}
export default getCart