diff --git a/src/components/TestToolMenu.js b/src/components/TestToolMenu.js index 10f12bac71d0..226c5a9897ce 100644 --- a/src/components/TestToolMenu.js +++ b/src/components/TestToolMenu.js @@ -1,5 +1,6 @@ import React from 'react'; import PropTypes from 'prop-types'; +import _ from 'underscore'; import {withOnyx} from 'react-native-onyx'; import lodashGet from 'lodash/get'; import styles from '../styles/styles'; @@ -14,6 +15,8 @@ import TestToolRow from './TestToolRow'; import networkPropTypes from './networkPropTypes'; import compose from '../libs/compose'; import {withNetwork} from './OnyxProvider'; +import getPlatform from '../libs/getPlatform'; +import CONST from '../CONST'; const propTypes = { /** User object in Onyx */ @@ -42,7 +45,7 @@ const TestToolMenu = props => ( This enables QA and internal testers to take advantage of sandbox environments for 3rd party services like Plaid and Onfido. */} User.setShouldUseStagingServer(!lodashGet(props, 'user.shouldUseStagingServer', true))} /> diff --git a/src/libs/HttpUtils.js b/src/libs/HttpUtils.js index 60971fe1b0b3..5fedc22f4d56 100644 --- a/src/libs/HttpUtils.js +++ b/src/libs/HttpUtils.js @@ -5,11 +5,15 @@ import CONFIG from '../CONFIG'; import CONST from '../CONST'; import ONYXKEYS from '../ONYXKEYS'; import HttpsError from './Errors/HttpsError'; +import shouldUseStagingServer from './shouldUseStagingServer'; +import getPlatform from './getPlatform'; -let shouldUseStagingServer = false; +// Desktop and web use staging config too so we we should default to staging API endpoint if on those platforms +const shouldDefaultToStaging = _.contains([CONST.PLATFORM.WEB, CONST.PLATFORM.DESKTOP], getPlatform()); +let stagingServerToggleState = false; Onyx.connect({ key: ONYXKEYS.USER, - callback: val => shouldUseStagingServer = lodashGet(val, 'shouldUseStagingServer', true), + callback: val => stagingServerToggleState = lodashGet(val, 'shouldUseStagingServer', shouldDefaultToStaging), }); let shouldFailAllRequests = false; @@ -105,7 +109,7 @@ function xhr(command, data, type = CONST.NETWORK.METHOD.POST, shouldUseSecure = let apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.URL_API_ROOT; - if (CONFIG.IS_IN_STAGING && shouldUseStagingServer) { + if (shouldUseStagingServer(stagingServerToggleState)) { apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.STAGING_SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.STAGING_EXPENSIFY_URL; } diff --git a/src/libs/shouldUseStagingServer/index.js b/src/libs/shouldUseStagingServer/index.js new file mode 100644 index 000000000000..745dd03b4489 --- /dev/null +++ b/src/libs/shouldUseStagingServer/index.js @@ -0,0 +1,13 @@ +import CONFIG from '../../CONFIG'; + +/** + * Helper method used to decide which API endpoint to call + * + * @param {Boolean} stagingServerToggleState + * @returns {Boolean} + */ +function shouldUseStagingServer(stagingServerToggleState) { + return CONFIG.IS_IN_STAGING && stagingServerToggleState; +} + +export default shouldUseStagingServer; diff --git a/src/libs/shouldUseStagingServer/index.native.js b/src/libs/shouldUseStagingServer/index.native.js new file mode 100644 index 000000000000..92baff89c835 --- /dev/null +++ b/src/libs/shouldUseStagingServer/index.native.js @@ -0,0 +1,13 @@ +import * as Environment from '../Environment/Environment'; + +/** + * Helper method used to decide which API endpoint to call in the Native apps. + * We build the staging native apps with production env config so we cannot rely on those values, + * hence we will decide solely on the value of the shouldUseStagingServer value (always false in production). + * + * @param {Boolean} stagingServerToggleState + * @returns {Boolean} + */ +export default function shouldUseStagingServer(stagingServerToggleState) { + return !Environment.isDevelopment() && stagingServerToggleState; +}