Skip to content

Instantly share code, notes, and snippets.

@0ex-d
Created August 4, 2020 23:32
Show Gist options
  • Save 0ex-d/e48d73b342c625a5704ab16167266c02 to your computer and use it in GitHub Desktop.
Save 0ex-d/e48d73b342c625a5704ab16167266c02 to your computer and use it in GitHub Desktop.
React Javascript environment config for various URL rules. Used in my React Native project.
/*
* Configure various url rules per environment
* Use `envSelector` and createSelector hooks to load env.json file
* By Precious Akin
*/
import { createSelector } from 'reselect';
import ENV_FILE from '../../env.json';
const envSelector = () => ENV_FILE;
export const apiURL = createSelector(
envSelector,
ENV => {
switch (ENV.network) {
case 'dev':
return `http://${ENV.offline_host || '0.0.0.0'}:9100`
case 'staging':
return ''
case 'production':
return 'https://prod-app.com'
default:
return 'https://staging-app.com'
}
}
);
@0ex-d
Copy link
Author

0ex-d commented Aug 5, 2020

Here is the json schema

{
"network": "production",
"offline_host": "127.0.0.1",
}

@0ex-d
Copy link
Author

0ex-d commented Aug 5, 2020

Call function in axios or fetch
let health_link = await axios.get(${apiURL}/health);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment