From 61f12921613d901458dc10384ed77f05b1f3dae2 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Mon, 15 Mar 2021 12:41:30 -0600 Subject: [PATCH] Updated the default cart endpoint --- framework/commerce/api/endpoints/cart.ts | 31 ++++++++++++------------ framework/commerce/api/utils/errors.ts | 22 +++++++++++++++++ 2 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 framework/commerce/api/utils/errors.ts diff --git a/framework/commerce/api/endpoints/cart.ts b/framework/commerce/api/endpoints/cart.ts index e7570c34c..03261e1ec 100644 --- a/framework/commerce/api/endpoints/cart.ts +++ b/framework/commerce/api/endpoints/cart.ts @@ -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 = 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 = 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 }] }) diff --git a/framework/commerce/api/utils/errors.ts b/framework/commerce/api/utils/errors.ts new file mode 100644 index 000000000..6f9ecce0c --- /dev/null +++ b/framework/commerce/api/utils/errors.ts @@ -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' + } +}