mirror of
https://github.com/vercel/commerce.git
synced 2025-06-18 21:21:21 +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 type { APIEndpoint } from '../utils/types'
|
||||||
import isAllowedMethod from '../utils/is-allowed-method'
|
import { CommerceAPIError } from '../utils/errors'
|
||||||
|
|
||||||
import cn from 'classnames'
|
|
||||||
import isAllowedOperation from '../utils/is-allowed-operation'
|
import isAllowedOperation from '../utils/is-allowed-operation'
|
||||||
import type { APIProvider, CartHandlers } from '..'
|
import type { APIProvider, CartHandlers } from '..'
|
||||||
|
|
||||||
cn({ yo: true })
|
|
||||||
|
|
||||||
const METHODS = ['GET', 'POST', 'PUT', 'DELETE']
|
|
||||||
|
|
||||||
const cartApi: APIEndpoint<APIProvider, CartHandlers> = async (ctx) => {
|
const cartApi: APIEndpoint<APIProvider, CartHandlers> = async (ctx) => {
|
||||||
|
const { req, res, handlers, config } = ctx
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!isAllowedOperation(ctx.req, ctx.res, {
|
!isAllowedOperation(req, res, {
|
||||||
GET: ctx.handlers['getCart'],
|
GET: handlers['getCart'],
|
||||||
|
POST: handlers['addItem'],
|
||||||
|
PUT: handlers['updateItem'],
|
||||||
|
DELETE: handlers['removeItem'],
|
||||||
})
|
})
|
||||||
) {
|
) {
|
||||||
return
|
return
|
||||||
@ -25,32 +24,32 @@ const cartApi: APIEndpoint<APIProvider, CartHandlers> = async (ctx) => {
|
|||||||
// Return current cart info
|
// Return current cart info
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
const body = { cartId }
|
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
|
// Create or add an item to the cart
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
const body = { ...req.body, cartId }
|
const body = { ...req.body, cartId }
|
||||||
return await handlers['addItem']({ req, res, config, body })
|
return await handlers['addItem']({ ...ctx, body })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update item in cart
|
// Update item in cart
|
||||||
if (req.method === 'PUT') {
|
if (req.method === 'PUT') {
|
||||||
const body = { ...req.body, cartId }
|
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
|
// Remove an item from the cart
|
||||||
if (req.method === 'DELETE') {
|
if (req.method === 'DELETE') {
|
||||||
const body = { ...req.body, cartId }
|
const body = { ...req.body, cartId }
|
||||||
return await handlers['removeItem']({ req, res, config, body })
|
return await handlers['removeItem']({ ...ctx, body })
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
|
||||||
const message =
|
const message =
|
||||||
error instanceof BigcommerceApiError
|
error instanceof CommerceAPIError
|
||||||
? 'An unexpected error ocurred with the Bigcommerce API'
|
? 'An unexpected error ocurred with the Commerce API'
|
||||||
: 'An unexpected error ocurred'
|
: 'An unexpected error ocurred'
|
||||||
|
|
||||||
res.status(500).json({ data: null, errors: [{ message }] })
|
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