From 17f8d497b8bb55bd91405e3895662a1b9b8ac2d3 Mon Sep 17 00:00:00 2001 From: tniezg Date: Wed, 18 Aug 2021 17:53:53 +0200 Subject: [PATCH] Show placeholder message for /chechout and adjust api fetcher type --- .../spree/api/endpoints/checkout/checkout.ts | 44 +++++++++++++++++++ .../spree/api/endpoints/checkout/index.ts | 24 +++++++++- framework/spree/api/index.ts | 4 +- .../spree/api/operations/get-all-products.ts | 4 +- framework/spree/api/operations/get-product.ts | 4 +- .../spree/api/operations/get-site-info.ts | 18 +++++--- framework/spree/next.config.js | 8 ++++ 7 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 framework/spree/api/endpoints/checkout/checkout.ts diff --git a/framework/spree/api/endpoints/checkout/checkout.ts b/framework/spree/api/endpoints/checkout/checkout.ts new file mode 100644 index 000000000..3622e1630 --- /dev/null +++ b/framework/spree/api/endpoints/checkout/checkout.ts @@ -0,0 +1,44 @@ +import type { CheckoutEndpoint } from '.' + +const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({ + req: request, + res: response, + config, +}) => { + try { + const html = ` + + + + + + Checkout + + +
+ + + +

Checkout not yet implemented :(

+

+ See #64 +

+
+ + + ` + + response.status(200) + response.setHeader('Content-Type', 'text/html') + response.write(html) + response.end() + } catch (error) { + console.error(error) + + const message = 'An unexpected error ocurred' + + response.status(500).json({ data: null, errors: [{ message }] }) + } +} + +export default checkout diff --git a/framework/spree/api/endpoints/checkout/index.ts b/framework/spree/api/endpoints/checkout/index.ts index 491bf0ac9..47b315728 100644 --- a/framework/spree/api/endpoints/checkout/index.ts +++ b/framework/spree/api/endpoints/checkout/index.ts @@ -1 +1,23 @@ -export default function noopApi(...args: any[]): void {} +import { createEndpoint } from '@commerce/api' +import type { CommerceAPI } from '@commerce/api' +import type { GetAPISchema } from '@commerce/api' +import checkoutEndpoint from '@commerce/api/endpoints/checkout' +import type { CheckoutSchema } from '@commerce/types/checkout' +import type { SpreeApiProvider } from '@framework/api' +import checkout from './checkout' + +export type CheckoutAPI = GetAPISchema< + CommerceAPI, + CheckoutSchema +> + +export type CheckoutEndpoint = CheckoutAPI['endpoint'] + +export const handlers: CheckoutEndpoint['handlers'] = { checkout } + +const checkoutApi = createEndpoint({ + handler: checkoutEndpoint, + handlers, +}) + +export default checkoutApi diff --git a/framework/spree/api/index.ts b/framework/spree/api/index.ts index c8bac3bf1..17ad8002f 100644 --- a/framework/spree/api/index.ts +++ b/framework/spree/api/index.ts @@ -10,7 +10,9 @@ import getAllProductPaths from './operations/get-all-product-paths' import getAllProducts from './operations/get-all-products' import getProduct from './operations/get-product' -export interface SpreeApiConfig extends CommerceAPIConfig {} +export interface SpreeApiConfig extends CommerceAPIConfig { + fetch: any // Using any type, because CommerceAPIConfig['fetch'] cannot be extended from Variables = any to SpreeSdkVariables +} const config: SpreeApiConfig = { commerceUrl: '', diff --git a/framework/spree/api/operations/get-all-products.ts b/framework/spree/api/operations/get-all-products.ts index 6ffb704b0..a821fd15e 100644 --- a/framework/spree/api/operations/get-all-products.ts +++ b/framework/spree/api/operations/get-all-products.ts @@ -57,7 +57,9 @@ export default function getAllProductsOperation({ const { data: { data: spreeSuccessResponse }, - } = await apiFetch<{ data: IProducts }>('__UNUSED__', { variables }) + } = await apiFetch<{ data: IProducts }, SpreeSdkVariables>('__UNUSED__', { + variables, + }) const normalizedProducts: Product[] = spreeSuccessResponse.data.map( (spreeProduct) => normalizeProduct(spreeSuccessResponse, spreeProduct) diff --git a/framework/spree/api/operations/get-product.ts b/framework/spree/api/operations/get-product.ts index 42bfbb7fb..1b4bc8744 100644 --- a/framework/spree/api/operations/get-product.ts +++ b/framework/spree/api/operations/get-product.ts @@ -63,7 +63,9 @@ export default function getProductOperation({ const { data: { data: spreeSuccessResponse }, - } = await apiFetch<{ data: IProduct }>('__UNUSED__', { variables }) + } = await apiFetch<{ data: IProduct }, SpreeSdkVariables>('__UNUSED__', { + variables, + }) return { product: normalizeProduct( diff --git a/framework/spree/api/operations/get-site-info.ts b/framework/spree/api/operations/get-site-info.ts index ab3d395cf..64e82e358 100644 --- a/framework/spree/api/operations/get-site-info.ts +++ b/framework/spree/api/operations/get-site-info.ts @@ -86,9 +86,12 @@ export default function getSiteInfoOperation({ const { data: { data: spreeCategoriesSuccessResponse }, - } = await apiFetch<{ - data: ITaxons - }>('__UNUSED__', { + } = await apiFetch< + { + data: ITaxons + }, + SpreeSdkVariables + >('__UNUSED__', { variables: createVariables( requireConfigValue('spreeCategoriesTaxonomyId') as string ), @@ -96,9 +99,12 @@ export default function getSiteInfoOperation({ const { data: { data: spreeBrandsSuccessResponse }, - } = await apiFetch<{ - data: ITaxons - }>('__UNUSED__', { + } = await apiFetch< + { + data: ITaxons + }, + SpreeSdkVariables + >('__UNUSED__', { variables: createVariables( requireConfigValue('spreeBrandsTaxonomyId') as string ), diff --git a/framework/spree/next.config.js b/framework/spree/next.config.js index 13d530a49..0aaa87e0a 100644 --- a/framework/spree/next.config.js +++ b/framework/spree/next.config.js @@ -5,4 +5,12 @@ module.exports = { images: { domains: [process.env.NEXT_PUBLIC_SPREE_ALLOWED_IMAGE_DOMAIN], }, + rewrites() { + return [ + { + source: '/checkout', + destination: '/api/checkout', + }, + ] + }, }