-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15178 from kidroca/kidroca/refactor/api-root
Refactor: Create libs/ApiUtils
- Loading branch information
Showing
13 changed files
with
120 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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'; | ||
|
||
// 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 shouldUseStagingServer = false; | ||
Environment.getEnvironment() | ||
.then((envName) => { | ||
ENV_NAME = envName; | ||
|
||
// We connect here, so we have the updated ENV_NAME when Onyx callback runs | ||
Onyx.connect({ | ||
key: ONYXKEYS.USER, | ||
callback: (val) => { | ||
// Toggling between APIs is not allowed on production | ||
if (ENV_NAME === CONST.ENVIRONMENT.PRODUCTION) { | ||
shouldUseStagingServer = false; | ||
return; | ||
} | ||
|
||
const defaultToggleState = ENV_NAME === CONST.ENVIRONMENT.STAGING; | ||
shouldUseStagingServer = lodashGet(val, '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); | ||
|
||
if (shouldUseStagingServer) { | ||
return shouldUseSecure | ||
? CONFIG.EXPENSIFY.STAGING_SECURE_API_ROOT | ||
: CONFIG.EXPENSIFY.STAGING_API_ROOT; | ||
} | ||
|
||
return shouldUseSecure | ||
? CONFIG.EXPENSIFY.DEFAULT_SECURE_API_ROOT | ||
: CONFIG.EXPENSIFY.DEFAULT_API_ROOT; | ||
} | ||
|
||
/** | ||
* 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} | ||
*/ | ||
function getCommandURL(request) { | ||
return `${getApiRoot(request)}api?command=${request.command}`; | ||
} | ||
|
||
/** | ||
* Check if we're currently using the staging API root | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function isUsingStagingApi() { | ||
return shouldUseStagingServer; | ||
} | ||
|
||
export { | ||
getApiRoot, | ||
getCommandURL, | ||
isUsingStagingApi, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
import Config from '../CONFIG'; | ||
import * as ApiUtils from './ApiUtils'; | ||
|
||
// Absolute URLs (`/` or `//`) should be resolved from API ROOT | ||
// Legacy attachments can come from either staging or prod, depending on the env they were uploaded by | ||
// Both should be replaced and loaded from API ROOT of the current environment | ||
const ORIGINS_TO_REPLACE = ['/+', Config.EXPENSIFY.EXPENSIFY_URL, Config.EXPENSIFY.STAGING_EXPENSIFY_URL]; | ||
const ORIGINS_TO_REPLACE = [ | ||
'/+', | ||
Config.EXPENSIFY.EXPENSIFY_URL, | ||
Config.EXPENSIFY.STAGING_API_ROOT, | ||
Config.EXPENSIFY.DEFAULT_API_ROOT, | ||
]; | ||
|
||
// Anything starting with a match from ORIGINS_TO_REPLACE | ||
const ORIGIN_PATTERN = new RegExp(`^(${ORIGINS_TO_REPLACE.join('|')})`); | ||
|
||
/** | ||
* When possible resolve sources relative to API ROOT | ||
* Updates applicable URLs, so they are accessed relative to URL_API_ROOT | ||
* When possible this function resolve URLs relative to API ROOT | ||
* - Absolute URLs like `/{path}`, become: `https://{API_ROOT}/{path}` | ||
* - Similarly for prod or staging URLs we replace the `https://www.expensify` | ||
* or `https://staging.expensify` part, with `https://{API_ROOT}` | ||
* - Unmatched URLs are returned with no modifications | ||
* - Unmatched URLs (non expensify) are returned with no modifications | ||
* | ||
* @param {String} url | ||
* @returns {String} | ||
*/ | ||
export default function tryResolveUrlFromApiRoot(url) { | ||
return url.replace(ORIGIN_PATTERN, Config.EXPENSIFY.URL_API_ROOT); | ||
const apiRoot = ApiUtils.getApiRoot({shouldUseSecure: false}); | ||
return url.replace(ORIGIN_PATTERN, apiRoot); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters