Loan Laux f52978e1a3
fix cart update and add API calls to work for logged-in users
Signed-off-by: Loan Laux <loan@outgrow.io>
2021-04-27 18:40:02 +04:00

61 lines
1.6 KiB
TypeScript

import type { CommerceAPIConfig } from '@commerce/api'
import {
API_URL,
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
REACTION_CART_ID_COOKIE,
REACTION_EMPTY_DUMMY_CART_ID,
REACTION_CUSTOMER_TOKEN_COOKIE,
REACTION_COOKIE_EXPIRE,
SHOP_ID,
} 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`
)
}
import fetchGraphqlApi from './utils/fetch-graphql-api'
export interface ReactionCommerceConfig extends CommerceAPIConfig {}
export class Config {
private config: ReactionCommerceConfig
constructor(config: ReactionCommerceConfig) {
this.config = config
}
getConfig(userConfig: Partial<ReactionCommerceConfig> = {}) {
return Object.entries(userConfig).reduce<ReactionCommerceConfig>(
(cfg, [key, value]) => Object.assign(cfg, { [key]: value }),
{ ...this.config }
)
}
setConfig(newConfig: Partial<ReactionCommerceConfig>) {
Object.assign(this.config, newConfig)
}
}
const config = new Config({
locale: 'en-US',
commerceUrl: API_URL,
anonymousCartTokenCookie: REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
cartIdCookie: REACTION_CART_ID_COOKIE,
dummyEmptyCartId: REACTION_EMPTY_DUMMY_CART_ID,
anonymousCartTokenCookieMaxAge: REACTION_COOKIE_EXPIRE,
fetch: fetchGraphqlApi,
customerCookie: REACTION_CUSTOMER_TOKEN_COOKIE,
shopId: SHOP_ID,
})
export function getConfig(userConfig?: Partial<ReactionCommerceConfig>) {
return config.getConfig(userConfig)
}
export function setConfig(newConfig: Partial<ReactionCommerceConfig>) {
return config.setConfig(newConfig)
}