Repository navigation
Refactor: Create libs/ApiUtils #15178
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
743ae89
CONFIG - rename API ROOT urls for clarity
kidroca db491dd
Create `libs/ApiUtils`
kidroca a851403
Create `ApiUtils.getCommandUrl` and replace usage in HttpUtils
kidroca 4c60df7
Update `tryResolveUrlFromApiRoot` to use `ApiUtils.getApiRoot`
kidroca 92bb599
ApiUtils - remove platform specific implementations and use a single …
kidroca 8cf601b
Update `TestToolMenu` to use `ApiUtils`
kidroca 9c64b57
Delete unused `./libs/shouldUseStagingServer/`
kidroca 6df8c6f
Rename CONFIG.PRODUCTION_API_ROOT to PRIMARY_API_ROOT
kidroca 4708122
Apply suggestions from code review
kidroca 595bb23
Document PRIMARY_API_ROOT
kidroca 6d88ebf
Update AddressSearch to use ApiUtils instead of referencing API_ROOT …
kidroca 8f85692
ApiUtils: Add missing docks
kidroca b75f313
ApiUtils: make `getApiRoot` `request` param optional
kidroca ebdb915
ApiUtils: apply code review suggestions to simplify `canUseStagingSer…
kidroca 0269102
Rename CONFIG.PRIMARY_API_ROOT to DEFAULT
kidroca ca66fba
ApiUtils - Apply suggestions from code review
kidroca 39fd3c6
ApiUtils rename `getCommandUrl` -> `getCommandURL`
kidroca 7b49753
Add requested comments for clarity
kidroca 9be14bc
ApiUtils - Move `Onyx.connect` inside `getEnvironment` promise
kidroca e3d84f9
Update NetworkConnection - use the updated CONFIG value - DEFAULT_API…
kidroca 3be8792
ApiUtils - remove `canUseStagingServer`
kidroca c197b41
Docs: Update src/libs/ApiUtils.js
kidroca d641fa8
Docs: TestToolMenu code comments
kidroca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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() { | ||
|
kidroca marked this conversation as resolved.
Resolved
mountiny marked this conversation as resolved.
Resolved
|
||
| return shouldUseStagingServer; | ||
| } | ||
|
|
||
| export { | ||
| getApiRoot, | ||
| getCommandURL, | ||
| isUsingStagingApi, | ||
| }; | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.