From e76fa6635e2d2610de86724cefeb2355a258d70d Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Wed, 20 Sep 2023 15:17:49 +0200 Subject: [PATCH 1/7] ref: moved ApiUtils to TS --- src/libs/{ApiUtils.js => ApiUtils.ts} | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) rename src/libs/{ApiUtils.js => ApiUtils.ts} (96%) diff --git a/src/libs/ApiUtils.js b/src/libs/ApiUtils.ts similarity index 96% rename from src/libs/ApiUtils.js rename to src/libs/ApiUtils.ts index 8dcdc44776aa..fe2faf729a0a 100644 --- a/src/libs/ApiUtils.js +++ b/src/libs/ApiUtils.ts @@ -5,6 +5,7 @@ import CONFIG from '../CONFIG'; import CONST from '../CONST'; import * as Environment from './Environment/Environment'; import proxyConfig from '../../config/proxyConfig'; +import {Request} from '../types/onyx'; // To avoid rebuilding native apps, native apps use production config for both staging and prod // We use the async environment check because it works on all platforms @@ -37,7 +38,7 @@ Environment.getEnvironment().then((envName) => { * @param {Boolean} [request.shouldUseSecure] * @returns {String} */ -function getApiRoot(request) { +function getApiRoot(request?: Request) { const shouldUseSecure = lodashGet(request, 'shouldUseSecure', false); if (shouldUseStagingServer) { From 91603cea1a859624e967dc3f33b77afa9f465dee Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 21 Sep 2023 14:27:45 +0200 Subject: [PATCH 2/7] ref: added correct types --- src/libs/ApiUtils.ts | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/libs/ApiUtils.ts b/src/libs/ApiUtils.ts index fe2faf729a0a..973929a19d86 100644 --- a/src/libs/ApiUtils.ts +++ b/src/libs/ApiUtils.ts @@ -1,4 +1,3 @@ -import lodashGet from 'lodash/get'; import Onyx from 'react-native-onyx'; import ONYXKEYS from '../ONYXKEYS'; import CONFIG from '../CONFIG'; @@ -25,7 +24,7 @@ Environment.getEnvironment().then((envName) => { } const defaultToggleState = ENV_NAME === CONST.ENVIRONMENT.STAGING || ENV_NAME === CONST.ENVIRONMENT.ADHOC; - shouldUseStagingServer = lodashGet(val, 'shouldUseStagingServer', defaultToggleState); + shouldUseStagingServer = val?.shouldUseStagingServer ?? defaultToggleState; }, }); }); @@ -33,13 +32,9 @@ Environment.getEnvironment().then((envName) => { /** * Get the currently used API endpoint * (Non-production environments allow for dynamically switching the API) - * - * @param {Object} [request] - * @param {Boolean} [request.shouldUseSecure] - * @returns {String} */ function getApiRoot(request?: Request) { - const shouldUseSecure = lodashGet(request, 'shouldUseSecure', false); + const shouldUseSecure = request?.shouldUseSecure ?? false; if (shouldUseStagingServer) { if (CONFIG.IS_USING_WEB_PROXY) { @@ -53,20 +48,14 @@ function getApiRoot(request?: Request) { /** * Get the command url for the given request - * - * @param {Object} request - * @param {String} request.command - the name of the API command - * @param {Boolean} [request.shouldUseSecure] - * @returns {String} + * @param - the name of the API command */ -function getCommandURL(request) { +function getCommandURL(request: Request) { return `${getApiRoot(request)}api?command=${request.command}`; } /** * Check if we're currently using the staging API root - * - * @returns {Boolean} */ function isUsingStagingApi() { return shouldUseStagingServer; From 3ad8dd27cbfa49e93b4b69e60ec923451a29e3d3 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 21 Sep 2023 14:41:55 +0200 Subject: [PATCH 3/7] fix: resolve comment --- src/libs/ApiUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/ApiUtils.ts b/src/libs/ApiUtils.ts index 973929a19d86..95ac1497b8ca 100644 --- a/src/libs/ApiUtils.ts +++ b/src/libs/ApiUtils.ts @@ -16,7 +16,7 @@ Environment.getEnvironment().then((envName) => { // We connect here, so we have the updated ENV_NAME when Onyx callback runs Onyx.connect({ key: ONYXKEYS.USER, - callback: (val) => { + callback: (value) => { // Toggling between APIs is not allowed on production and internal dev environment if (ENV_NAME === CONST.ENVIRONMENT.PRODUCTION || CONFIG.IS_USING_LOCAL_WEB) { shouldUseStagingServer = false; @@ -24,7 +24,7 @@ Environment.getEnvironment().then((envName) => { } const defaultToggleState = ENV_NAME === CONST.ENVIRONMENT.STAGING || ENV_NAME === CONST.ENVIRONMENT.ADHOC; - shouldUseStagingServer = val?.shouldUseStagingServer ?? defaultToggleState; + shouldUseStagingServer = value?.shouldUseStagingServer ?? defaultToggleState; }, }); }); From 06c34d41eecb6dfb9a9478f6b9158803f31fe685 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 21 Sep 2023 16:45:00 +0200 Subject: [PATCH 4/7] fix: resolve comments, fixed argument type --- src/libs/ApiUtils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/ApiUtils.ts b/src/libs/ApiUtils.ts index 95ac1497b8ca..24e6bada6f86 100644 --- a/src/libs/ApiUtils.ts +++ b/src/libs/ApiUtils.ts @@ -33,7 +33,7 @@ Environment.getEnvironment().then((envName) => { * Get the currently used API endpoint * (Non-production environments allow for dynamically switching the API) */ -function getApiRoot(request?: Request) { +function getApiRoot(request?: Pick): string { const shouldUseSecure = request?.shouldUseSecure ?? false; if (shouldUseStagingServer) { @@ -50,14 +50,14 @@ function getApiRoot(request?: Request) { * Get the command url for the given request * @param - the name of the API command */ -function getCommandURL(request: Request) { +function getCommandURL(request: Request): string { return `${getApiRoot(request)}api?command=${request.command}`; } /** * Check if we're currently using the staging API root */ -function isUsingStagingApi() { +function isUsingStagingApi(): boolean { return shouldUseStagingServer; } From fdf47e2cd5027ad61221394785e51e067fd40bd3 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 28 Sep 2023 12:47:47 +0200 Subject: [PATCH 5/7] ref: resolve comments --- src/libs/ApiUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ApiUtils.ts b/src/libs/ApiUtils.ts index 24e6bada6f86..87a251ccb086 100644 --- a/src/libs/ApiUtils.ts +++ b/src/libs/ApiUtils.ts @@ -33,7 +33,7 @@ Environment.getEnvironment().then((envName) => { * Get the currently used API endpoint * (Non-production environments allow for dynamically switching the API) */ -function getApiRoot(request?: Pick): string { +function getApiRoot(request?: Request): string { const shouldUseSecure = request?.shouldUseSecure ?? false; if (shouldUseStagingServer) { From f5d9107466b92563e0102ab63315f7464265f564 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 28 Sep 2023 13:31:16 +0200 Subject: [PATCH 6/7] fix: type errors --- src/libs/ApiUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ApiUtils.ts b/src/libs/ApiUtils.ts index 87a251ccb086..24e6bada6f86 100644 --- a/src/libs/ApiUtils.ts +++ b/src/libs/ApiUtils.ts @@ -33,7 +33,7 @@ Environment.getEnvironment().then((envName) => { * Get the currently used API endpoint * (Non-production environments allow for dynamically switching the API) */ -function getApiRoot(request?: Request): string { +function getApiRoot(request?: Pick): string { const shouldUseSecure = request?.shouldUseSecure ?? false; if (shouldUseStagingServer) { From 6af0606bae56db954fe30b54e13d4629b7ea7831 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 28 Sep 2023 13:32:50 +0200 Subject: [PATCH 7/7] fix: types error --- src/libs/ApiUtils.ts | 2 +- src/libs/tryResolveUrlFromApiRoot.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/ApiUtils.ts b/src/libs/ApiUtils.ts index 24e6bada6f86..87a251ccb086 100644 --- a/src/libs/ApiUtils.ts +++ b/src/libs/ApiUtils.ts @@ -33,7 +33,7 @@ Environment.getEnvironment().then((envName) => { * Get the currently used API endpoint * (Non-production environments allow for dynamically switching the API) */ -function getApiRoot(request?: Pick): string { +function getApiRoot(request?: Request): string { const shouldUseSecure = request?.shouldUseSecure ?? false; if (shouldUseStagingServer) { diff --git a/src/libs/tryResolveUrlFromApiRoot.ts b/src/libs/tryResolveUrlFromApiRoot.ts index f9aef09a00a7..6bfb5473271e 100644 --- a/src/libs/tryResolveUrlFromApiRoot.ts +++ b/src/libs/tryResolveUrlFromApiRoot.ts @@ -1,4 +1,5 @@ import Config from '../CONFIG'; +import {Request} from '../types/onyx'; import * as ApiUtils from './ApiUtils'; // Absolute URLs (`/` or `//`) should be resolved from API ROOT @@ -24,7 +25,7 @@ function tryResolveUrlFromApiRoot(url: string | number): string | number { if (typeof url === 'number') { return url; } - const apiRoot = ApiUtils.getApiRoot({shouldUseSecure: false}); + const apiRoot = ApiUtils.getApiRoot({shouldUseSecure: false} as Request); return url.replace(ORIGIN_PATTERN, apiRoot); }