-
Notifications
You must be signed in to change notification settings - Fork 214
Gracefully handle when some config.app.* properties are missing (@W-19453183@) #3230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c538dac
8cb30b7
ca5f1bb
287a823
6c79acb
0bd299f
2022342
9e4bc9b
159fd5a
78d9185
e1a940d
0436c03
0cf3273
83ec682
79c7bfb
946e287
385c8a1
4e5ace7
5e503d3
c4ccb91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * 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 settings from either a JSON string or object | ||
| * @param {string|object} settings - The settings | ||
| * @returns {object} Parsed settings object | ||
| */ | ||
| function parseSettings(settings) { | ||
| // 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 json format:', error.message) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| console.warn('Cannot parse settings from:', settings) | ||
| return | ||
| } | ||
|
|
||
| module.exports = { | ||
| parseSettings | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * 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 settings from either a JSON string or object | ||
| * @param {string|object} settings - The settings | ||
| * @returns {object} Parsed settings object | ||
| */ | ||
| function parseSettings(settings) { | ||
| // 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 json format:', error.message) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| console.warn('Cannot parse settings from:', settings) | ||
| return | ||
| } | ||
|
|
||
| module.exports = { | ||
| parseSettings | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ import { | |
| categoryUrlBuilder | ||
| } from '@salesforce/retail-react-app/app/utils/url' | ||
| import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config' | ||
| import {getCommerceAgentConfig} from '@salesforce/retail-react-app/app/utils/config-utils' | ||
|
|
||
| const onClient = typeof window !== 'undefined' | ||
|
|
||
|
|
@@ -93,8 +94,11 @@ const formatSuggestions = (searchSuggestions, input) => { | |
| */ | ||
| const Search = (props) => { | ||
| const config = getConfig() | ||
| const {enabled, askAgentOnSearch} = config.app.commerceAgent | ||
| const askAgentOnSearchEnabled = isAskAgentOnSearchEnabled(enabled, askAgentOnSearch) | ||
| const askAgentOnSearchEnabled = useMemo(() => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be consistent with the previous file (App component), we also call |
||
| const {enabled, askAgentOnSearch} = getCommerceAgentConfig() | ||
| return isAskAgentOnSearchEnabled(enabled, askAgentOnSearch) | ||
| }, [config.app.commerceAgent]) | ||
|
|
||
| const [isOpen, setIsOpen] = useState(false) | ||
| const [searchQuery, setSearchQuery] = useState('') | ||
| const navigate = useNavigation() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -437,7 +437,7 @@ const useEinstein = () => { | |
| const {effectiveDnt} = useDNT() | ||
| const {getTokenWhenReady} = useAccessToken() | ||
| const { | ||
| app: {einsteinAPI: config} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked in the entire monorepo for any config parsing that may fail. This is one example where I tried to make sure there is no js undefined error. |
||
| app: {einsteinAPI: config = {}} | ||
| } = getConfig() | ||
| const {host, einsteinId, siteId, isProduction} = config | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,7 +39,7 @@ const SocialLoginRedirect = () => { | |
| const {data: customer} = useCurrentCustomer() | ||
| // Build redirectURI from config values | ||
| const appOrigin = useAppOrigin() | ||
| const redirectPath = getConfig().app.login.social?.redirectURI || '' | ||
| const redirectPath = getConfig().app.login?.social?.redirectURI || '' | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| const redirectURI = buildRedirectURI(appOrigin, redirectPath) | ||
|
|
||
| const locatedFrom = getSessionJSONItem('returnToPage') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * 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 | ||
| */ | ||
|
|
||
| import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config' | ||
|
|
||
| export const getCommerceAgentConfig = () => { | ||
| const defaults = { | ||
| enabled: 'false', | ||
| askAgentOnSearch: 'false', | ||
| embeddedServiceName: '', | ||
| embeddedServiceEndpoint: '', | ||
| scriptSourceUrl: '', | ||
| scrt2Url: '', | ||
| salesforceOrgId: '', | ||
| commerceOrgId: '', | ||
| siteId: '' | ||
| } | ||
| return getConfig().app.commerceAgent ?? defaults | ||
|
Comment on lines
+11
to
+22
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If extensible project, it will automatically inherit this config-utils.js file. Consider the scenario of the config not having You may think that these defaults are redundant.. don't we already have them in the config file? Well yes, if you generate a new project. But existing config file may not have these values yet. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,14 +4,23 @@ | |
| * 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 | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| /* eslint-disable @typescript-eslint/no-var-requires */ | ||
| const sites = require('./sites.js') | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const {parseCommerceAgentSettings} = require('./utils.js') | ||
| const {parseSettings} = require('./utils.js') | ||
|
|
||
| module.exports = { | ||
| app: { | ||
| commerceAgent: parseCommerceAgentSettings(process.env.COMMERCE_AGENT_SETTINGS), | ||
| commerceAgent: parseSettings(process.env.COMMERCE_AGENT_SETTINGS) || { | ||
| enabled: 'false', | ||
| askAgentOnSearch: 'false', | ||
| embeddedServiceName: '', | ||
| embeddedServiceEndpoint: '', | ||
| scriptSourceUrl: '', | ||
| scrt2Url: '', | ||
| salesforceOrgId: '', | ||
| commerceOrgId: '', | ||
| siteId: '' | ||
| }, | ||
|
Comment on lines
+13
to
+23
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a more generic And why explicitly have the default values here? To be consistent with the existing pattern seen with |
||
| url: { | ||
| site: 'path', | ||
| locale: 'path', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably want to add this same changelog entry to the pwa-kit-create-app changelog
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided not to update the changelog for pwa-kit-create-app because the changes there are all related to shoppper agent, which has not been released yet. So we don't need to say we fix something that isn't released yet.