4
0
forked from crowetic/commerce

77 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-09-30 21:08:25 -05:00
import { CommerceAPIConfig } from 'lib/commerce/api';
import { GetAllProductsQueryVariables } from '../schema';
import fetchAPI from './utils/fetch-api';
2020-09-30 11:44:38 -05:00
2020-09-30 15:59:46 -05:00
export interface Images {
small?: ImageOptions;
medium?: ImageOptions;
large?: ImageOptions;
xl?: ImageOptions;
}
export interface ImageOptions {
width: number;
height?: number;
}
export type ProductImageVariables = Pick<
GetAllProductsQueryVariables,
| 'imgSmallWidth'
| 'imgSmallHeight'
| 'imgMediumWidth'
| 'imgMediumHeight'
| 'imgLargeWidth'
| 'imgLargeHeight'
| 'imgXLWidth'
| 'imgXLHeight'
>;
2020-09-30 21:08:25 -05:00
export interface BigcommerceConfig extends CommerceAPIConfig {
images?: Images;
readonly imageVariables?: ProductImageVariables;
}
2020-09-30 11:44:38 -05:00
2020-09-30 21:08:25 -05:00
const API_URL = process.env.BIGCOMMERCE_STOREFRONT_API_URL;
const API_TOKEN = process.env.BIGCOMMERCE_STOREFRONT_API_TOKEN;
2020-09-30 11:44:38 -05:00
2020-09-30 21:08:25 -05:00
if (!API_URL) {
throw new Error(
`The environment variable BIGCOMMERCE_STOREFRONT_API_URL is missing and it's required to access your store`
);
}
2020-09-30 11:44:38 -05:00
2020-09-30 21:08:25 -05:00
if (!API_TOKEN) {
throw new Error(
`The environment variable BIGCOMMERCE_STOREFRONT_API_TOKEN is missing and it's required to access your store`
);
}
2020-09-30 12:32:46 -05:00
2020-09-30 21:08:25 -05:00
const config: BigcommerceConfig = {
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;
},
};
2020-09-30 12:32:46 -05:00
2020-09-30 21:08:25 -05:00
export function getConfig() {
return config;
}
2020-09-30 11:44:38 -05:00
2020-09-30 21:08:25 -05:00
export function setConfig(newConfig: Partial<BigcommerceConfig>) {
Object.assign(config, newConfig);
2020-09-30 11:44:38 -05:00
}