From 7d3cc8d6b1bdc4e7e625913ceb5093dda1a72bc4 Mon Sep 17 00:00:00 2001 From: Thai Date: Sat, 12 Sep 2020 15:36:07 -0700 Subject: [PATCH 1/2] 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. --- .eslintrc | 7 ++++- config-overrides.js | 1 + package-lock.json | 18 ++++++------ src/config.ts | 23 +++++++++++++++ src/index.tsx | 17 ++++++----- src/pages/App/index.tsx | 3 +- src/services/ApiConfigStore.ts | 7 ++--- src/services/auth.js | 3 +- tsconfig.json | 54 +++++++++++++++++----------------- tsconfig.paths.json | 3 +- 10 files changed, 84 insertions(+), 52 deletions(-) create mode 100644 src/config.ts diff --git a/.eslintrc b/.eslintrc index b39c1616..85f69afd 100644 --- a/.eslintrc +++ b/.eslintrc @@ -78,6 +78,10 @@ { "pattern": "@Contexts/*", "group": "internal" + }, + { + "pattern": "@Config/*", + "group": "internal" } ] } @@ -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"] }, diff --git a/config-overrides.js b/config-overrides.js index 7eb24347..99daa082 100644 --- a/config-overrides.js +++ b/config-overrides.js @@ -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'), }) ); diff --git a/package-lock.json b/package-lock.json index 411f45c1..274825bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12716,7 +12716,7 @@ }, "chownr": { "version": "1.1.2", - "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.2.tgz", + "resolved": false, "integrity": "sha512-GkfeAQh+QNy3wquu9oIZr6SS5x7wGdSgNQvD10X3r+AZr1Oys22HW8kAmDMvNg2+Dm0TeGaEuO8gFwdBXxwO8A==" }, "code-point-at": { @@ -12764,7 +12764,7 @@ }, "fs-minipass": { "version": "1.2.6", - "resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.6.tgz", + "resolved": false, "integrity": "sha512-crhvyXcMejjv3Z5d2Fa9sf5xLYVCF5O1c71QxbVnbLsmYMBEvDAftewesN/HhY03YRoA7zOMxjNGrF5svGaaeQ==", "requires": { "minipass": "^2.2.1" @@ -12866,12 +12866,12 @@ }, "minimist": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": false, "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" }, "minipass": { "version": "2.3.5", - "resolved": "https://registry.npmjs.org/minipass/-/minipass-2.3.5.tgz", + "resolved": false, "integrity": "sha512-Gi1W4k059gyRbyVUZQ4mEqLm0YIUiGYfvxhF6SIlk3ui1WVxMTGfGdQ2SInh3PDrRTVvPKgULkpJtT4RH10+VA==", "requires": { "safe-buffer": "^5.1.2", @@ -12880,7 +12880,7 @@ }, "minizlib": { "version": "1.2.1", - "resolved": "https://registry.npmjs.org/minizlib/-/minizlib-1.2.1.tgz", + "resolved": false, "integrity": "sha512-7+4oTUOWKg7AuL3vloEWekXY2/D20cevzsrNT2kGWm+39J9hGTCBv8VI5Pm5lXZ/o3/mdR4f8rflAPhnQb8mPA==", "requires": { "minipass": "^2.2.1" @@ -12888,7 +12888,7 @@ }, "mkdirp": { "version": "0.5.1", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "resolved": false, "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", "requires": { "minimist": "0.0.8" @@ -12896,7 +12896,7 @@ "dependencies": { "minimist": { "version": "0.0.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "resolved": false, "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" } } @@ -13121,7 +13121,7 @@ }, "tar": { "version": "4.4.10", - "resolved": "https://registry.npmjs.org/tar/-/tar-4.4.10.tgz", + "resolved": false, "integrity": "sha512-g2SVs5QIxvo6OLp0GudTqEf05maawKUxXru104iaayWA09551tFCTI8f1Asb4lPfkBr91k07iL4c11XO3/b0tA==", "requires": { "chownr": "^1.1.1", @@ -13153,7 +13153,7 @@ }, "yallist": { "version": "3.0.3", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz", + "resolved": false, "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==" } } diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 00000000..c522af70 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,23 @@ +type Config = { + apiKey?: string; + authDomain?: string; + databaseURL?: string; + projectID?: string; + storageBucket?: string; + messagingSenderID?: string; + appID?: string; + apiBaseURL?: 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, + apiBaseURL: process.env.REACT_APP_API_BASE_URL, + nodeEnv: process.env.NODE_ENV, +}; diff --git a/src/index.tsx b/src/index.tsx index d0d51f3b..bf9aad46 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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, + storageBuck: config.storageBucket, + messagingSenderId: config.messagingSenderID, }; -firebase.initializeApp(config); +firebase.initializeApp(appConfig); document.body.style.height = '100%'; document.body.style.margin = '0'; diff --git a/src/pages/App/index.tsx b/src/pages/App/index.tsx index 06323435..0b1c920a 100644 --- a/src/pages/App/index.tsx +++ b/src/pages/App/index.tsx @@ -26,6 +26,7 @@ import { OfficerRoutingPermission, } from '@HOCs/RoutingPermissions'; import ApiConfigStore from '@Services/ApiConfigStore'; +import { config } from '@Config'; function App(): JSX.Element { const [userClaims, setUserClaims] = useState(null); @@ -140,4 +141,4 @@ function App(): JSX.Element { ); } -export default process.env.NODE_ENV === 'development' ? hot(App) : App; +export default config.nodeEnv === 'development' ? hot(App) : App; diff --git a/src/services/ApiConfigStore.ts b/src/services/ApiConfigStore.ts index 14376758..61d71b2c 100644 --- a/src/services/ApiConfigStore.ts +++ b/src/services/ApiConfigStore.ts @@ -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.apiBaseURL, }; private config: Configuration = new Configuration(this.configParams); diff --git a/src/services/auth.js b/src/services/auth.js index d746c0fb..0daab6c4 100644 --- a/src/services/auth.js +++ b/src/services/auth.js @@ -2,8 +2,9 @@ import * as firebase from 'firebase/app'; import 'firebase/auth'; import SIGNUP_ENDPOINT from '@Constants/endpoints'; +import { config } from '@Config'; -const SIGN_UP_URL = `${process.env.REACT_APP_API_BASE_URL}${SIGNUP_ENDPOINT}`; +const SIGN_UP_URL = `${config.apiBaseURL}${SIGNUP_ENDPOINT}`; // Auth API const doCreateUserWithEmailAndPassword = (email, password) => { diff --git a/tsconfig.json b/tsconfig.json index d4fca364..75325e2f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,27 +1,27 @@ -{ - "compilerOptions": { - "target": "es5", - "lib": [ - "dom", - "dom.iterable", - "esnext" - ], - "allowJs": true, - "skipLibCheck": true, - "esModuleInterop": true, - "allowSyntheticDefaultImports": true, - "strict": true, - "forceConsistentCasingInFileNames": true, - "module": "esnext", - "moduleResolution": "node", - "resolveJsonModule": true, - "isolatedModules": true, - "noEmit": true, - "jsx": "react", - "baseUrl": "." - }, - "include": [ - "src" - ], - "extends": "./tsconfig.paths.json" -} +{ + "compilerOptions": { + "target": "es5", + "lib": [ + "dom", + "dom.iterable", + "esnext" + ], + "allowJs": true, + "skipLibCheck": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "module": "esnext", + "moduleResolution": "node", + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "react", + "baseUrl": "." + }, + "include": [ + "src" + ], + "extends": "./tsconfig.paths.json" +} diff --git a/tsconfig.paths.json b/tsconfig.paths.json index e3cc3c97..9b202231 100644 --- a/tsconfig.paths.json +++ b/tsconfig.paths.json @@ -14,7 +14,8 @@ "@HOCs/*": ["src/HOCs/*"], "@Images": ["src/images"], "@Images/*": ["src/images/*"], - "@Contexts": ["src/contexts"] + "@Contexts": ["src/contexts"], + "@Config": ["src/config"] } } } From 998f3ebf69b9df28205deaf550073b01793574df Mon Sep 17 00:00:00 2001 From: Thai Date: Sun, 13 Sep 2020 14:08:51 -0700 Subject: [PATCH 2/2] Made changes based on review+discussions from PR #161. --- src/config.ts | 4 ++-- src/index.tsx | 2 +- src/services/ApiConfigStore.ts | 2 +- src/services/auth.js | 19 ------------------- 4 files changed, 4 insertions(+), 23 deletions(-) diff --git a/src/config.ts b/src/config.ts index c522af70..4677d170 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,7 +6,7 @@ type Config = { storageBucket?: string; messagingSenderID?: string; appID?: string; - apiBaseURL?: string; + apiURL?: string; nodeEnv?: string; }; @@ -18,6 +18,6 @@ export const config: Config = { storageBucket: process.env.REACT_APP_STORAGE_BUCKET, messagingSenderID: process.env.REACT_APP_MESSAGING_SENDER_ID, appID: process.env.REACT_APP_APP_ID, - apiBaseURL: process.env.REACT_APP_API_BASE_URL, + apiURL: process.env.REACT_APP_API_URL, nodeEnv: process.env.NODE_ENV, }; diff --git a/src/index.tsx b/src/index.tsx index bf9aad46..8a8c2464 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -13,7 +13,7 @@ const appConfig = { authDomain: config.authDomain, databaseURL: config.databaseURL, projectId: config.projectID, - storageBuck: config.storageBucket, + storageBucket: config.storageBucket, messagingSenderId: config.messagingSenderID, }; diff --git a/src/services/ApiConfigStore.ts b/src/services/ApiConfigStore.ts index 61d71b2c..fbe338e1 100644 --- a/src/services/ApiConfigStore.ts +++ b/src/services/ApiConfigStore.ts @@ -7,7 +7,7 @@ import { config } from '@Config'; class ApiConfigStoreClass { private configParams: ConfigurationParameters = { // TODO: load this from a config instead - basePath: config.apiBaseURL, + basePath: config.apiURL, }; private config: Configuration = new Configuration(this.configParams); diff --git a/src/services/auth.js b/src/services/auth.js index 0daab6c4..ade571c7 100644 --- a/src/services/auth.js +++ b/src/services/auth.js @@ -1,11 +1,6 @@ import * as firebase from 'firebase/app'; import 'firebase/auth'; -import SIGNUP_ENDPOINT from '@Constants/endpoints'; -import { config } from '@Config'; - -const SIGN_UP_URL = `${config.apiBaseURL}${SIGNUP_ENDPOINT}`; - // Auth API const doCreateUserWithEmailAndPassword = (email, password) => { return firebase.auth().createUserWithEmailAndPassword(email, password); @@ -45,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, @@ -66,5 +48,4 @@ export { doSendVerificationEmail, doPasswordUpdate, getCurrentUserClaims, - createUserAccountFromSignup, };