kanban-app/frontend/src/hooks/useApi.ts

92 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-02-25 09:29:45 +00:00
import axios from 'axios';
import { RegisterData, UserData, ProductData, OrderData, AuthResponse } from '../types';
2026-02-24 08:52:57 +00:00
const api = axios.create({
baseURL: '/api',
headers: {
'Content-Type': 'application/json',
},
2026-02-25 09:29:45 +00:00
});
2026-02-24 08:52:57 +00:00
// Add token to requests if available
api.interceptors.request.use(
(config) => {
2026-02-25 09:29:45 +00:00
const token = localStorage.getItem('token');
2026-02-24 08:52:57 +00:00
if (token) {
2026-02-25 09:29:45 +00:00
config.headers.Authorization = `Bearer ${token}`;
2026-02-24 08:52:57 +00:00
}
2026-02-25 09:29:45 +00:00
return config;
2026-02-24 08:52:57 +00:00
},
(error) => Promise.reject(error)
2026-02-25 09:29:45 +00:00
);
2026-02-24 08:52:57 +00:00
// Handle response errors
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
// Token expired or invalid
2026-02-25 09:29:45 +00:00
localStorage.removeItem('token');
localStorage.removeItem('user');
window.location.href = '/login';
2026-02-24 08:52:57 +00:00
}
2026-02-25 09:29:45 +00:00
return Promise.reject(error);
2026-02-24 08:52:57 +00:00
}
2026-02-25 09:29:45 +00:00
);
2026-02-24 08:52:57 +00:00
export function useApi() {
return {
// Auth
login: async (email: string, password: string): Promise<AuthResponse> => {
2026-02-25 09:29:45 +00:00
const response = await api.post<AuthResponse>('/auth/login', {
email,
password,
});
return response.data;
2026-02-24 08:52:57 +00:00
},
register: async (userData: RegisterData): Promise<AuthResponse> => {
2026-02-25 09:29:45 +00:00
const response = await api.post<AuthResponse>('/auth/register', userData);
return response.data;
2026-02-24 08:52:57 +00:00
},
getCurrentUser: async (): Promise<UserData> => {
2026-02-25 09:29:45 +00:00
const response = await api.get<UserData>('/users/me');
return response.data;
2026-02-24 08:52:57 +00:00
},
// Products
getProducts: async (): Promise<ProductData[]> => {
2026-02-25 09:29:45 +00:00
const response = await api.get<ProductData[]>('/products');
return response.data;
2026-02-24 08:52:57 +00:00
},
getProduct: async (id: string): Promise<ProductData> => {
2026-02-25 09:29:45 +00:00
const response = await api.get<ProductData>(`/products/${id}`);
return response.data;
2026-02-24 08:52:57 +00:00
},
createProduct: async (productData: Omit<ProductData, 'id'>): Promise<ProductData> => {
2026-02-25 09:29:45 +00:00
const response = await api.post<ProductData>('/products', productData);
return response.data;
2026-02-24 08:52:57 +00:00
},
updateProduct: async (id: string, productData: Partial<ProductData>): Promise<ProductData> => {
2026-02-25 09:29:45 +00:00
const response = await api.put<ProductData>(`/products/${id}`, productData);
return response.data;
2026-02-24 08:52:57 +00:00
},
deleteProduct: async (id: string): Promise<void> => {
2026-02-25 09:29:45 +00:00
await api.delete(`/products/${id}`);
2026-02-24 08:52:57 +00:00
},
// Orders
getOrders: async (): Promise<OrderData[]> => {
2026-02-25 09:29:45 +00:00
const response = await api.get<OrderData[]>('/orders');
return response.data;
2026-02-24 08:52:57 +00:00
},
getOrder: async (id: string): Promise<OrderData> => {
2026-02-25 09:29:45 +00:00
const response = await api.get<OrderData>(`/orders/${id}`);
return response.data;
2026-02-24 08:52:57 +00:00
},
createOrder: async (orderData: Omit<OrderData, 'id'>): Promise<OrderData> => {
2026-02-25 09:29:45 +00:00
const response = await api.post<OrderData>('/orders', orderData);
return response.data;
2026-02-24 08:52:57 +00:00
},
2026-02-25 09:29:45 +00:00
};
}