4
0
forked from crowetic/commerce
Files
assets
components
config
lib
bigcommerce
api
cart
handlers
add-item.ts
get-cart.ts
remove-item.ts
update-item.ts
index.ts
catalog
customers
definitions
fragments
operations
utils
wishlist
checkout.ts
index.ts
cart
products
scripts
wishlist
index.tsx
schema.d.ts
schema.graphql
use-customer.tsx
use-login.tsx
use-logout.tsx
use-price.tsx
use-signup.tsx
commerce
browser.ts
colors.ts
logger.ts
range-map.ts
to-pixels.ts
pages
public
utils
.gitignore
.prettierignore
README.md
codegen.json
global.d.ts
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
commerce/lib/bigcommerce/api/cart/handlers/remove-item.ts
2020-10-11 00:58:57 -05:00

35 lines
880 B
TypeScript

import getCartCookie from '../../utils/get-cart-cookie'
import type { CartHandlers } from '..'
// Return current cart info
const removeItem: CartHandlers['removeItem'] = async ({
res,
body: { cartId, itemId },
config,
}) => {
if (!cartId || !itemId) {
return res.status(400).json({
data: null,
errors: [{ message: 'Invalid request' }],
})
}
const result = await config.storeApiFetch<{ data: any } | null>(
`/v3/carts/${cartId}/items/${itemId}`,
{ method: 'DELETE' }
)
const data = result?.data ?? null
res.setHeader(
'Set-Cookie',
data
? // Update the cart cookie
getCartCookie(config.cartCookie, cartId, config.cartCookieMaxAge)
: // Remove the cart cookie if the cart was removed (empty items)
getCartCookie(config.cartCookie)
)
res.status(200).json({ data })
}
export default removeItem