mirror of
https://github.com/vercel/commerce.git
synced 2025-07-29 21:21:23 +00:00
.vscode
assets
components
config
docs
framework
bigcommerce
api
cart
handlers
index.ts
catalog
customers
definitions
fragments
utils
wishlist
checkout.ts
index.ts
auth
cart
common
customer
lib
product
scripts
wishlist
.env.template
README.md
commerce.config.json
fetcher.ts
index.tsx
next.config.js
provider.ts
schema.d.ts
schema.graphql
types.ts
commerce
shopify
lib
pages
public
.editorconfig
.env.template
.gitignore
.prettierignore
.prettierrc
README.md
codegen.json
commerce.config.json
global.d.ts
license.md
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
79 lines
2.3 KiB
TypeScript
79 lines
2.3 KiB
TypeScript
import isAllowedMethod from '../utils/is-allowed-method'
|
|
import createApiHandler, {
|
|
BigcommerceApiHandler,
|
|
BigcommerceHandler,
|
|
} from '../utils/create-api-handler'
|
|
import { BigcommerceApiError } from '../utils/errors'
|
|
import getCart from './handlers/get-cart'
|
|
import addItem from './handlers/add-item'
|
|
import updateItem from './handlers/update-item'
|
|
import removeItem from './handlers/remove-item'
|
|
import type {
|
|
BigcommerceCart,
|
|
GetCartHandlerBody,
|
|
AddCartItemHandlerBody,
|
|
UpdateCartItemHandlerBody,
|
|
RemoveCartItemHandlerBody,
|
|
} from '../../types'
|
|
|
|
export type CartHandlers = {
|
|
getCart: BigcommerceHandler<BigcommerceCart, GetCartHandlerBody>
|
|
addItem: BigcommerceHandler<BigcommerceCart, AddCartItemHandlerBody>
|
|
updateItem: BigcommerceHandler<BigcommerceCart, UpdateCartItemHandlerBody>
|
|
removeItem: BigcommerceHandler<BigcommerceCart, RemoveCartItemHandlerBody>
|
|
}
|
|
|
|
const METHODS = ['GET', 'POST', 'PUT', 'DELETE']
|
|
|
|
// TODO: a complete implementation should have schema validation for `req.body`
|
|
const cartApi: BigcommerceApiHandler<BigcommerceCart, CartHandlers> = async (
|
|
req,
|
|
res,
|
|
config,
|
|
handlers
|
|
) => {
|
|
if (!isAllowedMethod(req, res, METHODS)) return
|
|
|
|
const { cookies } = req
|
|
const cartId = cookies[config.cartCookie]
|
|
|
|
try {
|
|
// Return current cart info
|
|
if (req.method === 'GET') {
|
|
const body = { cartId }
|
|
return await handlers['getCart']({ req, res, config, body })
|
|
}
|
|
|
|
// Create or add an item to the cart
|
|
if (req.method === 'POST') {
|
|
const body = { ...req.body, cartId }
|
|
return await handlers['addItem']({ req, res, config, body })
|
|
}
|
|
|
|
// Update item in cart
|
|
if (req.method === 'PUT') {
|
|
const body = { ...req.body, cartId }
|
|
return await handlers['updateItem']({ req, res, config, body })
|
|
}
|
|
|
|
// Remove an item from the cart
|
|
if (req.method === 'DELETE') {
|
|
const body = { ...req.body, cartId }
|
|
return await handlers['removeItem']({ req, res, config, body })
|
|
}
|
|
} catch (error) {
|
|
console.error(error)
|
|
|
|
const message =
|
|
error instanceof BigcommerceApiError
|
|
? 'An unexpected error ocurred with the Bigcommerce API'
|
|
: 'An unexpected error ocurred'
|
|
|
|
res.status(500).json({ data: null, errors: [{ message }] })
|
|
}
|
|
}
|
|
|
|
export const handlers = { getCart, addItem, updateItem, removeItem }
|
|
|
|
export default createApiHandler(cartApi, handlers, {})
|