From d7b9c815938dbdf97302e80895da2e6210b92d77 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Fri, 29 Sep 2023 11:39:56 +0200 Subject: [PATCH 1/7] Migrate environment lib to TS --- .../{Environment.js => Environment.ts} | 25 ++++++------------- .../{index.android.js => index.android.ts} | 11 +++++--- .../{index.ios.js => index.ios.ts} | 6 ++--- .../betaChecker/{index.js => index.ts} | 4 +-- .../{index.native.js => index.native.ts} | 11 +++----- .../getEnvironment/{index.js => index.ts} | 7 ++---- 6 files changed, 24 insertions(+), 40 deletions(-) rename src/libs/Environment/{Environment.js => Environment.ts} (69%) rename src/libs/Environment/betaChecker/{index.android.js => index.android.ts} (89%) rename src/libs/Environment/betaChecker/{index.ios.js => index.ios.ts} (67%) rename src/libs/Environment/betaChecker/{index.js => index.ts} (70%) rename src/libs/Environment/getEnvironment/{index.native.js => index.native.ts} (75%) rename src/libs/Environment/getEnvironment/{index.js => index.ts} (53%) diff --git a/src/libs/Environment/Environment.js b/src/libs/Environment/Environment.ts similarity index 69% rename from src/libs/Environment/Environment.js rename to src/libs/Environment/Environment.ts index c039b49d33aa0..f14e21eac90de 100644 --- a/src/libs/Environment/Environment.js +++ b/src/libs/Environment/Environment.ts @@ -1,5 +1,4 @@ import Config from 'react-native-config'; -import lodashGet from 'lodash/get'; import CONST from '../../CONST'; import getEnvironment from './getEnvironment'; import CONFIG from '../../CONFIG'; @@ -20,40 +19,32 @@ const OLDDOT_ENVIRONMENT_URLS = { /** * Are we running the app in development? - * - * @return {boolean} */ -function isDevelopment() { - return lodashGet(Config, 'ENVIRONMENT', CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.DEV; +function isDevelopment(): boolean { + return (Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.DEV; } /** * Are we running an internal test build? - * - * @return {boolean} */ -function isInternalTestBuild() { - return lodashGet(Config, 'ENVIRONMENT', CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.ADHOC && lodashGet(Config, 'PULL_REQUEST_NUMBER', ''); +function isInternalTestBuild(): boolean { + return !!((Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.ADHOC && (Config?.PULL_REQUEST_NUMBER ?? '')); } /** * Get the URL based on the environment we are in - * - * @returns {Promise} */ -function getEnvironmentURL() { +function getEnvironmentURL(): Promise { return new Promise((resolve) => { - getEnvironment().then((environment) => resolve(ENVIRONMENT_URLS[environment])); + getEnvironment().then((environment) => resolve(ENVIRONMENT_URLS[environment as keyof typeof ENVIRONMENT_URLS])); }); } /** * Get the corresponding oldDot URL based on the environment we are in - * - * @returns {Promise} */ -function getOldDotEnvironmentURL() { - return getEnvironment().then((environment) => OLDDOT_ENVIRONMENT_URLS[environment]); +function getOldDotEnvironmentURL(): Promise { + return getEnvironment().then((environment) => OLDDOT_ENVIRONMENT_URLS[environment as keyof typeof OLDDOT_ENVIRONMENT_URLS]); } export {getEnvironment, isInternalTestBuild, isDevelopment, getEnvironmentURL, getOldDotEnvironmentURL}; diff --git a/src/libs/Environment/betaChecker/index.android.js b/src/libs/Environment/betaChecker/index.android.ts similarity index 89% rename from src/libs/Environment/betaChecker/index.android.js rename to src/libs/Environment/betaChecker/index.android.ts index e74648973c342..8b3afed3b2f36 100644 --- a/src/libs/Environment/betaChecker/index.android.js +++ b/src/libs/Environment/betaChecker/index.android.ts @@ -8,15 +8,18 @@ import * as AppUpdate from '../../actions/AppUpdate'; let isLastSavedBeta = false; Onyx.connect({ key: ONYXKEYS.IS_BETA, - callback: (value) => (isLastSavedBeta = value), + callback: (value) => { + if (!value) { + return; + } + isLastSavedBeta = value; + }, }); /** * Check the GitHub releases to see if the current build is a beta build or production build - * - * @returns {Promise} */ -function isBetaBuild() { +function isBetaBuild(): Promise { return new Promise((resolve) => { fetch(CONST.GITHUB_RELEASE_URL) .then((res) => res.json()) diff --git a/src/libs/Environment/betaChecker/index.ios.js b/src/libs/Environment/betaChecker/index.ios.ts similarity index 67% rename from src/libs/Environment/betaChecker/index.ios.js rename to src/libs/Environment/betaChecker/index.ios.ts index 65b3ea935b04e..2d6079e30a1cb 100644 --- a/src/libs/Environment/betaChecker/index.ios.js +++ b/src/libs/Environment/betaChecker/index.ios.ts @@ -2,12 +2,10 @@ import {NativeModules} from 'react-native'; /** * Check to see if the build is staging (TestFlight) or production - * - * @returns {Promise} */ -function isBetaBuild() { +function isBetaBuild(): Promise { return new Promise((resolve) => { - NativeModules.EnvironmentChecker.isBeta().then((isBeta) => { + NativeModules.EnvironmentChecker.isBeta().then((isBeta: boolean) => { resolve(isBeta); }); }); diff --git a/src/libs/Environment/betaChecker/index.js b/src/libs/Environment/betaChecker/index.ts similarity index 70% rename from src/libs/Environment/betaChecker/index.js rename to src/libs/Environment/betaChecker/index.ts index 9d0d4af5741bf..cda7c297624fd 100644 --- a/src/libs/Environment/betaChecker/index.js +++ b/src/libs/Environment/betaChecker/index.ts @@ -1,9 +1,7 @@ /** * There's no beta build in non native - * - * @returns {Promise} */ -function isBetaBuild() { +function isBetaBuild(): Promise { return Promise.resolve(false); } diff --git a/src/libs/Environment/getEnvironment/index.native.js b/src/libs/Environment/getEnvironment/index.native.ts similarity index 75% rename from src/libs/Environment/getEnvironment/index.native.js rename to src/libs/Environment/getEnvironment/index.native.ts index 73014c4beffb0..1e3333c6a881d 100644 --- a/src/libs/Environment/getEnvironment/index.native.js +++ b/src/libs/Environment/getEnvironment/index.native.ts @@ -1,28 +1,25 @@ -import lodashGet from 'lodash/get'; import Config from 'react-native-config'; import betaChecker from '../betaChecker'; import CONST from '../../../CONST'; -let environment = null; +let environment: string | null = null; /** * Returns a promise that resolves with the current environment string value - * - * @returns {Promise} */ -function getEnvironment() { +function getEnvironment(): Promise { return new Promise((resolve) => { // If we've already set the environment, use the current value if (environment) { return resolve(environment); } - if (lodashGet(Config, 'ENVIRONMENT', CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.DEV) { + if ((Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.DEV) { environment = CONST.ENVIRONMENT.DEV; return resolve(environment); } - if (lodashGet(Config, 'ENVIRONMENT', CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.ADHOC) { + if ((Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.ADHOC) { environment = CONST.ENVIRONMENT.ADHOC; return resolve(environment); } diff --git a/src/libs/Environment/getEnvironment/index.js b/src/libs/Environment/getEnvironment/index.ts similarity index 53% rename from src/libs/Environment/getEnvironment/index.js rename to src/libs/Environment/getEnvironment/index.ts index a987678d6a6e2..2c27be5fc4711 100644 --- a/src/libs/Environment/getEnvironment/index.js +++ b/src/libs/Environment/getEnvironment/index.ts @@ -1,14 +1,11 @@ -import lodashGet from 'lodash/get'; import Config from 'react-native-config'; import CONST from '../../../CONST'; /** * Returns a promise that resolves with the current environment string value - * - * @returns {Promise} */ -function getEnvironment() { - return Promise.resolve(lodashGet(Config, 'ENVIRONMENT', CONST.ENVIRONMENT.DEV)); +function getEnvironment(): Promise { + return Promise.resolve(Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV); } export default getEnvironment; From 89173ee79f3c10f48de8d5449e45b843bf2019aa Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Tue, 3 Oct 2023 20:36:35 +0200 Subject: [PATCH 2/7] Create types for environment urls keys --- src/libs/Environment/Environment.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libs/Environment/Environment.ts b/src/libs/Environment/Environment.ts index f14e21eac90de..b201fd5be4390 100644 --- a/src/libs/Environment/Environment.ts +++ b/src/libs/Environment/Environment.ts @@ -17,6 +17,9 @@ const OLDDOT_ENVIRONMENT_URLS = { [CONST.ENVIRONMENT.ADHOC]: CONST.STAGING_EXPENSIFY_URL, }; +type EnvironmentUrlsKeys = keyof typeof ENVIRONMENT_URLS; +type OldDotEnvironmentUrlsKeys = keyof typeof OLDDOT_ENVIRONMENT_URLS; + /** * Are we running the app in development? */ @@ -36,7 +39,7 @@ function isInternalTestBuild(): boolean { */ function getEnvironmentURL(): Promise { return new Promise((resolve) => { - getEnvironment().then((environment) => resolve(ENVIRONMENT_URLS[environment as keyof typeof ENVIRONMENT_URLS])); + getEnvironment().then((environment) => resolve(ENVIRONMENT_URLS[environment as EnvironmentUrlsKeys])); }); } @@ -44,7 +47,7 @@ function getEnvironmentURL(): Promise { * Get the corresponding oldDot URL based on the environment we are in */ function getOldDotEnvironmentURL(): Promise { - return getEnvironment().then((environment) => OLDDOT_ENVIRONMENT_URLS[environment as keyof typeof OLDDOT_ENVIRONMENT_URLS]); + return getEnvironment().then((environment) => OLDDOT_ENVIRONMENT_URLS[environment as OldDotEnvironmentUrlsKeys]); } export {getEnvironment, isInternalTestBuild, isDevelopment, getEnvironmentURL, getOldDotEnvironmentURL}; From e2738d30b5e767e3568e4476fbc6438870c19607 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Wed, 11 Oct 2023 12:06:24 +0200 Subject: [PATCH 3/7] Remove assertion --- src/libs/Environment/Environment.ts | 7 ++----- src/libs/Environment/getEnvironment/index.native.ts | 5 +++-- src/libs/Environment/getEnvironment/index.ts | 5 +++-- src/libs/Environment/getEnvironment/types.ts | 6 ++++++ 4 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 src/libs/Environment/getEnvironment/types.ts diff --git a/src/libs/Environment/Environment.ts b/src/libs/Environment/Environment.ts index b201fd5be4390..60b83e0a1dfe3 100644 --- a/src/libs/Environment/Environment.ts +++ b/src/libs/Environment/Environment.ts @@ -17,9 +17,6 @@ const OLDDOT_ENVIRONMENT_URLS = { [CONST.ENVIRONMENT.ADHOC]: CONST.STAGING_EXPENSIFY_URL, }; -type EnvironmentUrlsKeys = keyof typeof ENVIRONMENT_URLS; -type OldDotEnvironmentUrlsKeys = keyof typeof OLDDOT_ENVIRONMENT_URLS; - /** * Are we running the app in development? */ @@ -39,7 +36,7 @@ function isInternalTestBuild(): boolean { */ function getEnvironmentURL(): Promise { return new Promise((resolve) => { - getEnvironment().then((environment) => resolve(ENVIRONMENT_URLS[environment as EnvironmentUrlsKeys])); + getEnvironment().then((environment) => resolve(ENVIRONMENT_URLS[environment])); }); } @@ -47,7 +44,7 @@ function getEnvironmentURL(): Promise { * Get the corresponding oldDot URL based on the environment we are in */ function getOldDotEnvironmentURL(): Promise { - return getEnvironment().then((environment) => OLDDOT_ENVIRONMENT_URLS[environment as OldDotEnvironmentUrlsKeys]); + return getEnvironment().then((environment) => OLDDOT_ENVIRONMENT_URLS[environment]); } export {getEnvironment, isInternalTestBuild, isDevelopment, getEnvironmentURL, getOldDotEnvironmentURL}; diff --git a/src/libs/Environment/getEnvironment/index.native.ts b/src/libs/Environment/getEnvironment/index.native.ts index 1e3333c6a881d..0e3b943b1bd58 100644 --- a/src/libs/Environment/getEnvironment/index.native.ts +++ b/src/libs/Environment/getEnvironment/index.native.ts @@ -1,13 +1,14 @@ import Config from 'react-native-config'; import betaChecker from '../betaChecker'; import CONST from '../../../CONST'; +import Environment from './types'; -let environment: string | null = null; +let environment: Environment | null = null; /** * Returns a promise that resolves with the current environment string value */ -function getEnvironment(): Promise { +function getEnvironment(): Promise { return new Promise((resolve) => { // If we've already set the environment, use the current value if (environment) { diff --git a/src/libs/Environment/getEnvironment/index.ts b/src/libs/Environment/getEnvironment/index.ts index 2c27be5fc4711..9a18a997eec6c 100644 --- a/src/libs/Environment/getEnvironment/index.ts +++ b/src/libs/Environment/getEnvironment/index.ts @@ -1,11 +1,12 @@ import Config from 'react-native-config'; import CONST from '../../../CONST'; +import Environment from './types'; /** * Returns a promise that resolves with the current environment string value */ -function getEnvironment(): Promise { - return Promise.resolve(Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV); +function getEnvironment(): Promise { + return Promise.resolve((Config?.ENVIRONMENT as Environment) ?? CONST.ENVIRONMENT.DEV); } export default getEnvironment; diff --git a/src/libs/Environment/getEnvironment/types.ts b/src/libs/Environment/getEnvironment/types.ts new file mode 100644 index 0000000000000..7cf17af3e0d85 --- /dev/null +++ b/src/libs/Environment/getEnvironment/types.ts @@ -0,0 +1,6 @@ +import {ValueOf} from 'type-fest'; +import CONST from '../../../CONST'; + +type Environment = ValueOf; + +export default Environment; From cb7efb9b6c9215e96170476572c7203845681683 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Wed, 11 Oct 2023 22:32:34 +0200 Subject: [PATCH 4/7] Add IsBetaBuild type --- src/libs/Environment/betaChecker/index.android.ts | 3 ++- src/libs/Environment/betaChecker/index.ios.ts | 3 ++- src/libs/Environment/betaChecker/index.ts | 4 +++- src/libs/Environment/betaChecker/types.ts | 3 +++ 4 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 src/libs/Environment/betaChecker/types.ts diff --git a/src/libs/Environment/betaChecker/index.android.ts b/src/libs/Environment/betaChecker/index.android.ts index 8b3afed3b2f36..e33b9c7feb6e6 100644 --- a/src/libs/Environment/betaChecker/index.android.ts +++ b/src/libs/Environment/betaChecker/index.android.ts @@ -4,6 +4,7 @@ import CONST from '../../../CONST'; import pkg from '../../../../package.json'; import ONYXKEYS from '../../../ONYXKEYS'; import * as AppUpdate from '../../actions/AppUpdate'; +import IsBetaBuild from './types'; let isLastSavedBeta = false; Onyx.connect({ @@ -19,7 +20,7 @@ Onyx.connect({ /** * Check the GitHub releases to see if the current build is a beta build or production build */ -function isBetaBuild(): Promise { +function isBetaBuild(): IsBetaBuild { return new Promise((resolve) => { fetch(CONST.GITHUB_RELEASE_URL) .then((res) => res.json()) diff --git a/src/libs/Environment/betaChecker/index.ios.ts b/src/libs/Environment/betaChecker/index.ios.ts index 2d6079e30a1cb..0d901fc4b0031 100644 --- a/src/libs/Environment/betaChecker/index.ios.ts +++ b/src/libs/Environment/betaChecker/index.ios.ts @@ -1,9 +1,10 @@ import {NativeModules} from 'react-native'; +import IsBetaBuild from './types'; /** * Check to see if the build is staging (TestFlight) or production */ -function isBetaBuild(): Promise { +function isBetaBuild(): IsBetaBuild { return new Promise((resolve) => { NativeModules.EnvironmentChecker.isBeta().then((isBeta: boolean) => { resolve(isBeta); diff --git a/src/libs/Environment/betaChecker/index.ts b/src/libs/Environment/betaChecker/index.ts index cda7c297624fd..541a3120ccce9 100644 --- a/src/libs/Environment/betaChecker/index.ts +++ b/src/libs/Environment/betaChecker/index.ts @@ -1,7 +1,9 @@ +import IsBetaBuild from './types'; + /** * There's no beta build in non native */ -function isBetaBuild(): Promise { +function isBetaBuild(): IsBetaBuild { return Promise.resolve(false); } diff --git a/src/libs/Environment/betaChecker/types.ts b/src/libs/Environment/betaChecker/types.ts new file mode 100644 index 0000000000000..61ce4bc9cec4f --- /dev/null +++ b/src/libs/Environment/betaChecker/types.ts @@ -0,0 +1,3 @@ +type IsBetaBuild = Promise; + +export default IsBetaBuild; From 86e1ef176839afdd8df5c625164b9e9c049266d9 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Fri, 13 Oct 2023 15:16:02 +0200 Subject: [PATCH 5/7] Resolve typecheck errors --- src/libs/ApiUtils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/ApiUtils.ts b/src/libs/ApiUtils.ts index 87a251ccb086e..54c7689dc4fb3 100644 --- a/src/libs/ApiUtils.ts +++ b/src/libs/ApiUtils.ts @@ -1,3 +1,4 @@ +import {ValueOf} from 'type-fest'; import Onyx from 'react-native-onyx'; import ONYXKEYS from '../ONYXKEYS'; import CONFIG from '../CONFIG'; @@ -8,7 +9,7 @@ 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 -let ENV_NAME = CONST.ENVIRONMENT.PRODUCTION; +let ENV_NAME: ValueOf = CONST.ENVIRONMENT.PRODUCTION; let shouldUseStagingServer = false; Environment.getEnvironment().then((envName) => { ENV_NAME = envName; From cca1382c56bfb1f37145e59fd4a60d22efa615b1 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Sun, 29 Oct 2023 19:46:36 +0100 Subject: [PATCH 6/7] Fix lint problems for Environment lib --- src/libs/Environment/getEnvironment/index.native.ts | 2 +- src/libs/Environment/getEnvironment/index.ts | 2 +- src/libs/Environment/getEnvironment/types.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/Environment/getEnvironment/index.native.ts b/src/libs/Environment/getEnvironment/index.native.ts index 7d15bb17a6259..766f288376b5b 100644 --- a/src/libs/Environment/getEnvironment/index.native.ts +++ b/src/libs/Environment/getEnvironment/index.native.ts @@ -1,7 +1,7 @@ import Config from 'react-native-config'; -import Environment from './types'; import betaChecker from '@libs/Environment/betaChecker'; import CONST from '@src/CONST'; +import Environment from './types'; let environment: Environment | null = null; diff --git a/src/libs/Environment/getEnvironment/index.ts b/src/libs/Environment/getEnvironment/index.ts index d6c475d2b356c..84f64e91649bc 100644 --- a/src/libs/Environment/getEnvironment/index.ts +++ b/src/libs/Environment/getEnvironment/index.ts @@ -1,6 +1,6 @@ import Config from 'react-native-config'; -import Environment from './types'; import CONST from '@src/CONST'; +import Environment from './types'; function getEnvironment(): Promise { return Promise.resolve((Config?.ENVIRONMENT as Environment) ?? CONST.ENVIRONMENT.DEV); diff --git a/src/libs/Environment/getEnvironment/types.ts b/src/libs/Environment/getEnvironment/types.ts index 7cf17af3e0d85..9247ed17ffe20 100644 --- a/src/libs/Environment/getEnvironment/types.ts +++ b/src/libs/Environment/getEnvironment/types.ts @@ -1,5 +1,5 @@ import {ValueOf} from 'type-fest'; -import CONST from '../../../CONST'; +import CONST from '@src/CONST'; type Environment = ValueOf; From 91dcf2b6920f70223c16cf422662c36e865a4206 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Sun, 29 Oct 2023 19:51:28 +0100 Subject: [PATCH 7/7] Resolve prettier error --- 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 d66cde3f4eed6..3d73471368979 100644 --- a/src/libs/ApiUtils.ts +++ b/src/libs/ApiUtils.ts @@ -1,5 +1,5 @@ -import {ValueOf} from 'type-fest'; import Onyx from 'react-native-onyx'; +import {ValueOf} from 'type-fest'; import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS';