From 649d4ad697333d1738230060ad425f1e2a3c2b48 Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Sat, 19 Nov 2022 12:01:22 +0000 Subject: [PATCH 1/7] Look at the app platform to decide how to choose api --- src/libs/HttpUtils.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libs/HttpUtils.js b/src/libs/HttpUtils.js index 4c8fe2ee6b30..a12dde795416 100644 --- a/src/libs/HttpUtils.js +++ b/src/libs/HttpUtils.js @@ -5,6 +5,7 @@ import CONFIG from '../CONFIG'; import CONST from '../CONST'; import ONYXKEYS from '../ONYXKEYS'; import HttpsError from './Errors/HttpsError'; +import getOperatingSystem from './getOperatingSystem'; let shouldUseStagingServer = false; Onyx.connect({ @@ -98,7 +99,10 @@ 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 we are in native mobile apps, we dont have access to up-to-date Config so we need to only rely on the toggle switch + const nativeStagingSwitcher = (getOperatingSystem() === CONST.OS.ANDROID || getOperatingSystem() === CONST.OS.IOS) && shouldUseStagingServer; + const webStagingSwitcher = CONFIG.IS_IN_STAGING && shouldUseStagingServer; + if (nativeStagingSwitcher || webStagingSwitcher) { apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.STAGING_SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.STAGING_EXPENSIFY_URL; } From dccda4fc1130bc0582a9abc03f7a6bb5705959bf Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Sat, 19 Nov 2022 12:07:51 +0000 Subject: [PATCH 2/7] Get operating system just once --- src/libs/HttpUtils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/HttpUtils.js b/src/libs/HttpUtils.js index a12dde795416..cb9105211c2e 100644 --- a/src/libs/HttpUtils.js +++ b/src/libs/HttpUtils.js @@ -100,7 +100,8 @@ function xhr(command, data, type = CONST.NETWORK.METHOD.POST, shouldUseSecure = let apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.URL_API_ROOT; // If we are in native mobile apps, we dont have access to up-to-date Config so we need to only rely on the toggle switch - const nativeStagingSwitcher = (getOperatingSystem() === CONST.OS.ANDROID || getOperatingSystem() === CONST.OS.IOS) && shouldUseStagingServer; + const platform = getOperatingSystem(); + const nativeStagingSwitcher = (platform === CONST.OS.ANDROID || platform === CONST.OS.IOS) && shouldUseStagingServer; const webStagingSwitcher = CONFIG.IS_IN_STAGING && shouldUseStagingServer; if (nativeStagingSwitcher || webStagingSwitcher) { apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.STAGING_SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.STAGING_EXPENSIFY_URL; From ec71c9731efbaa57bf0de33e694e663338efe1be Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Sat, 19 Nov 2022 12:11:24 +0000 Subject: [PATCH 3/7] Move the platform to global --- src/libs/HttpUtils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/HttpUtils.js b/src/libs/HttpUtils.js index cb9105211c2e..04f0068f935b 100644 --- a/src/libs/HttpUtils.js +++ b/src/libs/HttpUtils.js @@ -22,6 +22,8 @@ Onyx.connect({ // We use the AbortController API to terminate pending request in `cancelPendingRequests` let cancellationController = new AbortController(); +const platform = getOperatingSystem(); + /** * Send an HTTP request, and attempt to resolve the json response. * If there is a network error, we'll set the application offline. @@ -100,7 +102,6 @@ function xhr(command, data, type = CONST.NETWORK.METHOD.POST, shouldUseSecure = let apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.URL_API_ROOT; // If we are in native mobile apps, we dont have access to up-to-date Config so we need to only rely on the toggle switch - const platform = getOperatingSystem(); const nativeStagingSwitcher = (platform === CONST.OS.ANDROID || platform === CONST.OS.IOS) && shouldUseStagingServer; const webStagingSwitcher = CONFIG.IS_IN_STAGING && shouldUseStagingServer; if (nativeStagingSwitcher || webStagingSwitcher) { From 94ef87536c439185b9e2bab696f9d88536cdf06b Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Wed, 23 Nov 2022 00:05:11 +0000 Subject: [PATCH 4/7] Use library --- src/libs/HttpUtils.js | 10 ++++------ src/libs/shouldUseStagingServer/index.js | 13 +++++++++++++ src/libs/shouldUseStagingServer/index.native.js | 3 +++ 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 src/libs/shouldUseStagingServer/index.js create mode 100644 src/libs/shouldUseStagingServer/index.native.js diff --git a/src/libs/HttpUtils.js b/src/libs/HttpUtils.js index 04f0068f935b..ba519d48e212 100644 --- a/src/libs/HttpUtils.js +++ b/src/libs/HttpUtils.js @@ -6,11 +6,12 @@ import CONST from '../CONST'; import ONYXKEYS from '../ONYXKEYS'; import HttpsError from './Errors/HttpsError'; import getOperatingSystem from './getOperatingSystem'; +import shouldUseStagingServer from './shouldUseStagingServer'; -let shouldUseStagingServer = false; +let stagingServerToggleState = false; Onyx.connect({ key: ONYXKEYS.USER, - callback: val => shouldUseStagingServer = lodashGet(val, 'shouldUseStagingServer', true), + callback: val => stagingServerToggleState = lodashGet(val, 'shouldUseStagingServer', true), }); let shouldFailAllRequests = false; @@ -101,10 +102,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 we are in native mobile apps, we dont have access to up-to-date Config so we need to only rely on the toggle switch - const nativeStagingSwitcher = (platform === CONST.OS.ANDROID || platform === CONST.OS.IOS) && shouldUseStagingServer; - const webStagingSwitcher = CONFIG.IS_IN_STAGING && shouldUseStagingServer; - if (nativeStagingSwitcher || webStagingSwitcher) { + 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..2db22e938e21 --- /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; \ No newline at end of file diff --git a/src/libs/shouldUseStagingServer/index.native.js b/src/libs/shouldUseStagingServer/index.native.js new file mode 100644 index 000000000000..f83c2cb3a799 --- /dev/null +++ b/src/libs/shouldUseStagingServer/index.native.js @@ -0,0 +1,3 @@ +export default function shouldUseStagingServer(stagingServerToggleState) { + return stagingServerToggleState; +} From 3c40e03f829ae2da5e1615d13d918ed9b29c188e Mon Sep 17 00:00:00 2001 From: Vit Horacek <36083550+mountiny@users.noreply.github.com> Date: Wed, 23 Nov 2022 00:19:16 +0000 Subject: [PATCH 5/7] Update src/libs/shouldUseStagingServer/index.native.js Co-authored-by: Rajat Parashar --- src/libs/shouldUseStagingServer/index.native.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libs/shouldUseStagingServer/index.native.js b/src/libs/shouldUseStagingServer/index.native.js index f83c2cb3a799..8c660f9089f7 100644 --- a/src/libs/shouldUseStagingServer/index.native.js +++ b/src/libs/shouldUseStagingServer/index.native.js @@ -1,3 +1,7 @@ +/* + * @param {Boolean} stagingServerToggleState + * @returns {Boolean} + */ export default function shouldUseStagingServer(stagingServerToggleState) { return stagingServerToggleState; } From fa09886a8bbb676eb84bfaffd240df8d4d086f71 Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Thu, 24 Nov 2022 13:40:08 +0000 Subject: [PATCH 6/7] Remove the platform --- src/libs/HttpUtils.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libs/HttpUtils.js b/src/libs/HttpUtils.js index ba519d48e212..3359abe01373 100644 --- a/src/libs/HttpUtils.js +++ b/src/libs/HttpUtils.js @@ -5,7 +5,6 @@ import CONFIG from '../CONFIG'; import CONST from '../CONST'; import ONYXKEYS from '../ONYXKEYS'; import HttpsError from './Errors/HttpsError'; -import getOperatingSystem from './getOperatingSystem'; import shouldUseStagingServer from './shouldUseStagingServer'; let stagingServerToggleState = false; @@ -23,8 +22,6 @@ Onyx.connect({ // We use the AbortController API to terminate pending request in `cancelPendingRequests` let cancellationController = new AbortController(); -const platform = getOperatingSystem(); - /** * Send an HTTP request, and attempt to resolve the json response. * If there is a network error, we'll set the application offline. From 99424cdd8c9323ab13b49158b14f9db3e3677731 Mon Sep 17 00:00:00 2001 From: Vit Horacek Date: Thu, 24 Nov 2022 14:01:07 +0000 Subject: [PATCH 7/7] Fix lint --- src/libs/shouldUseStagingServer/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/shouldUseStagingServer/index.js b/src/libs/shouldUseStagingServer/index.js index 2db22e938e21..745dd03b4489 100644 --- a/src/libs/shouldUseStagingServer/index.js +++ b/src/libs/shouldUseStagingServer/index.js @@ -6,8 +6,8 @@ import CONFIG from '../../CONFIG'; * @param {Boolean} stagingServerToggleState * @returns {Boolean} */ -function shouldUseStagingServer(stagingServerToggleState){ +function shouldUseStagingServer(stagingServerToggleState) { return CONFIG.IS_IN_STAGING && stagingServerToggleState; } -export default shouldUseStagingServer; \ No newline at end of file +export default shouldUseStagingServer;