4
0
forked from crowetic/commerce

89 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-10-25 18:23:36 -05:00
import type { RequestInit } from '@vercel/fetch'
2020-10-27 01:04:51 +01:00
import type { CommerceAPIConfig } from '../../commerce/api'
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
export interface BigcommerceConfig extends CommerceAPIConfig {
// Indicates if the returned metadata with translations should be applied to the
// data or returned as it is
applyLocale?: boolean
2020-10-03 16:06:41 -05:00
storeApiUrl: string
storeApiToken: string
storeApiClientId: string
2020-10-27 00:20:11 -05:00
storeChannelId?: string
2020-10-03 16:06:41 -05:00
storeApiFetch<T>(endpoint: string, options?: RequestInit): Promise<T>
2020-10-01 15:16:23 -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-10-27 00:20:11 -05:00
const STORE_CHANNEL_ID = process.env.BIGCOMMERCE_CHANNEL_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: Omit<BigcommerceConfig, 'customerCookie'>) {
2020-10-01 15:16:23 -05:00
this.config = {
...config,
// The customerCookie is not customizable for now, BC sets the cookie and it's
// not important to rename it
customerCookie: 'SHOP_TOKEN',
2020-10-01 20:40:40 -05:00
}
2020-10-01 15:16:23 -05:00
}
2020-10-03 16:06:41 -05:00
getConfig(userConfig: Partial<BigcommerceConfig> = {}) {
return Object.entries(userConfig).reduce<BigcommerceConfig>(
(cfg, [key, value]) => Object.assign(cfg, { [key]: value }),
{ ...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
}
}
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,
applyLocale: true,
2020-10-03 16:06:41 -05:00
// REST API only
storeApiUrl: STORE_API_URL,
storeApiToken: STORE_API_TOKEN,
storeApiClientId: STORE_API_CLIENT_ID,
2020-10-27 00:20:11 -05:00
storeChannelId: STORE_CHANNEL_ID,
2020-10-03 16:06:41 -05:00
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
}