-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor fetchData to support dynamic query parameters and update App…
….js to use new fetchData structure - Updated fetchData.js to build URLs dynamically with query parameters - Modified App.js to pass query parameters to fetchData for fetching environment details - Improved error handling for fetch requests
- Loading branch information
Samyak Rout
committed
Nov 14, 2024
1 parent
64e3814
commit 19b71cb
Showing
9 changed files
with
117 additions
and
31 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,17 @@ | ||
// config.js | ||
|
||
// Set default values for API_HOST and API_PORT | ||
const API_HOST = process.env.REACT_APP_API_HOST || "http://localhost"; | ||
const API_PORT = process.env.REACT_APP_API_PORT || "8080"; | ||
|
||
// Construct the base API URL | ||
const API_BASE_URL = `${API_HOST}:${API_PORT}/api`; | ||
|
||
// Export config with dynamically constructed URLs | ||
const config = { | ||
fetchCompanyDetails: "http://localhost:8080/api", | ||
fetchInfraTypes: "http://localhost:8080/api", | ||
fetchInternalEnvDetails: "http://localhost:8080/api", | ||
fetchCompanyDetails: `${API_BASE_URL}/company`, | ||
fetchInfraTypes: `${API_BASE_URL}/infra-types`, | ||
fetchInternalEnvDetails: `${API_BASE_URL}/internal-env-details`, | ||
}; | ||
|
||
export default config; | ||
export default config; |
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,33 @@ | ||
// src/services/fetchData.js | ||
|
||
import config from "../config"; | ||
|
||
export async function fetchData(endpointKey, params = {}) { | ||
// Get the base URL based on the endpoint key in config | ||
const baseUrl = config[endpointKey]; | ||
if (!baseUrl) { | ||
throw new Error(`Invalid endpoint key: ${endpointKey}`); | ||
} | ||
|
||
// Construct the full URL with query parameters | ||
const url = new URL(baseUrl); | ||
Object.keys(params).forEach(key => { | ||
if (params[key]) { | ||
url.searchParams.append(key, params[key]); | ||
} | ||
}); | ||
|
||
try { | ||
// Perform the fetch call with the constructed URL | ||
const response = await fetch(url.toString()); | ||
if (!response.ok) { | ||
throw new Error(`Error: ${response.statusText}`); | ||
} | ||
|
||
// Return the parsed JSON data | ||
return await response.json(); | ||
} catch (error) { | ||
console.error(`Failed to fetch data from ${url}:`, error); | ||
throw error; | ||
} | ||
} |
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