From 2281490342bfc1c50b4ad65d0a1291893198d4ab Mon Sep 17 00:00:00 2001
From: Luis Alvarez <luis@vercel.com>
Date: Fri, 2 Apr 2021 12:05:01 -0600
Subject: [PATCH] Removed endpoints folder

---
 .../api/endpoints/cart/handlers/add-item.ts   | 45 -----------
 .../api/endpoints/cart/handlers/get-cart.ts   | 32 --------
 .../endpoints/cart/handlers/remove-item.ts    | 33 --------
 .../endpoints/cart/handlers/update-item.ts    | 35 ---------
 .../bigcommerce/api/endpoints/cart/index.ts   | 78 -------------------
 5 files changed, 223 deletions(-)
 delete mode 100644 framework/bigcommerce/api/endpoints/cart/handlers/add-item.ts
 delete mode 100644 framework/bigcommerce/api/endpoints/cart/handlers/get-cart.ts
 delete mode 100644 framework/bigcommerce/api/endpoints/cart/handlers/remove-item.ts
 delete mode 100644 framework/bigcommerce/api/endpoints/cart/handlers/update-item.ts
 delete mode 100644 framework/bigcommerce/api/endpoints/cart/index.ts

diff --git a/framework/bigcommerce/api/endpoints/cart/handlers/add-item.ts b/framework/bigcommerce/api/endpoints/cart/handlers/add-item.ts
deleted file mode 100644
index c47e72cdb..000000000
--- a/framework/bigcommerce/api/endpoints/cart/handlers/add-item.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import { parseCartItem } from '../../utils/parse-item'
-import getCartCookie from '../../utils/get-cart-cookie'
-import type { CartHandlers } from '..'
-
-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: [parseCartItem(item)],
-      ...(!cartId && config.storeChannelId
-        ? { channel_id: config.storeChannelId }
-        : {}),
-    }),
-  }
-  const { data } = cartId
-    ? await config.storeApiFetch(
-        `/v3/carts/${cartId}/items?include=line_items.physical_items.options`,
-        options
-      )
-    : await config.storeApiFetch(
-        '/v3/carts?include=line_items.physical_items.options',
-        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
diff --git a/framework/bigcommerce/api/endpoints/cart/handlers/get-cart.ts b/framework/bigcommerce/api/endpoints/cart/handlers/get-cart.ts
deleted file mode 100644
index 890ac9997..000000000
--- a/framework/bigcommerce/api/endpoints/cart/handlers/get-cart.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import type { BigcommerceCart } from '../../../types'
-import { BigcommerceApiError } from '../../utils/errors'
-import getCartCookie from '../../utils/get-cart-cookie'
-import type { CartHandlers } from '../'
-
-// Return current cart info
-const getCart: CartHandlers['getCart'] = async ({
-  res,
-  body: { cartId },
-  config,
-}) => {
-  let result: { data?: BigcommerceCart } = {}
-
-  if (cartId) {
-    try {
-      result = await config.storeApiFetch(
-        `/v3/carts/${cartId}?include=line_items.physical_items.options`
-      )
-    } catch (error) {
-      if (error instanceof BigcommerceApiError && error.status === 404) {
-        // Remove the cookie if it exists but the cart wasn't found
-        res.setHeader('Set-Cookie', getCartCookie(config.cartCookie))
-      } else {
-        throw error
-      }
-    }
-  }
-
-  res.status(200).json({ data: result.data ?? null })
-}
-
-export default getCart
diff --git a/framework/bigcommerce/api/endpoints/cart/handlers/remove-item.ts b/framework/bigcommerce/api/endpoints/cart/handlers/remove-item.ts
deleted file mode 100644
index c09848948..000000000
--- a/framework/bigcommerce/api/endpoints/cart/handlers/remove-item.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import getCartCookie from '../../utils/get-cart-cookie'
-import type { CartHandlers } from '..'
-
-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}?include=line_items.physical_items.options`,
-    { 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
diff --git a/framework/bigcommerce/api/endpoints/cart/handlers/update-item.ts b/framework/bigcommerce/api/endpoints/cart/handlers/update-item.ts
deleted file mode 100644
index 27b74ca20..000000000
--- a/framework/bigcommerce/api/endpoints/cart/handlers/update-item.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-import { parseCartItem } from '../../utils/parse-item'
-import getCartCookie from '../../utils/get-cart-cookie'
-import type { CartHandlers } from '..'
-
-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}?include=line_items.physical_items.options`,
-    {
-      method: 'PUT',
-      body: JSON.stringify({
-        line_item: parseCartItem(item),
-      }),
-    }
-  )
-
-  // Update the cart cookie
-  res.setHeader(
-    'Set-Cookie',
-    getCartCookie(config.cartCookie, cartId, config.cartCookieMaxAge)
-  )
-  res.status(200).json({ data })
-}
-
-export default updateItem
diff --git a/framework/bigcommerce/api/endpoints/cart/index.ts b/framework/bigcommerce/api/endpoints/cart/index.ts
deleted file mode 100644
index 4ee668895..000000000
--- a/framework/bigcommerce/api/endpoints/cart/index.ts
+++ /dev/null
@@ -1,78 +0,0 @@
-import isAllowedMethod from '../utils/is-allowed-method'
-import createApiHandler, {
-  BigcommerceApiHandler,
-  BigcommerceHandler,
-} from '../utils/create-api-handler'
-import { BigcommerceApiError } from '../utils/errors'
-import getCart from './handlers/get-cart'
-import addItem from './handlers/add-item'
-import updateItem from './handlers/update-item'
-import removeItem from './handlers/remove-item'
-import type {
-  BigcommerceCart,
-  GetCartHandlerBody,
-  AddCartItemHandlerBody,
-  UpdateCartItemHandlerBody,
-  RemoveCartItemHandlerBody,
-} from '../../types'
-
-export type CartHandlers = {
-  getCart: BigcommerceHandler<BigcommerceCart, GetCartHandlerBody>
-  addItem: BigcommerceHandler<BigcommerceCart, AddCartItemHandlerBody>
-  updateItem: BigcommerceHandler<BigcommerceCart, UpdateCartItemHandlerBody>
-  removeItem: BigcommerceHandler<BigcommerceCart, RemoveCartItemHandlerBody>
-}
-
-const METHODS = ['GET', 'POST', 'PUT', 'DELETE']
-
-// TODO: a complete implementation should have schema validation for `req.body`
-const cartApi: BigcommerceApiHandler<BigcommerceCart, CartHandlers> = async (
-  req,
-  res,
-  config,
-  handlers
-) => {
-  if (!isAllowedMethod(req, res, METHODS)) return
-
-  const { cookies } = req
-  const cartId = cookies[config.cartCookie]
-
-  try {
-    // Return current cart info
-    if (req.method === 'GET') {
-      const body = { cartId }
-      return await handlers['getCart']({ req, res, config, 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 })
-    }
-
-    // Update item in cart
-    if (req.method === 'PUT') {
-      const body = { ...req.body, cartId }
-      return await handlers['updateItem']({ req, res, config, body })
-    }
-
-    // Remove an item from the cart
-    if (req.method === 'DELETE') {
-      const body = { ...req.body, cartId }
-      return await handlers['removeItem']({ req, res, config, body })
-    }
-  } catch (error) {
-    console.error(error)
-
-    const message =
-      error instanceof BigcommerceApiError
-        ? 'An unexpected error ocurred with the Bigcommerce API'
-        : 'An unexpected error ocurred'
-
-    res.status(500).json({ data: null, errors: [{ message }] })
-  }
-}
-
-export const handlers = { getCart, addItem, updateItem, removeItem }
-
-export default createApiHandler(cartApi, handlers, {})