From 779c6bed2d8b76ee0e826c2f83b27647a3384495 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Wed, 31 Mar 2021 22:57:36 -0600 Subject: [PATCH] Updated cart endpoint --- framework/commerce/api/endpoints/cart.ts | 22 +++++++++---------- .../api/utils/is-allowed-operation.ts | 6 ++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/framework/commerce/api/endpoints/cart.ts b/framework/commerce/api/endpoints/cart.ts index 30e2ff72d..6b98335ca 100644 --- a/framework/commerce/api/endpoints/cart.ts +++ b/framework/commerce/api/endpoints/cart.ts @@ -1,19 +1,19 @@ -import type { APIEndpoint } from '../utils/types' +import type { CartSchema } from '../../types/cart' import { CommerceAPIError } from '../utils/errors' import isAllowedOperation from '../utils/is-allowed-operation' -import type { GetAPISchema, CartSchema } from '..' +import type { GetAPISchema } from '..' const cartApi: GetAPISchema['endpoint']['handler'] = async ( ctx ) => { - const { req, res, handlers, config } = ctx + const { req, res, operations, config } = ctx if ( !isAllowedOperation(req, res, { - GET: handlers['getCart'], - POST: handlers['addItem'], - PUT: handlers['updateItem'], - DELETE: handlers['removeItem'], + GET: operations['getCart'], + POST: operations['addItem'], + PUT: operations['updateItem'], + DELETE: operations['removeItem'], }) ) { return @@ -27,25 +27,25 @@ const cartApi: GetAPISchema['endpoint']['handler'] = async ( // Return current cart info if (req.method === 'GET') { const body = { cartId } - return await handlers['getCart']({ ...ctx, body }) + return await operations['getCart']({ ...ctx, body }) } // Create or add an item to the cart if (req.method === 'POST') { const body = { ...req.body, cartId } - return await handlers['addItem']({ ...ctx, body }) + return await operations['addItem']({ ...ctx, body }) } // Update item in cart if (req.method === 'PUT') { const body = { ...req.body, cartId } - return await handlers['updateItem']({ ...ctx, body }) + return await operations['updateItem']({ ...ctx, body }) } // Remove an item from the cart if (req.method === 'DELETE') { const body = { ...req.body, cartId } - return await handlers['removeItem']({ ...ctx, body }) + return await operations['removeItem']({ ...ctx, body }) } } catch (error) { console.error(error) diff --git a/framework/commerce/api/utils/is-allowed-operation.ts b/framework/commerce/api/utils/is-allowed-operation.ts index 6c8f89d00..f507781bf 100644 --- a/framework/commerce/api/utils/is-allowed-operation.ts +++ b/framework/commerce/api/utils/is-allowed-operation.ts @@ -5,11 +5,11 @@ import { APIHandler } from './types' export default function isAllowedOperation( req: NextApiRequest, res: NextApiResponse, - allowedHandlers: { [k in HTTP_METHODS]?: APIHandler } + allowedOperations: { [k in HTTP_METHODS]?: APIHandler } ) { - const methods = Object.keys(allowedHandlers) as HTTP_METHODS[] + const methods = Object.keys(allowedOperations) as HTTP_METHODS[] const allowedMethods = methods.reduce((arr, method) => { - if (allowedHandlers[method]) { + if (allowedOperations[method]) { arr.push(method) } return arr