Skip to content

Commit

Permalink
Create api.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bufanoc authored Dec 23, 2024
1 parent 01fea64 commit 9df4aa4
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions frontend/src/services/api.js
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;

0 comments on commit 9df4aa4

Please sign in to comment.