File size: 2,164 Bytes
057576a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import axios from 'axios';

const API_BASE_URL = 'http://localhost:5000/api';

// Create axios instance without credentials
const api = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    'Content-Type': 'application/json',
  }
});

// Add request interceptor to log requests
api.interceptors.request.use((config) => {
  console.log(`Making ${config.method?.toUpperCase()} request to: ${config.url}`);
  return config;
});

// Add response interceptor to handle errors
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' });
  }
);

// Auth services
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')
};

// Test CORS
export const testCors = () => api.get('/test-cors');

// Upload services (mock for now - will be implemented when backend is ready)
export const uploadService = {
  uploadFile: (file, onProgress) => {
    return new Promise((resolve) => {
      // Simulate file upload progress
      let progress = 0;
      const interval = setInterval(() => {
        progress += 10;
        if (onProgress) onProgress(progress);
        
        if (progress >= 100) {
          clearInterval(interval);
          // Return mock file data
          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;