File size: 2,496 Bytes
11f4e50 | 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 73 74 75 76 77 78 79 80 | import axios from 'axios';
const API_BASE = '/api';
const api = axios.create({
baseURL: API_BASE,
headers: { 'Content-Type': 'application/json' },
});
// Attach JWT token to every request
api.interceptors.request.use((config) => {
const token = localStorage.getItem('director_token');
if (token && config.headers) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
// Handle 401 responses globally
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
localStorage.removeItem('director_token');
localStorage.removeItem('director_user');
window.location.href = '/login';
}
return Promise.reject(error);
}
);
// Auth
export const authAPI = {
register: (email: string, password: string) =>
api.post('/auth/register', { email, password }),
login: (email: string, password: string) =>
api.post('/auth/login', { email, password }),
};
// Projects
export const projectsAPI = {
list: () => api.get('/projects'),
get: (id: string) => api.get(`/projects/${id}`),
create: (data: { name: string; defaultPlatform: string; defaultFormat: string }) =>
api.post('/projects', data),
delete: (id: string) => api.delete(`/projects/${id}`),
};
// Videos
export const videosAPI = {
create: (projectId: string, data: any) =>
api.post(`/videos/${projectId}`, data),
get: (id: string) => api.get(`/videos/${id}`),
generate: (id: string) => api.post(`/videos/${id}/generate`),
status: (id: string) => api.get(`/videos/${id}/status`),
export: (id: string, data: { formats: string[]; quality: string }) =>
api.post(`/videos/${id}/export`, data),
};
// Presets
export const presetsAPI = {
list: () => api.get('/presets'),
create: (data: any) => api.post('/presets', data),
update: (id: string, data: any) => api.put(`/presets/${id}`, data),
delete: (id: string) => api.delete(`/presets/${id}`),
};
// Assets
export const assetsAPI = {
upload: (files: FileList | File[]) => {
const formData = new FormData();
Array.from(files).forEach((file) => formData.append('files', file));
return api.post('/assets/upload', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
});
},
};
export default api;
|