chainr-ai's picture
Upload 8536 files
4888678 verified
import axios from 'axios';
const API_URL = process.env.REACT_APP_API_URL || 'http://localhost:5000/api';
export const api = axios.create({
baseURL: API_URL,
timeout: 10000,
headers: {
'Content-Type': 'application/json',
},
});
// Request interceptor
api.interceptors.request.use(
(config) => {
const token = localStorage.getItem('token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);
// Response interceptor
api.interceptors.response.use(
(response) => {
return response;
},
(error) => {
if (error.response?.status === 401) {
localStorage.removeItem('token');
window.location.href = '/login';
}
return Promise.reject(error);
}
);
// API methods
export const authAPI = {
login: (email, password) => api.post('/auth/login', { email, password }),
register: (username, email, password) => api.post('/auth/register', { username, email, password }),
getMe: () => api.get('/auth/me'),
updateProfile: (updates) => api.put('/auth/profile', updates),
};
export const conversationAPI = {
getConversations: (params = {}) => api.get('/conversations', { params }),
getConversation: (id) => api.get(`/conversations/${id}`),
createConversation: (title) => api.post('/conversations', { title }),
updateConversation: (id, updates) => api.put(`/conversations/${id}`, updates),
deleteConversation: (id) => api.delete(`/conversations/${id}`),
};
export const chatAPI = {
sendMessage: (conversationId, content) => api.post('/chat/message', { conversationId, content }),
getMessages: (conversationId, params = {}) => api.get(`/chat/messages/${conversationId}`, { params }),
deleteMessage: (messageId) => api.delete(`/chat/message/${messageId}`),
};