4
0
forked from crowetic/commerce

63 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-03-02 21:05:13 -06:00
import type { CommerceAPIConfig } from '@commerce/api'
import {
API_URL,
API_TOKEN,
SHOPIFY_CHECKOUT_ID_COOKIE,
SHOPIFY_CUSTOMER_TOKEN_COOKIE,
SHOPIFY_COOKIE_EXPIRE,
} from '../const'
if (!API_URL) {
throw new Error(
`The environment variable NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN is missing and it's required to access your store`
)
}
if (!API_TOKEN) {
throw new Error(
`The environment variable NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN is missing and it's required to access your store`
)
}
import fetchGraphqlApi from './utils/fetch-graphql-api'
2021-03-27 15:54:32 -06:00
export interface SwellConfig extends CommerceAPIConfig {}
2021-03-02 21:05:13 -06:00
export class Config {
2021-03-27 15:54:32 -06:00
private config: SwellConfig
2021-03-02 21:05:13 -06:00
2021-03-27 15:54:32 -06:00
constructor(config: SwellConfig) {
2021-03-02 21:05:13 -06:00
this.config = config
}
2021-03-27 15:54:32 -06:00
getConfig(userConfig: Partial<SwellConfig> = {}) {
return Object.entries(userConfig).reduce<SwellConfig>(
2021-03-02 21:05:13 -06:00
(cfg, [key, value]) => Object.assign(cfg, { [key]: value }),
{ ...this.config }
)
}
2021-03-27 15:54:32 -06:00
setConfig(newConfig: Partial<SwellConfig>) {
2021-03-02 21:05:13 -06:00
Object.assign(this.config, newConfig)
}
}
const config = new Config({
locale: 'en-US',
commerceUrl: API_URL,
apiToken: API_TOKEN!,
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE,
cartCookieMaxAge: SHOPIFY_COOKIE_EXPIRE,
fetch: fetchGraphqlApi,
customerCookie: SHOPIFY_CUSTOMER_TOKEN_COOKIE,
})
2021-03-27 15:54:32 -06:00
export function getConfig(userConfig?: Partial<SwellConfig>) {
2021-03-02 21:05:13 -06:00
return config.getConfig(userConfig)
}
2021-03-27 15:54:32 -06:00
export function setConfig(newConfig: Partial<SwellConfig>) {
2021-03-02 21:05:13 -06:00
return config.setConfig(newConfig)
}