-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.js
35 lines (32 loc) · 1.01 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import config from 'config'
/**
* Adjust the config provided to the current store selected via request params
* @param Object config configuration
* @param Express request req
* TODO: Implement multistore in OpenCart VS Bridge
*/
export function multiStoreConfig(apiConfig, req) {
let confCopy = Object.assign({}, apiConfig)
let storeCode = ''
if (req.headers['x-vs-store-code']) {
storeCode = req.headers['x-vs-store']
}
if (req.query.storeCode) {
storeCode = req.query.storeCode
}
if (storeCode && config.availableStores.indexOf(storeCode) >= 0)
{
if (config.opencart2['api_' + storeCode]) {
confCopy = Object.assign({}, config.opencart2['api_' + storeCode])
} else {
if (new RegExp("(/" + config.availableStores.join("|") + "/)", "gm").exec(confCopy.url) === null) {
confCopy.url = (confCopy.url).replace(/(vsbridge)/gm, `${storeCode}/$1`);
}
}
} else {
if (storeCode) {
console.error('Unavailable store code', storeCode)
}
}
return confCopy
}