-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 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,58 @@ | ||
import axios from 'axios'; | ||
|
||
// Get the current hostname and port for the API | ||
const getApiUrl = () => { | ||
const hostname = window.location.hostname; | ||
return `http://${hostname}:5000/api`; | ||
}; | ||
|
||
const api = axios.create({ | ||
baseURL: getApiUrl(), | ||
timeout: 10000, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
// Add request interceptor | ||
api.interceptors.request.use( | ||
(config) => { | ||
// You can add authentication headers here if needed | ||
return config; | ||
}, | ||
(error) => { | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
// Add response interceptor | ||
api.interceptors.response.use( | ||
(response) => { | ||
return response; | ||
}, | ||
(error) => { | ||
if (error.response) { | ||
// Handle specific error cases | ||
switch (error.response.status) { | ||
case 401: | ||
// Handle unauthorized | ||
break; | ||
case 403: | ||
// Handle forbidden | ||
break; | ||
case 404: | ||
// Handle not found | ||
break; | ||
case 500: | ||
// Handle server error | ||
break; | ||
default: | ||
// Handle other errors | ||
break; | ||
} | ||
} | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
export default api; |