bryanbalmer's picture
Update all the front end files to follow best practices and implement the backend
a25197f verified
Raw
History Blame Contribute Delete
2.26 kB
```javascript
import axios from 'axios';
const API_BASE_URL = process.env.VUE_APP_API_URL || 'http://localhost:3000/api';
const api = axios.create({
baseURL: API_BASE_URL,
timeout: 10000,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
// Add request interceptor for auth
api.interceptors.request.use((config) => {
const token = localStorage.getItem('authToken');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
}, (error) => {
return Promise.reject(error);
});
// Add response interceptor for error handling
api.interceptors.response.use((response) => {
return response.data;
}, (error) => {
if (error.response) {
switch (error.response.status) {
case 401:
// Handle unauthorized
break;
case 404:
// Handle not found
break;
case 500:
// Handle server error
break;
default:
// Handle other errors
}
}
return Promise.reject(error);
});
export default {
// Recipes API
getRecipes() {
return api.get('/recipes');
},
getRecipe(id) {
return api.get(`/recipes/${id}`);
},
createRecipe(recipe) {
return api.post('/recipes', recipe);
},
updateRecipe(id, recipe) {
return api.put(`/recipes/${id}`, recipe);
},
deleteRecipe(id) {
return api.delete(`/recipes/${id}`);
},
// Calendar API
getCalendarEvents() {
return api.get('/calendar');
},
createCalendarEvent(event) {
return api.post('/calendar', event);
},
updateCalendarEvent(id, event) {
return api.put(`/calendar/${id}`, event);
},
deleteCalendarEvent(id) {
return api.delete(`/calendar/${id}`);
},
// Grocery API
getGroceryLists() {
return api.get('/grocery');
},
createGroceryList(list) {
return api.post('/grocery', list);
},
updateGroceryList(id, list) {
return api.put(`/grocery/${id}`, list);
},
deleteGroceryList(id) {
return api.delete(`/grocery/${id}`);
}
};
```