| import axios from 'axios';
|
|
|
| const API_BASE_URL = 'http://localhost:5000/api';
|
|
|
|
|
| const api = axios.create({
|
| baseURL: API_BASE_URL,
|
| headers: {
|
| 'Content-Type': 'application/json',
|
| }
|
| });
|
|
|
|
|
| api.interceptors.request.use((config) => {
|
| console.log(`Making ${config.method?.toUpperCase()} request to: ${config.url}`);
|
| return config;
|
| });
|
|
|
|
|
| api.interceptors.response.use(
|
| (response) => {
|
| console.log('Response received:', response.status);
|
| return response.data;
|
| },
|
| (error) => {
|
| console.error('API Error:', error.response?.data || error.message);
|
| return Promise.reject(error.response?.data || { error: 'Network error' });
|
| }
|
| );
|
|
|
|
|
| export const authService = {
|
| register: (userData) => api.post('/auth/register', userData),
|
| login: (credentials) => api.post('/auth/login', credentials),
|
| logout: () => api.post('/auth/logout'),
|
| getMe: () => api.get('/auth/me')
|
| };
|
|
|
|
|
| export const testCors = () => api.get('/test-cors');
|
|
|
|
|
| export const uploadService = {
|
| uploadFile: (file, onProgress) => {
|
| return new Promise((resolve) => {
|
|
|
| let progress = 0;
|
| const interval = setInterval(() => {
|
| progress += 10;
|
| if (onProgress) onProgress(progress);
|
|
|
| if (progress >= 100) {
|
| clearInterval(interval);
|
|
|
| resolve({
|
| file: {
|
| url: URL.createObjectURL(file),
|
| filename: `mock-${Date.now()}-${file.name}`,
|
| originalName: file.name,
|
| mimeType: file.type,
|
| size: file.size,
|
| thumbnail: file.type.startsWith('image/') ? URL.createObjectURL(file) : null
|
| }
|
| });
|
| }
|
| }, 100);
|
| });
|
| },
|
| getFileUrl: (filename) => `${API_BASE_URL.replace('/api', '')}/uploads/${filename}`
|
| };
|
|
|
| export default api; |