This commit is contained in:
tuanOts
2025-05-25 17:15:22 +07:00
parent 6b6e2bbc8a
commit 9687351177
11 changed files with 1099 additions and 38 deletions
+53
View File
@@ -0,0 +1,53 @@
import axios from 'axios';
// Create axios instance with base configuration
const api = axios.create({
baseURL: process.env.REACT_APP_API_BASE_URL,
timeout: 10000,
headers: {
'Content-Type': 'application/json',
},
});
// Request interceptor
api.interceptors.request.use(
(config) => {
// Add auth token if available
const token = localStorage.getItem('authToken');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
console.log('API Request:', config.method?.toUpperCase(), config.url);
return config;
},
(error) => {
console.error('Request Error:', error);
return Promise.reject(error);
}
);
// Response interceptor
api.interceptors.response.use(
(response) => {
console.log('API Response:', response.status, response.config.url);
return response;
},
(error) => {
console.error('Response Error:', error.response?.status, error.response?.data);
// Handle common error cases
if (error.response?.status === 401) {
// Unauthorized - redirect to login or refresh token
localStorage.removeItem('authToken');
// You can add redirect logic here
} else if (error.response?.status === 500) {
// Server error
console.error('Server Error:', error.response.data);
}
return Promise.reject(error);
}
);
export default api;
+128
View File
@@ -0,0 +1,128 @@
import api from './api';
// Products API endpoints
const ENDPOINTS = {
PRODUCTS: 'Products',
PRODUCT_BY_ID: (id) => `Products/${id}`,
CATEGORIES: 'Products/categories',
BRANDS: 'Products/brands',
SEARCH: 'Products/search',
};
// Products API service
export const productsApi = {
// Get all products
getAllProducts: async (params = {}) => {
try {
const response = await api.get(ENDPOINTS.PRODUCTS, { params });
return response.data;
} catch (error) {
console.error('Error fetching products:', error);
throw error;
}
},
// Get product by ID
getProductById: async (id) => {
try {
const response = await api.get(ENDPOINTS.PRODUCT_BY_ID(id));
return response.data;
} catch (error) {
console.error(`Error fetching product ${id}:`, error);
throw error;
}
},
// Create new product
createProduct: async (productData) => {
try {
const response = await api.post(ENDPOINTS.PRODUCTS, productData);
return response.data;
} catch (error) {
console.error('Error creating product:', error);
throw error;
}
},
// Update product
updateProduct: async (id, productData) => {
try {
const response = await api.put(ENDPOINTS.PRODUCT_BY_ID(id), productData);
return response.data;
} catch (error) {
console.error(`Error updating product ${id}:`, error);
throw error;
}
},
// Delete product
deleteProduct: async (id) => {
try {
const response = await api.delete(ENDPOINTS.PRODUCT_BY_ID(id));
return response.data;
} catch (error) {
console.error(`Error deleting product ${id}:`, error);
throw error;
}
},
// Search products
searchProducts: async (query, params = {}) => {
try {
const response = await api.get(ENDPOINTS.SEARCH, {
params: { q: query, ...params }
});
return response.data;
} catch (error) {
console.error('Error searching products:', error);
throw error;
}
},
// Get product categories
getCategories: async () => {
try {
const response = await api.get(ENDPOINTS.CATEGORIES);
return response.data;
} catch (error) {
console.error('Error fetching categories:', error);
throw error;
}
},
// Get product brands
getBrands: async () => {
try {
const response = await api.get(ENDPOINTS.BRANDS);
return response.data;
} catch (error) {
console.error('Error fetching brands:', error);
throw error;
}
},
// Bulk operations
bulkUpdateProducts: async (products) => {
try {
const response = await api.put(`${ENDPOINTS.PRODUCTS}/bulk`, { products });
return response.data;
} catch (error) {
console.error('Error bulk updating products:', error);
throw error;
}
},
bulkDeleteProducts: async (productIds) => {
try {
const response = await api.delete(`${ENDPOINTS.PRODUCTS}/bulk`, {
data: { ids: productIds }
});
return response.data;
} catch (error) {
console.error('Error bulk deleting products:', error);
throw error;
}
},
};
export default productsApi;