From bb321e2237e68ddd7b327615e241741d2f11d4f6 Mon Sep 17 00:00:00 2001 From: Greg Hoskin Date: Wed, 9 Jun 2021 09:29:58 -0700 Subject: [PATCH] update checkout --- framework/swell/api/checkout/index.ts | 20 ------------- .../swell/api/endpoints/checkout/index.ts | 30 +++++++++++++++++++ .../swell/api/operations/get-all-products.ts | 3 +- framework/swell/cart/utils/checkout-create.ts | 2 +- 4 files changed, 33 insertions(+), 22 deletions(-) delete mode 100644 framework/swell/api/checkout/index.ts create mode 100644 framework/swell/api/endpoints/checkout/index.ts diff --git a/framework/swell/api/checkout/index.ts b/framework/swell/api/checkout/index.ts deleted file mode 100644 index 0759e2abc..000000000 --- a/framework/swell/api/checkout/index.ts +++ /dev/null @@ -1,20 +0,0 @@ -import createApiHandler, { SwellApiHandler } from '../utils/create-api-handler' - -import { SWELL_CHECKOUT_URL_COOKIE } from '../../const' - -import { getConfig } from '..' - -const checkoutApi: SwellApiHandler = async (req, res, config) => { - config = getConfig() - - const { cookies } = req - const checkoutUrl = cookies[SWELL_CHECKOUT_URL_COOKIE] - - if (checkoutUrl) { - res.redirect(checkoutUrl) - } else { - res.redirect('/cart') - } -} - -export default createApiHandler(checkoutApi, {}, {}) diff --git a/framework/swell/api/endpoints/checkout/index.ts b/framework/swell/api/endpoints/checkout/index.ts new file mode 100644 index 000000000..ab17a3767 --- /dev/null +++ b/framework/swell/api/endpoints/checkout/index.ts @@ -0,0 +1,30 @@ +import { CommerceAPI, createEndpoint, GetAPISchema } from '@commerce/api' +import { CheckoutSchema } from '@commerce/types/checkout' +import { SWELL_CHECKOUT_URL_COOKIE } from '../../../const' +import checkoutEndpoint from '@commerce/api/endpoints/checkout' + +const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({ + req, + res, + config, +}) => { + const { cookies } = req + const checkoutUrl = cookies[SWELL_CHECKOUT_URL_COOKIE] + + if (checkoutUrl) { + res.redirect(checkoutUrl) + } else { + res.redirect('/cart') + } +} +export const handlers: CheckoutEndpoint['handlers'] = { checkout } + +export type CheckoutAPI = GetAPISchema +export type CheckoutEndpoint = CheckoutAPI['endpoint'] + +const checkoutApi = createEndpoint({ + handler: checkoutEndpoint, + handlers, +}) + +export default checkoutApi diff --git a/framework/swell/api/operations/get-all-products.ts b/framework/swell/api/operations/get-all-products.ts index 9c4c1534a..7e8e99a5c 100644 --- a/framework/swell/api/operations/get-all-products.ts +++ b/framework/swell/api/operations/get-all-products.ts @@ -17,6 +17,7 @@ export default function getAllProductsOperation({ async function getAllProducts({ config: cfg, + variables = { first: 250 }, }: { query?: string variables?: ProductVariables @@ -26,7 +27,7 @@ export default function getAllProductsOperation({ const config = commerce.getConfig(cfg) const { results } = await config.fetch('products', 'list', [ { - limit: 250, + limit: variables.first, }, ]) const products = results.map((product: SwellProduct) => diff --git a/framework/swell/cart/utils/checkout-create.ts b/framework/swell/cart/utils/checkout-create.ts index 5ee3833c5..a4b82d849 100644 --- a/framework/swell/cart/utils/checkout-create.ts +++ b/framework/swell/cart/utils/checkout-create.ts @@ -9,7 +9,7 @@ export const checkoutCreate = async (fetch: any) => { }) if (!cart) { - const cart = await fetch({ + await fetch({ query: 'cart', method: 'setItems', variables: [[]],