mirror of
https://github.com/vercel/commerce.git
synced 2025-09-03 06:20:15 +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
36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
import { normalizeCart } from '../../../lib/normalize'
|
|
import { BigcommerceApiError } from '../../utils/errors'
|
|
import getCartCookie from '../../utils/get-cart-cookie'
|
|
import type { BigcommerceCart } from '../../../types/cart'
|
|
import type { CartEndpoint } from '.'
|
|
|
|
// Return current cart info
|
|
const getCart: CartEndpoint['handlers']['getCart'] = async ({
|
|
res,
|
|
body: { cartId },
|
|
config,
|
|
}) => {
|
|
let result: { data?: BigcommerceCart } = {}
|
|
|
|
if (cartId) {
|
|
try {
|
|
result = await config.storeApiFetch(
|
|
`/v3/carts/${cartId}?include=line_items.physical_items.options,line_items.digital_items.options`
|
|
)
|
|
} catch (error) {
|
|
if (error instanceof BigcommerceApiError && 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: result.data ? normalizeCart(result.data) : null,
|
|
})
|
|
}
|
|
|
|
export default getCart
|