mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Updated the default cart endpoint
This commit is contained in:
parent
647e8aaf4c
commit
61f1292161
@ -1,18 +1,17 @@
|
||||
import type { APIEndpoint, APIHandler } from '../utils/types'
|
||||
import isAllowedMethod from '../utils/is-allowed-method'
|
||||
|
||||
import cn from 'classnames'
|
||||
import type { APIEndpoint } from '../utils/types'
|
||||
import { CommerceAPIError } from '../utils/errors'
|
||||
import isAllowedOperation from '../utils/is-allowed-operation'
|
||||
import type { APIProvider, CartHandlers } from '..'
|
||||
|
||||
cn({ yo: true })
|
||||
|
||||
const METHODS = ['GET', 'POST', 'PUT', 'DELETE']
|
||||
|
||||
const cartApi: APIEndpoint<APIProvider, CartHandlers> = async (ctx) => {
|
||||
const { req, res, handlers, config } = ctx
|
||||
|
||||
if (
|
||||
!isAllowedOperation(ctx.req, ctx.res, {
|
||||
GET: ctx.handlers['getCart'],
|
||||
!isAllowedOperation(req, res, {
|
||||
GET: handlers['getCart'],
|
||||
POST: handlers['addItem'],
|
||||
PUT: handlers['updateItem'],
|
||||
DELETE: handlers['removeItem'],
|
||||
})
|
||||
) {
|
||||
return
|
||||
@ -25,32 +24,32 @@ const cartApi: APIEndpoint<APIProvider, CartHandlers> = async (ctx) => {
|
||||
// Return current cart info
|
||||
if (req.method === 'GET') {
|
||||
const body = { cartId }
|
||||
return await handlers['getCart']({ req, res, config, body })
|
||||
return await handlers['getCart']({ ...ctx, 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 })
|
||||
return await handlers['addItem']({ ...ctx, body })
|
||||
}
|
||||
|
||||
// Update item in cart
|
||||
if (req.method === 'PUT') {
|
||||
const body = { ...req.body, cartId }
|
||||
return await handlers['updateItem']({ req, res, config, body })
|
||||
return await handlers['updateItem']({ ...ctx, body })
|
||||
}
|
||||
|
||||
// Remove an item from the cart
|
||||
if (req.method === 'DELETE') {
|
||||
const body = { ...req.body, cartId }
|
||||
return await handlers['removeItem']({ req, res, config, body })
|
||||
return await handlers['removeItem']({ ...ctx, body })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
|
||||
const message =
|
||||
error instanceof BigcommerceApiError
|
||||
? 'An unexpected error ocurred with the Bigcommerce API'
|
||||
error instanceof CommerceAPIError
|
||||
? 'An unexpected error ocurred with the Commerce API'
|
||||
: 'An unexpected error ocurred'
|
||||
|
||||
res.status(500).json({ data: null, errors: [{ message }] })
|
||||
|
22
framework/commerce/api/utils/errors.ts
Normal file
22
framework/commerce/api/utils/errors.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import type { Response } from '@vercel/fetch'
|
||||
|
||||
export class CommerceAPIError extends Error {
|
||||
status: number
|
||||
res: Response
|
||||
data: any
|
||||
|
||||
constructor(msg: string, res: Response, data?: any) {
|
||||
super(msg)
|
||||
this.name = 'CommerceApiError'
|
||||
this.status = res.status
|
||||
this.res = res
|
||||
this.data = data
|
||||
}
|
||||
}
|
||||
|
||||
export class CommerceNetworkError extends Error {
|
||||
constructor(msg: string) {
|
||||
super(msg)
|
||||
this.name = 'CommerceNetworkError'
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user