From 5bd31d254d00b0686a343c8fac451778aea5bd2b Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Thu, 1 Oct 2020 15:16:23 -0500 Subject: [PATCH] Updated config --- lib/bigcommerce/api/index.ts | 61 ++++++++++++++++++++++++------------ lib/commerce-api.ts | 9 ------ 2 files changed, 41 insertions(+), 29 deletions(-) delete mode 100644 lib/commerce-api.ts diff --git a/lib/bigcommerce/api/index.ts b/lib/bigcommerce/api/index.ts index aada33024..0cf0568c7 100644 --- a/lib/bigcommerce/api/index.ts +++ b/lib/bigcommerce/api/index.ts @@ -26,8 +26,11 @@ export type ProductImageVariables = Pick< | 'imgXLHeight' >; -export interface BigcommerceConfig extends CommerceAPIConfig { +export interface BigcommerceConfigOptions extends CommerceAPIConfig { images?: Images; +} + +export interface BigcommerceConfig extends BigcommerceConfigOptions { readonly imageVariables?: ProductImageVariables; } @@ -46,31 +49,49 @@ if (!API_TOKEN) { ); } -const config: BigcommerceConfig = { +export class Config { + private config: BigcommerceConfig; + + constructor(config: BigcommerceConfigOptions) { + this.config = { + ...config, + get imageVariables() { + const { images } = this; + return images + ? { + imgSmallWidth: images.small?.width, + imgSmallHeight: images.small?.height, + imgMediumWidth: images.medium?.height, + imgMediumHeight: images.medium?.height, + imgLargeWidth: images.large?.height, + imgLargeHeight: images.large?.height, + imgXLWidth: images.xl?.height, + imgXLHeight: images.xl?.height, + } + : undefined; + }, + }; + } + + getConfig() { + return this.config; + } + + setConfig(newConfig: Partial) { + Object.assign(this.config, newConfig); + } +} + +const config = new Config({ commerceUrl: API_URL, apiToken: API_TOKEN, fetch: fetchAPI, - get imageVariables() { - const { images } = this; - return images - ? { - imgSmallWidth: images.small?.width, - imgSmallHeight: images.small?.height, - imgMediumWidth: images.medium?.height, - imgMediumHeight: images.medium?.height, - imgLargeWidth: images.large?.height, - imgLargeHeight: images.large?.height, - imgXLWidth: images.xl?.height, - imgXLHeight: images.xl?.height, - } - : undefined; - }, -}; +}); export function getConfig() { - return config; + return config.getConfig(); } export function setConfig(newConfig: Partial) { - Object.assign(config, newConfig); + return config.setConfig(newConfig); } diff --git a/lib/commerce-api.ts b/lib/commerce-api.ts deleted file mode 100644 index e2288f197..000000000 --- a/lib/commerce-api.ts +++ /dev/null @@ -1,9 +0,0 @@ -import BigcommerceAPI from "./bigcommerce/api"; - -const API_URL = process.env.NEXT_EXAMPLE_BIGCOMMERCE_STOREFRONT_API_URL!; -const API_TOKEN = process.env.NEXT_EXAMPLE_BIGCOMMERCE_STOREFRONT_API_TOKEN!; - -export const commerce = new BigcommerceAPI({ - commerceUrl: API_URL, - apiToken: API_TOKEN, -});