145 lines
3.2 KiB
TypeScript
145 lines
3.2 KiB
TypeScript
import axios from 'axios';
|
|
|
|
const API_BASE_URL = process.env.REACT_APP_API_URL;
|
|
|
|
const api = axios.create({
|
|
baseURL: API_BASE_URL,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
// Add auth token to requests
|
|
api.interceptors.request.use((config) => {
|
|
const token = localStorage.getItem('token');
|
|
if (token && config.headers) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
return config;
|
|
});
|
|
|
|
// Handle auth errors
|
|
api.interceptors.response.use(
|
|
(response) => response,
|
|
(error) => {
|
|
if (error.response?.status === 401) {
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
window.location.href = '/login';
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export interface Recipe {
|
|
_id: string;
|
|
title: string;
|
|
description: string;
|
|
ingredients: Array<{
|
|
name: string;
|
|
amount: number;
|
|
unit: string;
|
|
}>;
|
|
instructions: Array<{
|
|
step: number;
|
|
description: string;
|
|
}>;
|
|
servings: number;
|
|
prepTime: number;
|
|
cookTime: number;
|
|
category: 'breakfast' | 'lunch' | 'dinner' | 'dessert' | 'snack' | 'appetizer';
|
|
difficulty: 'easy' | 'medium' | 'hard';
|
|
imageUrl: string;
|
|
createdAt: string;
|
|
createdBy?: string;
|
|
}
|
|
|
|
export interface User {
|
|
id: string;
|
|
username: string;
|
|
email: string;
|
|
}
|
|
|
|
export interface AuthResponse {
|
|
token: string;
|
|
user: User;
|
|
}
|
|
|
|
export interface ApiResponse<T> {
|
|
data: T;
|
|
message?: string;
|
|
}
|
|
|
|
export interface UserSelection {
|
|
_id: string;
|
|
userId: string;
|
|
selectedRecipes: Array<{
|
|
recipeId: Recipe;
|
|
quantity: number;
|
|
addedAt: string;
|
|
}>;
|
|
aggregatedIngredients: Array<{
|
|
name: string;
|
|
totalAmount: number;
|
|
unit: string;
|
|
recipes: Array<{
|
|
recipeId: string;
|
|
recipeTitle: string;
|
|
amount: number;
|
|
quantity: number;
|
|
}>;
|
|
}>;
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
}
|
|
|
|
// Auth API
|
|
export const authAPI = {
|
|
register: (userData: { username: string; email: string; password: string }) =>
|
|
api.post<AuthResponse>('/users/register', userData),
|
|
|
|
login: (credentials: { email: string; password: string }) =>
|
|
api.post<AuthResponse>('/users/login', credentials),
|
|
|
|
getProfile: () =>
|
|
api.get<User>('/users/profile'),
|
|
};
|
|
|
|
// Recipes API
|
|
export const recipesAPI = {
|
|
getAll: (params?: { category?: string; difficulty?: string; search?: string }) =>
|
|
api.get('/recipes', { params }),
|
|
|
|
getById: (id: string) =>
|
|
api.get(`/recipes/${id}`),
|
|
|
|
create: (recipe: Omit<Recipe, '_id' | 'createdAt'>) =>
|
|
api.post('/recipes', recipe),
|
|
|
|
update: (id: string, recipe: Partial<Recipe>) =>
|
|
api.put(`/recipes/${id}`, recipe),
|
|
|
|
delete: (id: string) =>
|
|
api.delete(`/recipes/${id}`),
|
|
};
|
|
|
|
// Selections API
|
|
export const selectionsAPI = {
|
|
get: () =>
|
|
api.get<UserSelection>('/selections'),
|
|
|
|
addRecipe: (recipeId: string, quantity: number = 1) =>
|
|
api.post<UserSelection>('/selections/add', { recipeId, quantity }),
|
|
|
|
updateQuantity: (recipeId: string, quantity: number) =>
|
|
api.put<UserSelection>('/selections/update', { recipeId, quantity }),
|
|
|
|
removeRecipe: (recipeId: string) =>
|
|
api.delete<UserSelection>(`/selections/remove/${recipeId}`),
|
|
|
|
clear: () =>
|
|
api.delete<UserSelection>('/selections/clear'),
|
|
};
|
|
|
|
export default api;
|