mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
.vscode
assets
components
config
framework
bigcommerce
commerce
shopify
swell
vendure
api
fragments
utils
checkout.ts
index.ts
auth
cart
common
customer
lib
product
wishlist
README.md
codegen.json
index.tsx
schema.d.ts
schema.graphql
lib
pages
public
.editorconfig
.env.template
.gitignore
.prettierignore
.prettierrc
README.md
codegen.json
commerce.config.json
global.d.ts
license.md
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import type { CommerceAPIConfig } from '@commerce/api'
|
|
import fetchGraphqlApi from './utils/fetch-graphql-api'
|
|
|
|
export interface VendureConfig extends CommerceAPIConfig {}
|
|
|
|
const API_URL = process.env.NEXT_PUBLIC_VENDURE_SHOP_API_URL
|
|
|
|
if (!API_URL) {
|
|
throw new Error(
|
|
`The environment variable NEXT_PUBLIC_VENDURE_SHOP_API_URL is missing and it's required to access your store`
|
|
)
|
|
}
|
|
|
|
export class Config {
|
|
private config: VendureConfig
|
|
|
|
constructor(config: VendureConfig) {
|
|
this.config = {
|
|
...config,
|
|
}
|
|
}
|
|
|
|
getConfig(userConfig: Partial<VendureConfig> = {}) {
|
|
return Object.entries(userConfig).reduce<VendureConfig>(
|
|
(cfg, [key, value]) => Object.assign(cfg, { [key]: value }),
|
|
{ ...this.config }
|
|
)
|
|
}
|
|
|
|
setConfig(newConfig: Partial<VendureConfig>) {
|
|
Object.assign(this.config, newConfig)
|
|
}
|
|
}
|
|
|
|
const ONE_DAY = 60 * 60 * 24
|
|
const config = new Config({
|
|
commerceUrl: API_URL,
|
|
apiToken: '',
|
|
cartCookie: '',
|
|
customerCookie: '',
|
|
cartCookieMaxAge: ONE_DAY * 30,
|
|
fetch: fetchGraphqlApi,
|
|
})
|
|
|
|
export function getConfig(userConfig?: Partial<VendureConfig>) {
|
|
return config.getConfig(userConfig)
|
|
}
|
|
|
|
export function setConfig(newConfig: Partial<VendureConfig>) {
|
|
return config.setConfig(newConfig)
|
|
}
|