4
0
forked from crowetic/commerce

53 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-03-02 21:05:13 -06:00
import type { CommerceAPIConfig } from '@commerce/api'
import {
2021-04-25 14:20:58 -05:00
SWELL_CHECKOUT_ID_COOKIE,
SWELL_CUSTOMER_TOKEN_COOKIE,
SWELL_COOKIE_EXPIRE,
2021-03-02 21:05:13 -06:00
} from '../const'
2021-04-25 14:20:58 -05:00
import fetcher from '../fetcher'
2021-03-29 17:05:20 -06:00
import fetchSwellApi from './utils/fetch-swell-api'
2021-03-02 21:05:13 -06:00
2021-03-29 17:05:20 -06:00
export interface SwellConfig extends CommerceAPIConfig {
fetchSwell: any
}
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',
2021-04-25 14:20:58 -05:00
commerceUrl: '',
apiToken: ''!,
cartCookie: SWELL_CHECKOUT_ID_COOKIE,
cartCookieMaxAge: SWELL_COOKIE_EXPIRE,
2021-03-29 17:05:20 -06:00
fetchSwell: fetchSwellApi,
2021-04-25 14:20:58 -05:00
fetch: fetcher,
customerCookie: SWELL_CUSTOMER_TOKEN_COOKIE,
2021-03-02 21:05:13 -06:00
})
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)
}