forked from crowetic/commerce
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
38 lines
912 B
TypeScript
38 lines
912 B
TypeScript
import parseItem from '../../utils/parse-item'
|
|
import getCartCookie from '../../utils/get-cart-cookie'
|
|
import type { CartHandlers } from '..'
|
|
|
|
// Return current cart info
|
|
const addItem: CartHandlers['addItem'] = async ({
|
|
res,
|
|
body: { cartId, item },
|
|
config,
|
|
}) => {
|
|
if (!item) {
|
|
return res.status(400).json({
|
|
data: null,
|
|
errors: [{ message: 'Missing item' }],
|
|
})
|
|
}
|
|
if (!item.quantity) item.quantity = 1
|
|
|
|
const options = {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
line_items: [parseItem(item)],
|
|
}),
|
|
}
|
|
const { data } = cartId
|
|
? await config.storeApiFetch(`/v3/carts/${cartId}/items`, options)
|
|
: await config.storeApiFetch('/v3/carts', options)
|
|
|
|
// Create or update the cart cookie
|
|
res.setHeader(
|
|
'Set-Cookie',
|
|
getCartCookie(config.cartCookie, data.id, config.cartCookieMaxAge)
|
|
)
|
|
res.status(200).json({ data })
|
|
}
|
|
|
|
export default addItem
|