2020-10-01 20:40:40 -05:00
import { CommerceAPIConfig } from 'lib/commerce/api'
import { GetAllProductsQueryVariables } from '../schema'
2020-10-03 16:24:12 -05:00
import fetchGraphqlApi from './utils/fetch-graphql-api'
2020-10-03 16:06:41 -05:00
import fetchStoreApi from './utils/fetch-store-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-03 16:06:41 -05:00
storeApiUrl : string
storeApiToken : string
storeApiClientId : string
storeApiFetch < T > ( endpoint : string , options? : RequestInit ) : Promise < T >
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-10-03 16:06:41 -05:00
const STORE_API_URL = process . env . BIGCOMMERCE_STORE_API_URL
const STORE_API_TOKEN = process . env . BIGCOMMERCE_STORE_API_TOKEN
const STORE_API_CLIENT_ID = process . env . BIGCOMMERCE_STORE_API_CLIENT_ID
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-03 16:06:41 -05:00
if ( ! ( STORE_API_URL && STORE_API_TOKEN && STORE_API_CLIENT_ID ) ) {
throw new Error (
` The environment variables BIGCOMMERCE_STORE_API_URL, BIGCOMMERCE_STORE_API_TOKEN, BIGCOMMERCE_STORE_API_CLIENT_ID have to be set in order to access the REST API of your store `
)
}
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 ,
2020-10-03 16:06:41 -05:00
imageVariables : this.getImageVariables ( config . images ) ,
2020-10-01 20:40:40 -05:00
}
2020-10-01 15:16:23 -05:00
}
2020-10-03 16:06:41 -05:00
getImageVariables ( images? : Images ) {
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 ( userConfig : Partial < BigcommerceConfig > = { } ) {
const { images : configImages , . . . config } = this . config
const images = { . . . configImages , . . . userConfig . images }
return Object . assign ( config , userConfig , {
images ,
imageVariables : this.getImageVariables ( images ) ,
} )
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
}
}
2020-10-03 20:49:09 -05:00
const ONE_DAY = 60 * 60 * 24
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 ,
2020-10-03 16:06:41 -05:00
cartCookie : process.env.BIGCOMMERCE_CART_COOKIE ? ? 'bc_cartId' ,
2020-10-03 20:49:09 -05:00
cartCookieMaxAge : ONE_DAY * 30 ,
2020-10-03 16:24:12 -05:00
fetch : fetchGraphqlApi ,
2020-10-03 16:06:41 -05:00
// REST API only
storeApiUrl : STORE_API_URL ,
storeApiToken : STORE_API_TOKEN ,
storeApiClientId : STORE_API_CLIENT_ID ,
storeApiFetch : fetchStoreApi ,
2020-10-01 20:40:40 -05:00
} )
2020-09-30 12:32:46 -05:00
2020-10-03 16:06:41 -05:00
export function getConfig ( userConfig? : Partial < BigcommerceConfig > ) {
return config . getConfig ( userConfig )
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
}