4
0
forked from crowetic/commerce

98 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-10-01 20:40:40 -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 {
2020-10-01 20:40:40 -05:00
small?: ImageOptions
medium?: ImageOptions
large?: ImageOptions
xl?: ImageOptions
2020-09-30 15:59:46 -05:00
}
export interface ImageOptions {
2020-10-01 20:40:40 -05:00
width: number
height?: number
2020-09-30 15:59:46 -05:00
}
export type ProductImageVariables = Pick<
GetAllProductsQueryVariables,
| 'imgSmallWidth'
| 'imgSmallHeight'
| 'imgMediumWidth'
| 'imgMediumHeight'
| 'imgLargeWidth'
| 'imgLargeHeight'
| 'imgXLWidth'
| 'imgXLHeight'
2020-10-01 20:40:40 -05:00
>
2020-09-30 15:59:46 -05:00
2020-10-01 15:16:23 -05:00
export interface BigcommerceConfigOptions extends CommerceAPIConfig {
2020-10-01 20:40:40 -05:00
images?: Images
2020-10-01 15:16:23 -05:00
}
export interface BigcommerceConfig extends BigcommerceConfigOptions {
2020-10-01 20:40:40 -05:00
readonly imageVariables?: ProductImageVariables
2020-09-30 21:08:25 -05:00
}
2020-09-30 11:44:38 -05:00
2020-10-01 20:40:40 -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-10-01 20:40:40 -05:00
)
2020-09-30 21:08:25 -05:00
}
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-10-01 20:40:40 -05:00
)
2020-09-30 21:08:25 -05:00
}
2020-09-30 12:32:46 -05:00
2020-10-01 15:16:23 -05:00
export class Config {
2020-10-01 20:40:40 -05:00
private config: BigcommerceConfig
2020-10-01 15:16:23 -05:00
constructor(config: BigcommerceConfigOptions) {
this.config = {
...config,
get imageVariables() {
2020-10-01 20:40:40 -05:00
const { images } = this
2020-10-01 15:16:23 -05:00
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,
}
2020-10-01 20:40:40 -05:00
: undefined
2020-10-01 15:16:23 -05:00
},
2020-10-01 20:40:40 -05:00
}
2020-10-01 15:16:23 -05:00
}
getConfig() {
2020-10-01 20:40:40 -05:00
return this.config
2020-10-01 15:16:23 -05:00
}
setConfig(newConfig: Partial<BigcommerceConfig>) {
2020-10-01 20:40:40 -05:00
Object.assign(this.config, newConfig)
2020-10-01 15:16:23 -05:00
}
}
const config = new Config({
2020-09-30 21:08:25 -05:00
commerceUrl: API_URL,
apiToken: API_TOKEN,
fetch: fetchAPI,
2020-10-01 20:40:40 -05:00
})
2020-09-30 12:32:46 -05:00
2020-09-30 21:08:25 -05:00
export function getConfig() {
2020-10-01 20:40:40 -05:00
return config.getConfig()
2020-09-30 21:08:25 -05:00
}
2020-09-30 11:44:38 -05:00
2020-09-30 21:08:25 -05:00
export function setConfig(newConfig: Partial<BigcommerceConfig>) {
2020-10-01 20:40:40 -05:00
return config.setConfig(newConfig)
2020-09-30 11:44:38 -05:00
}