mirror of
https://github.com/vercel/commerce.git
synced 2025-08-16 13:51:24 +00:00
.vscode
assets
components
config
framework
bigcommerce
api
definitions
endpoints
cart
add-item.ts
get-cart.ts
index.ts
remove-item.ts
update-item.ts
catalog
checkout
customer
login
logout
signup
wishlist
fragments
operations
utils
index.ts
auth
cart
checkout
customer
lib
product
scripts
types
wishlist
.env.template
README.md
commerce.config.json
fetcher.ts
index.tsx
next.config.js
provider.ts
schema.d.ts
schema.graphql
commerce
commercejs
commercelayer
kibocommerce
local
ordercloud
saleor
shopify
spree
swell
vendure
lib
pages
public
.editorconfig
.env.template
.eslintrc
.gitignore
.prettierignore
.prettierrc
README.md
codegen.bigcommerce.json
codegen.json
commerce.config.json
global.d.ts
license.md
next-env.d.ts
next.config.js
package-lock.json
package.json
postcss.config.js
swell-js.d.ts
tailwind.config.js
tsconfig.json
* Update add-item.ts include digital items * Update add-item.ts include digital items Co-authored-by: Gonzalo Pozzo <gonzalo.pozzo4@gmail.com>
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { normalizeCart } from '../../../lib/normalize'
|
|
import { parseCartItem } from '../../utils/parse-item'
|
|
import getCartCookie from '../../utils/get-cart-cookie'
|
|
import type { CartEndpoint } from '.'
|
|
|
|
const addItem: CartEndpoint['handlers']['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: [parseCartItem(item)],
|
|
...(!cartId && config.storeChannelId
|
|
? { channel_id: config.storeChannelId }
|
|
: {}),
|
|
}),
|
|
}
|
|
const { data } = cartId
|
|
? await config.storeApiFetch(
|
|
`/v3/carts/${cartId}/items?include=line_items.physical_items.options,line_items.digital_items.options`,
|
|
options
|
|
)
|
|
: await config.storeApiFetch(
|
|
'/v3/carts?include=line_items.physical_items.options,line_items.digital_items.options',
|
|
options
|
|
)
|
|
|
|
// Create or update the cart cookie
|
|
res.setHeader(
|
|
'Set-Cookie',
|
|
getCartCookie(config.cartCookie, data.id, config.cartCookieMaxAge)
|
|
)
|
|
res.status(200).json({ data: normalizeCart(data) })
|
|
}
|
|
|
|
export default addItem
|