-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor configuration handling in webapp
- Move configuration values to a separate JSON file - Add fetchConfig function to retrieve configuration based on environment - Update services to use fetchApiEndPoint for API base URI - Remove API_BASE_URI constant from services - Update fetch calls in services to use fetchApiEndPoint for endpoint URLs
- Loading branch information
1 parent
bbf0484
commit 9ba0736
Showing
7 changed files
with
106 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"development": { | ||
"react_app_devcamper_base_api_uri": "http://localhost:5000/api/v1" | ||
}, | ||
"production": { | ||
"react_app_devcamper_base_api_uri": "http://devcamper.webapi/api/v1" | ||
} | ||
} |
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
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,28 @@ | ||
let config = null; | ||
|
||
export const fetchConfig = async () => { | ||
if (!config) { | ||
const response = await fetch('/config/config.json'); | ||
if (!response.ok) { | ||
throw new Error('Failed to fetch configuration'); | ||
} | ||
const json = await response.json(); | ||
const env = process.env.NODE_ENV || 'development'; | ||
console.log(`Using configuration for environment: ${env}`); | ||
console.log(`Configuration: ${JSON.stringify(json[env], null, 2)}`); | ||
config = json[env]; | ||
} | ||
return config; | ||
}; | ||
|
||
export const getConfigValue = async (key) => { | ||
const config = await fetchConfig(); | ||
return config[key]; | ||
}; | ||
|
||
export const fetchApiEndPoint = async (endpoint) => { | ||
const config = await fetchConfig(); | ||
const api = config.react_app_devcamper_base_api_uri; | ||
console.log(`fetchApi endpoint: ${api}${endpoint}`); | ||
return `${api}${endpoint}`; | ||
}; |