125 lines
3.7 KiB
JavaScript
125 lines
3.7 KiB
JavaScript
import axios from 'axios';
|
|
|
|
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000';
|
|
|
|
const api = axios.create({
|
|
baseURL: `${API_URL}/api`,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
|
|
// Add token to requests
|
|
api.interceptors.request.use((config) => {
|
|
const token = localStorage.getItem('token');
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
return config;
|
|
});
|
|
|
|
// Handle 401 errors
|
|
api.interceptors.response.use(
|
|
(response) => response,
|
|
(error) => {
|
|
if (error.response?.status === 401) {
|
|
localStorage.removeItem('token');
|
|
window.location.href = '/login';
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default api;
|
|
|
|
// Auth API
|
|
export const authAPI = {
|
|
register: (email, password) => api.post('/auth/register', { email, password }),
|
|
login: (email, password) => api.post('/auth/login', { email, password }),
|
|
getMe: () => api.get('/auth/me')
|
|
};
|
|
|
|
// Contacts API
|
|
export const contactsAPI = {
|
|
list: (params) => api.get('/contacts', { params }),
|
|
get: (id) => api.get(`/contacts/${id}`),
|
|
create: (data) => api.post('/contacts', data),
|
|
update: (id, data) => api.put(`/contacts/${id}`, data),
|
|
delete: (id) => api.delete(`/contacts/${id}`),
|
|
|
|
// Tags
|
|
listTags: () => api.get('/contacts/tags'),
|
|
createTag: (name) => api.post('/contacts/tags', { name }),
|
|
addTag: (contactId, tagId) => api.post(`/contacts/${contactId}/tags/${tagId}`),
|
|
removeTag: (contactId, tagId) => api.delete(`/contacts/${contactId}/tags/${tagId}`),
|
|
|
|
// DND
|
|
listDND: () => api.get('/contacts/dnd'),
|
|
addDND: (data) => api.post('/contacts/dnd', data),
|
|
removeDND: (id) => api.delete(`/contacts/dnd/${id}`)
|
|
};
|
|
|
|
// Lists API
|
|
export const listsAPI = {
|
|
list: () => api.get('/lists'),
|
|
get: (id) => api.get(`/lists/${id}`),
|
|
create: (data) => api.post('/lists', data),
|
|
update: (id, data) => api.put(`/lists/${id}`, data),
|
|
delete: (id) => api.delete(`/lists/${id}`),
|
|
getContacts: (id) => api.get(`/lists/${id}/contacts`),
|
|
addMembers: (id, contactIds) => api.post(`/lists/${id}/members`, { contact_ids: contactIds }),
|
|
removeMembers: (id, contactIds) => api.delete(`/lists/${id}/members`, { data: { contact_ids: contactIds } })
|
|
};
|
|
|
|
// Templates API
|
|
export const templatesAPI = {
|
|
list: () => api.get('/templates'),
|
|
get: (id) => api.get(`/templates/${id}`),
|
|
create: (data) => api.post('/templates', data),
|
|
update: (id, data) => api.put(`/templates/${id}`, data),
|
|
delete: (id) => api.delete(`/templates/${id}`)
|
|
};
|
|
|
|
// Campaigns API
|
|
export const campaignsAPI = {
|
|
list: (params) => api.get('/campaigns', { params }),
|
|
get: (id) => api.get(`/campaigns/${id}`),
|
|
create: (data) => api.post('/campaigns', data),
|
|
update: (id, data) => api.put(`/campaigns/${id}`, data),
|
|
delete: (id) => api.delete(`/campaigns/${id}`),
|
|
preview: (id) => api.get(`/campaigns/${id}/preview`),
|
|
send: (id) => api.post(`/campaigns/${id}/send`),
|
|
reset: (id) => api.post(`/campaigns/${id}/reset`),
|
|
getRecipients: (id, params) => api.get(`/campaigns/${id}/recipients`, { params })
|
|
};
|
|
|
|
// Imports API
|
|
export const importsAPI = {
|
|
uploadExcel: (file) => {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
return api.post('/imports/excel', formData, {
|
|
headers: { 'Content-Type': 'multipart/form-data' }
|
|
});
|
|
},
|
|
uploadCSV: (file) => {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
return api.post('/imports/csv', formData, {
|
|
headers: { 'Content-Type': 'multipart/form-data' }
|
|
});
|
|
},
|
|
googleAuthStart: () => api.get('/imports/google/start'),
|
|
googleSync: () => api.post('/imports/google/sync')
|
|
};
|
|
|
|
// Stats API
|
|
export const statsAPI = {
|
|
get: () => api.get('/stats')
|
|
};
|
|
|
|
// Workers API
|
|
export const workersAPI = {
|
|
tick: () => api.post('/workers/tick')
|
|
};
|