Skip to content

Commit

Permalink
Took out most usages of process.env and replaced with config file. (#161
Browse files Browse the repository at this point in the history
)

* Took out most usages of process.env and replaced with config file.

Except for src/ServiceWorker.js and ./cypress/plugins because I don't
think it's a super good idea to change anything in those.

* Made changes based on review+discussions from PR #161.
  • Loading branch information
thai-truong authored Sep 15, 2020
1 parent 954fb42 commit abf2501
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 33 deletions.
7 changes: 6 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@
{
"pattern": "@Contexts/*",
"group": "internal"
},
{
"pattern": "@Config/*",
"group": "internal"
}
]
}
Expand All @@ -94,7 +98,8 @@
["@Pages", "./src/pages"],
["@HOCs", "./src/HOCs"],
["@Images", "./src/images"],
["@Contexts", "./src/contexts"]
["@Contexts", "./src/contexts"],
["@Config", "./src/config"]
],
"extensions": [".js", ".jsx", ".json", ".ts", ".tsx"]
},
Expand Down
1 change: 1 addition & 0 deletions config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ module.exports = override(
'@HOCs': path.resolve(__dirname, './src/HOCs'),
'@Images': path.resolve(__dirname, './src/images'),
'@Contexts': path.resolve(__dirname, './src/contexts'),
'@Config': path.resolve(__dirname, './src/config'),
})
);
23 changes: 23 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type Config = {
apiKey?: string;
authDomain?: string;
databaseURL?: string;
projectID?: string;
storageBucket?: string;
messagingSenderID?: string;
appID?: string;
apiURL?: string;
nodeEnv?: string;
};

export const config: Config = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_DATABASE_URL,
projectID: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
messagingSenderID: process.env.REACT_APP_MESSAGING_SENDER_ID,
appID: process.env.REACT_APP_APP_ID,
apiURL: process.env.REACT_APP_API_URL,
nodeEnv: process.env.NODE_ENV,
};
17 changes: 9 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import 'firebase/firestore';
import * as serviceWorker from './serviceWorker';

import App from '@Pages/App';
import { config } from '@Config';

const config = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_DATABASE_URL,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
const appConfig = {
apiKey: config.apiKey,
authDomain: config.authDomain,
databaseURL: config.databaseURL,
projectId: config.projectID,
storageBucket: config.storageBucket,
messagingSenderId: config.messagingSenderID,
};

firebase.initializeApp(config);
firebase.initializeApp(appConfig);

document.body.style.height = '100%';
document.body.style.margin = '0';
Expand Down
3 changes: 2 additions & 1 deletion src/pages/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
OfficerRoutingPermission,
} from '@HOCs/RoutingPermissions';
import ApiConfigStore from '@Services/ApiConfigStore';
import { config } from '@Config';

function App(): JSX.Element {
const [userClaims, setUserClaims] = useState<UserContextValues | null>(null);
Expand Down Expand Up @@ -146,4 +147,4 @@ function App(): JSX.Element {
);
}

export default process.env.NODE_ENV === 'development' ? hot(App) : App;
export default config.nodeEnv === 'development' ? hot(App) : App;
7 changes: 3 additions & 4 deletions src/services/ApiConfigStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import { ConfigurationParameters, Configuration } from './api';

import { config } from '@Config';

class ApiConfigStoreClass {
private configParams: ConfigurationParameters = {
// TODO: load this from a config instead
basePath:
process.env.NODE_ENV === 'development'
? 'http://localhost:3001'
: 'https://dev-api.hknucsd.com',
basePath: config.apiURL,
};

private config: Configuration = new Configuration(this.configParams);
Expand Down
18 changes: 0 additions & 18 deletions src/services/auth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import * as firebase from 'firebase/app';
import 'firebase/auth';

import SIGNUP_ENDPOINT from '@Constants/endpoints';

const SIGN_UP_URL = `${process.env.REACT_APP_API_BASE_URL}${SIGNUP_ENDPOINT}`;

// Auth API
const doCreateUserWithEmailAndPassword = (email, password) => {
return firebase.auth().createUserWithEmailAndPassword(email, password);
Expand Down Expand Up @@ -44,19 +40,6 @@ const getCurrentUserClaims = async () => {
return tokenResult.claims;
};

const createUserAccountFromSignup = async signupSubmission => {
const response = await fetch(SIGN_UP_URL, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(signupSubmission),
});

return response;
};

export {
doCreateUserWithEmailAndPassword,
doSignInWithEmailAndPassword,
Expand All @@ -65,5 +48,4 @@ export {
doSendVerificationEmail,
doPasswordUpdate,
getCurrentUserClaims,
createUserAccountFromSignup,
};
3 changes: 2 additions & 1 deletion tsconfig.paths.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"@HOCs/*": ["src/HOCs/*"],
"@Images": ["src/images"],
"@Images/*": ["src/images/*"],
"@Contexts": ["src/contexts"]
"@Contexts": ["src/contexts"],
"@Config": ["src/config"]
}
}
}

0 comments on commit abf2501

Please sign in to comment.