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 'ApiUtils.js' lib to TypeScript #27935

Merged
merged 8 commits into from
Oct 2, 2023
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions src/libs/ApiUtils.js → src/libs/ApiUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import ONYXKEYS from '../ONYXKEYS';
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
Expand All @@ -16,29 +16,25 @@ 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;
return;
}

const defaultToggleState = ENV_NAME === CONST.ENVIRONMENT.STAGING || ENV_NAME === CONST.ENVIRONMENT.ADHOC;
shouldUseStagingServer = lodashGet(val, 'shouldUseStagingServer', defaultToggleState);
shouldUseStagingServer = value?.shouldUseStagingServer ?? defaultToggleState;
},
});
});

/**
* 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) {
const shouldUseSecure = lodashGet(request, 'shouldUseSecure', false);
function getApiRoot(request?: Pick<Request, 'shouldUseSecure'>): string {
const shouldUseSecure = request?.shouldUseSecure ?? false;
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB: Why not just type Request?

Copy link
Contributor

Choose a reason for hiding this comment

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

Does Request have a shouldUseSecure parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


if (shouldUseStagingServer) {
if (CONFIG.IS_USING_WEB_PROXY) {
Expand All @@ -52,22 +48,16 @@ function getApiRoot(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
*/
madmax330 marked this conversation as resolved.
Show resolved Hide resolved
function getCommandURL(request) {
function getCommandURL(request: Request): string {
return `${getApiRoot(request)}api?command=${request.command}`;
}

/**
* Check if we're currently using the staging API root
*
* @returns {Boolean}
*/
function isUsingStagingApi() {
function isUsingStagingApi(): boolean {
return shouldUseStagingServer;
}

Expand Down
Loading