import type { RequestInit } from '@vercel/fetch' import type { CommerceAPIConfig } from '@commerce/api' import fetchGraphqlApi from './utils/fetch-graphql-api' import fetchStoreApi from './utils/fetch-store-api' export interface AquilacmsConfig extends CommerceAPIConfig { // Indicates if the returned metadata with translations should be applied to the // data or returned as it is applyLocale?: boolean storeApiUrl: string storeApiFetch(endpoint: string, options?: RequestInit): Promise } const STORE_URL = process.env.AQUILACMS_URL const STORE_API_URL = process.env.AQUILACMS_API_URL if (!STORE_URL) { throw new Error( `The environment variable AQUILACMS_URL is missing and it's required to access your store` ) } if (!STORE_API_URL) { throw new Error( `The environment variable AQUILACMS_API_URL is missing and it's required to access your store` ) } export class Config { private config: AquilacmsConfig constructor(config: Omit) { 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', } } getConfig(userConfig: Partial = {}) { return Object.entries(userConfig).reduce( (cfg, [key, value]) => Object.assign(cfg, { [key]: value }), { ...this.config } ) } setConfig(newConfig: Partial) { Object.assign(this.config, newConfig) } } const ONE_DAY = 60 * 60 * 24 const config = new Config({ commerceUrl: STORE_URL, apiToken: '', cartCookie: process.env.AQUILACMS_CART_COOKIE ?? 'aquilacms_cartId', cartCookieMaxAge: ONE_DAY * 30, fetch: fetchGraphqlApi, applyLocale: true, storeApiUrl: STORE_API_URL, storeApiFetch: fetchStoreApi, }) export function getConfig(userConfig?: Partial) { return config.getConfig(userConfig) } export function setConfig(newConfig: Partial) { return config.setConfig(newConfig) }