Skip to content

Commit

Permalink
Merge pull request #27935 from kubabutkiewicz/ts-migration/ApiUtils-lib
Browse files Browse the repository at this point in the history
[No QA] [TS migration] Migrate 'ApiUtils.js' lib to TypeScript
  • Loading branch information
madmax330 authored Oct 2, 2023
2 parents cc76174 + 6af0606 commit 5c9171a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
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?: Request): string {
const shouldUseSecure = request?.shouldUseSecure ?? false;

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
*/
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
3 changes: 2 additions & 1 deletion src/libs/tryResolveUrlFromApiRoot.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}

Expand Down

0 comments on commit 5c9171a

Please sign in to comment.