-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathutils.js.hbs
More file actions
48 lines (43 loc) · 1.4 KB
/
utils.js.hbs
File metadata and controls
48 lines (43 loc) · 1.4 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
/**
* Safely parses commerce agent settings from either a JSON string or object
* @param {string|object} settings - The commerce agent settings
* @returns {object} Parsed commerce agent settings object
*/
function parseCommerceAgentSettings(settings) {
// Default configuration when no settings are provided
const defaultConfig = {
enabled: 'false',
askAgentOnSearch: 'false',
embeddedServiceName: '',
embeddedServiceEndpoint: '',
scriptSourceUrl: '',
scrt2Url: '',
salesforceOrgId: '',
commerceOrgId: '',
siteId: ''
}
// If settings is already an object, return it
if (typeof settings === 'object' && settings !== null) {
return settings
}
// If settings is a string, try to parse it
if (typeof settings === 'string') {
try {
return JSON.parse(settings)
} catch (error) {
console.warn('Invalid COMMERCE_AGENT_SETTINGS format, using defaults:', error.message)
return defaultConfig
}
}
// If settings is undefined/null, return defaults
return defaultConfig
}
module.exports = {
parseCommerceAgentSettings
}