Skip to content
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

[No QA] [TS migration] Migrate 'Environment' lib to TypeScript #28600

3 changes: 2 additions & 1 deletion src/libs/ApiUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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';
Expand All @@ -8,7 +9,7 @@ import * as Environment from './Environment/Environment';

// 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<typeof CONST.ENVIRONMENT> = CONST.ENVIRONMENT.PRODUCTION;
let shouldUseStagingServer = false;
Environment.getEnvironment().then((envName) => {
ENV_NAME = envName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import lodashGet from 'lodash/get';
import Config from 'react-native-config';
import CONFIG from '@src/CONFIG';
import CONST from '@src/CONST';
Expand All @@ -20,39 +19,31 @@ 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 ?? ''));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: I think the !! here could just be around the pull request number part


/**
* Get the URL based on the environment we are in
*
* @returns {Promise}
*/
function getEnvironmentURL() {
function getEnvironmentURL(): Promise<string> {
return new Promise((resolve) => {
getEnvironment().then((environment) => resolve(ENVIRONMENT_URLS[environment]));
});
}

/**
* Get the corresponding oldDot URL based on the environment we are in
*
* @returns {Promise<string>}
*/
function getOldDotEnvironmentURL() {
function getOldDotEnvironmentURL(): Promise<string> {
return getEnvironment().then((environment) => OLDDOT_ENVIRONMENT_URLS[environment]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import * as AppUpdate from '@userActions/AppUpdate';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import pkg from '../../../../package.json';
import IsBetaBuild from './types';

let isLastSavedBeta = false;
Onyx.connect({
key: ONYXKEYS.IS_BETA,
callback: (value) => (isLastSavedBeta = value),
callback: (value) => {
isLastSavedBeta = Boolean(value);
},
});

/**
* Check the GitHub releases to see if the current build is a beta build or production build
*
* @returns {Promise}
*/
function isBetaBuild() {
function isBetaBuild(): IsBetaBuild {
return new Promise((resolve) => {
fetch(CONST.GITHUB_RELEASE_URL)
.then((res) => res.json())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {NativeModules} from 'react-native';
import IsBetaBuild from './types';

/**
* Check to see if the build is staging (TestFlight) or production
*
* @returns {Promise}
*/
function isBetaBuild() {
function isBetaBuild(): IsBetaBuild {
return new Promise((resolve) => {
NativeModules.EnvironmentChecker.isBeta().then((isBeta) => {
NativeModules.EnvironmentChecker.isBeta().then((isBeta: boolean) => {
resolve(isBeta);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import IsBetaBuild from './types';

/**
* There's no beta build in non native
*
* @returns {Promise}
*/
function isBetaBuild() {
function isBetaBuild(): IsBetaBuild {
return Promise.resolve(false);
}

Expand Down
3 changes: 3 additions & 0 deletions src/libs/Environment/betaChecker/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type IsBetaBuild = Promise<boolean>;

export default IsBetaBuild;
14 changes: 0 additions & 14 deletions src/libs/Environment/getEnvironment/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import lodashGet from 'lodash/get';
import Config from 'react-native-config';
import betaChecker from '@libs/Environment/betaChecker';
import CONST from '@src/CONST';
import Environment from './types';

let environment = null;
let environment: Environment | null = null;

/**
* Returns a promise that resolves with the current environment string value
*
* @returns {Promise}
*/
function getEnvironment() {
function getEnvironment(): Promise<Environment> {
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);
}
Expand Down
9 changes: 9 additions & 0 deletions src/libs/Environment/getEnvironment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Config from 'react-native-config';
import CONST from '@src/CONST';
import Environment from './types';

function getEnvironment(): Promise<Environment> {
return Promise.resolve((Config?.ENVIRONMENT as Environment) ?? CONST.ENVIRONMENT.DEV);
}

export default getEnvironment;
6 changes: 6 additions & 0 deletions src/libs/Environment/getEnvironment/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {ValueOf} from 'type-fest';
import CONST from '@src/CONST';

type Environment = ValueOf<typeof CONST.ENVIRONMENT>;

export default Environment;
Loading