mirror of
https://github.com/vercel/commerce.git
synced 2025-03-31 17:25:53 +00:00
Added handlers for the cart API
This commit is contained in:
parent
1c3714bf51
commit
f1b9ea605e
37
lib/bigcommerce/api/cart/handlers/add-item.ts
Normal file
37
lib/bigcommerce/api/cart/handlers/add-item.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
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
|
34
lib/bigcommerce/api/cart/handlers/remove-item.ts
Normal file
34
lib/bigcommerce/api/cart/handlers/remove-item.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
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
|
36
lib/bigcommerce/api/cart/handlers/update-item.ts
Normal file
36
lib/bigcommerce/api/cart/handlers/update-item.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import parseItem from '../../utils/parse-item'
|
||||||
|
import getCartCookie from '../../utils/get-cart-cookie'
|
||||||
|
import type { CartHandlers } from '..'
|
||||||
|
|
||||||
|
// Return current cart info
|
||||||
|
const updateItem: CartHandlers['updateItem'] = async ({
|
||||||
|
res,
|
||||||
|
body: { cartId, itemId, item },
|
||||||
|
config,
|
||||||
|
}) => {
|
||||||
|
if (!cartId || !itemId || !item) {
|
||||||
|
return res.status(400).json({
|
||||||
|
data: null,
|
||||||
|
errors: [{ message: 'Invalid request' }],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await config.storeApiFetch(
|
||||||
|
`/v3/carts/${cartId}/items/${itemId}`,
|
||||||
|
{
|
||||||
|
method: 'PUT',
|
||||||
|
body: JSON.stringify({
|
||||||
|
line_item: parseItem(item),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Update the cart cookie
|
||||||
|
res.setHeader(
|
||||||
|
'Set-Cookie',
|
||||||
|
getCartCookie(config.cartCookie, cartId, config.cartCookieMaxAge)
|
||||||
|
)
|
||||||
|
res.status(200).json({ data })
|
||||||
|
}
|
||||||
|
|
||||||
|
export default updateItem
|
@ -1,4 +1,3 @@
|
|||||||
import { serialize, CookieSerializeOptions } from 'cookie'
|
|
||||||
import isAllowedMethod from '../utils/is-allowed-method'
|
import isAllowedMethod from '../utils/is-allowed-method'
|
||||||
import createApiHandler, {
|
import createApiHandler, {
|
||||||
BigcommerceApiHandler,
|
BigcommerceApiHandler,
|
||||||
@ -6,6 +5,9 @@ import createApiHandler, {
|
|||||||
} from '../utils/create-api-handler'
|
} from '../utils/create-api-handler'
|
||||||
import { BigcommerceApiError } from '../utils/errors'
|
import { BigcommerceApiError } from '../utils/errors'
|
||||||
import getCart from './handlers/get-cart'
|
import getCart from './handlers/get-cart'
|
||||||
|
import addItem from './handlers/add-item'
|
||||||
|
import updateItem from './handlers/update-item'
|
||||||
|
import removeItem from './handlers/remove-item'
|
||||||
|
|
||||||
type Body<T> = Partial<T> | undefined
|
type Body<T> = Partial<T> | undefined
|
||||||
|
|
||||||
@ -44,6 +46,15 @@ export type Cart = {
|
|||||||
|
|
||||||
export type CartHandlers = {
|
export type CartHandlers = {
|
||||||
getCart: BigcommerceHandler<Cart, { cartId?: string }>
|
getCart: BigcommerceHandler<Cart, { cartId?: string }>
|
||||||
|
addItem: BigcommerceHandler<Cart, { cartId?: string } & Body<AddItemBody>>
|
||||||
|
updateItem: BigcommerceHandler<
|
||||||
|
Cart,
|
||||||
|
{ cartId?: string } & Body<UpdateItemBody>
|
||||||
|
>
|
||||||
|
removeItem: BigcommerceHandler<
|
||||||
|
Cart,
|
||||||
|
{ cartId?: string } & Body<RemoveItemBody>
|
||||||
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
const METHODS = ['GET', 'POST', 'PUT', 'DELETE']
|
const METHODS = ['GET', 'POST', 'PUT', 'DELETE']
|
||||||
@ -63,97 +74,26 @@ const cartApi: BigcommerceApiHandler<Cart, CartHandlers> = async (
|
|||||||
try {
|
try {
|
||||||
// Return current cart info
|
// Return current cart info
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
return await handlers['getCart']({ req, res, config, body: { cartId } })
|
const body = { cartId }
|
||||||
|
return await handlers['getCart']({ req, res, config, body })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create or add an item to the cart
|
// Create or add an item to the cart
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
const { item } = (req.body as Body<AddItemBody>) ?? {}
|
const body = { cartId, ...req.body }
|
||||||
|
return await handlers['addItem']({ req, res, config, body })
|
||||||
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)
|
|
||||||
)
|
|
||||||
|
|
||||||
return res.status(200).json({ data })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update item in cart
|
// Update item in cart
|
||||||
if (req.method === 'PUT') {
|
if (req.method === 'PUT') {
|
||||||
const { itemId, item } = (req.body as Body<UpdateItemBody>) ?? {}
|
const body = { cartId, ...req.body }
|
||||||
|
return await handlers['updateItem']({ req, res, config, body })
|
||||||
if (!cartId || !itemId || !item) {
|
|
||||||
return res.status(400).json({
|
|
||||||
data: null,
|
|
||||||
errors: [{ message: 'Invalid request' }],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const { data } = await config.storeApiFetch(
|
|
||||||
`/v3/carts/${cartId}/items/${itemId}`,
|
|
||||||
{
|
|
||||||
method: 'PUT',
|
|
||||||
body: JSON.stringify({
|
|
||||||
line_item: parseItem(item),
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Update the cart cookie
|
|
||||||
res.setHeader(
|
|
||||||
'Set-Cookie',
|
|
||||||
getCartCookie(config.cartCookie, cartId, config.cartCookieMaxAge)
|
|
||||||
)
|
|
||||||
|
|
||||||
return res.status(200).json({ data })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove an item from the cart
|
// Remove an item from the cart
|
||||||
if (req.method === 'DELETE') {
|
if (req.method === 'DELETE') {
|
||||||
const { itemId } = (req.body as Body<RemoveItemBody>) ?? {}
|
const body = { cartId, ...req.body }
|
||||||
|
return await handlers['removeItem']({ req, res, config, body })
|
||||||
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)
|
|
||||||
)
|
|
||||||
|
|
||||||
return res.status(200).json({ data })
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
@ -167,31 +107,6 @@ const cartApi: BigcommerceApiHandler<Cart, CartHandlers> = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCartCookie(name: string, cartId?: string, maxAge?: number) {
|
export const handlers = { getCart, addItem, updateItem, removeItem }
|
||||||
const options: CookieSerializeOptions =
|
|
||||||
cartId && maxAge
|
|
||||||
? {
|
|
||||||
maxAge,
|
|
||||||
expires: new Date(Date.now() + maxAge * 1000),
|
|
||||||
secure: process.env.NODE_ENV === 'production',
|
|
||||||
path: '/',
|
|
||||||
sameSite: 'lax',
|
|
||||||
}
|
|
||||||
: { maxAge: -1, path: '/' } // Removes the cookie
|
|
||||||
|
|
||||||
return serialize(name, cartId || '', options)
|
export default createApiHandler(cartApi, handlers)
|
||||||
}
|
|
||||||
|
|
||||||
const parseItem = (item: ItemBody) => ({
|
|
||||||
quantity: item.quantity,
|
|
||||||
product_id: item.productId,
|
|
||||||
variant_id: item.variantId,
|
|
||||||
})
|
|
||||||
|
|
||||||
const handlers = {
|
|
||||||
getCart,
|
|
||||||
}
|
|
||||||
|
|
||||||
const h = createApiHandler(cartApi, handlers)
|
|
||||||
|
|
||||||
export default h
|
|
||||||
|
9
lib/bigcommerce/api/utils/parse-item.ts
Normal file
9
lib/bigcommerce/api/utils/parse-item.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import type { ItemBody } from '../cart'
|
||||||
|
|
||||||
|
const parseItem = (item: ItemBody) => ({
|
||||||
|
quantity: item.quantity,
|
||||||
|
product_id: item.productId,
|
||||||
|
variant_id: item.variantId,
|
||||||
|
})
|
||||||
|
|
||||||
|
export default parseItem
|
Loading…
x
Reference in New Issue
Block a user