const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:3005/api'; const getHeaders = () => { const token = localStorage.getItem('auth_token'); return { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}) }; }; export const api = { // Auth register: async (email, password) => { const res = await fetch(`${API_URL}/auth/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error); return data; // returns { status: 'pending' | 'active', token, profile } }, login: async (email, password) => { const res = await fetch(`${API_URL}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error); return data; }, getMe: async () => { const res = await fetch(`${API_URL}/auth/me`, { headers: getHeaders() }); const data = await res.json(); if (!res.ok) throw new Error(data.error); return data; }, // Projects getProjects: async () => { const res = await fetch(`${API_URL}/projects`, { headers: getHeaders() }); const data = await res.json(); if (!res.ok) throw new Error(data.error); return data; }, getProject: async (id) => { const res = await fetch(`${API_URL}/projects/${id}`, { headers: getHeaders() }); const data = await res.json(); if (!res.ok) throw new Error(data.error); return data; }, saveProject: async (projectData) => { const res = await fetch(`${API_URL}/projects`, { method: 'POST', headers: getHeaders(), body: JSON.stringify(projectData) }); const data = await res.json(); if (!res.ok) throw new Error(data.error); return data; }, // Admin getUsers: async () => { const res = await fetch(`${API_URL}/users`, { headers: getHeaders() }); const data = await res.json(); if (!res.ok) throw new Error(data.error); return data; }, updateUser: async (id, updateData) => { const res = await fetch(`${API_URL}/users/${id}`, { method: 'PUT', headers: getHeaders(), body: JSON.stringify(updateData) }); const data = await res.json(); if (!res.ok) throw new Error(data.error); return data; }, // Credits deductCredit: async () => { const res = await fetch(`${API_URL}/users/deduct-credit`, { method: 'POST', headers: getHeaders() }); const data = await res.json(); if (!res.ok) throw new Error(data.error); return data; }, // Storage uploadAsset: async (fileBlob) => { const formData = new FormData(); formData.append('file', fileBlob); const token = localStorage.getItem('auth_token'); const headers = token ? { Authorization: `Bearer ${token}` } : {}; const res = await fetch(`${API_URL}/upload`, { method: 'POST', headers, body: formData }); const data = await res.json(); if (!res.ok) throw new Error(data.error); return data.url; } };