mirror of
https://github.com/vercel/commerce.git
synced 2025-04-25 12:27:50 +00:00
* Add ordercloud provider * Fix provider errors * Make submit checkout optional * Make submit checkout optional * Remove nullables when creating endpoint type * Update readme * Log checkout error * Log error * Save token to cookie * Update fetch rest * Use token at checkout Co-authored-by: Luis Alvarez <luis@vercel.com>
66 lines
1.5 KiB
TypeScript
66 lines
1.5 KiB
TypeScript
import type { OrdercloudLineItem } from '../../../types/cart'
|
|
import type { CartEndpoint } from '.'
|
|
|
|
import { serialize } from 'cookie'
|
|
|
|
import { formatCart } from '../../utils/cart'
|
|
|
|
// Return current cart info
|
|
const getCart: CartEndpoint['handlers']['getCart'] = async ({
|
|
req,
|
|
res,
|
|
body: { cartId },
|
|
config: { restBuyerFetch, cartCookie, tokenCookie },
|
|
}) => {
|
|
if (!cartId) {
|
|
return res.status(400).json({
|
|
data: null,
|
|
errors: [{ message: 'Invalid request' }],
|
|
})
|
|
}
|
|
|
|
try {
|
|
// Get token from cookies
|
|
const token = req.cookies[tokenCookie]
|
|
|
|
// Get cart
|
|
const cart = await restBuyerFetch(
|
|
'GET',
|
|
`/orders/Outgoing/${cartId}`,
|
|
null,
|
|
{ token }
|
|
)
|
|
|
|
// Get line items
|
|
const lineItems = await restBuyerFetch(
|
|
'GET',
|
|
`/orders/Outgoing/${cartId}/lineitems`,
|
|
null,
|
|
{ token }
|
|
).then((response: { Items: OrdercloudLineItem[] }) => response.Items)
|
|
|
|
// Format cart
|
|
const formattedCart = formatCart(cart, lineItems)
|
|
|
|
// Return cart and errors
|
|
res.status(200).json({ data: formattedCart, errors: [] })
|
|
} catch (error) {
|
|
// Reset cart and token cookie
|
|
res.setHeader('Set-Cookie', [
|
|
serialize(cartCookie, cartId, {
|
|
maxAge: -1,
|
|
path: '/',
|
|
}),
|
|
serialize(tokenCookie, cartId, {
|
|
maxAge: -1,
|
|
path: '/',
|
|
}),
|
|
])
|
|
|
|
// Return empty cart
|
|
res.status(200).json({ data: null, errors: [] })
|
|
}
|
|
}
|
|
|
|
export default getCart
|