/** * api.js — Connect your React app to Finmih backend * * LOCAL dev: set VITE_API_URL=http://localhost:8000 in frontend/.env * Production: set VITE_API_URL=https://YOUR_HF_USERNAME-finmih-api.hf.space */ const BASE = import.meta.env.VITE_API_URL || "http://localhost:8000"; const getToken = () => localStorage.getItem("token"); async function req(path, options = {}) { const token = getToken(); const res = await fetch(`${BASE}${path}`, { ...options, headers: { "Content-Type": "application/json", ...(token ? { Authorization: `Bearer ${token}` } : {}), ...options.headers, }, }); if (res.status === 401) { localStorage.removeItem("token"); window.location.href = "/login"; } const data = await res.json(); if (!res.ok) throw new Error(data.detail || "API error"); return data; } // ── Auth ────────────────────────────────────────────────────────────────────── export const register = (name, email, password, income, currency = "PKR") => req("/api/auth/register", { method: "POST", body: JSON.stringify({ name, email, password, income, currency }) }); export const login = (email, password) => req("/api/auth/login", { method: "POST", body: JSON.stringify({ email, password }) }); export const getMe = () => req("/api/auth/me"); // ── Expenses ────────────────────────────────────────────────────────────────── export const getExpenses = () => req("/api/expenses/"); export const getExpenseSummary = () => req("/api/expenses/summary"); export const addExpense = (data) => req("/api/expenses/", { method: "POST", body: JSON.stringify(data) }); export const deleteExpense = (id) => req(`/api/expenses/${id}`, { method: "DELETE" }); // ── Debt ────────────────────────────────────────────────────────────────────── export const getDebts = () => req("/api/debt/"); export const getDebtAnalysis = () => req("/api/debt/analysis"); export const addDebt = (data) => req("/api/debt/", { method: "POST", body: JSON.stringify(data) }); export const deleteDebt = (id) => req(`/api/debt/${id}`, { method: "DELETE" }); // ── Goals ───────────────────────────────────────────────────────────────────── export const getGoals = () => req("/api/goals/"); export const addGoal = (data) => req("/api/goals/", { method: "POST", body: JSON.stringify(data) }); export const contributeGoal= (id, amt)=> req(`/api/goals/${id}/contribute?amount=${amt}`, { method: "PUT" }); export const deleteGoal = (id) => req(`/api/goals/${id}`, { method: "DELETE" }); // ── AI Advisor ──────────────────────────────────────────────────────────────── export const chat = (message, history = []) => req("/api/advisor/chat", { method: "POST", body: JSON.stringify({ message, history }) }); // ── Growth Predictor ────────────────────────────────────────────────────────── export const predictGrowth = (initial_amount, monthly_saving, annual_rate, years) => req("/api/predictor/growth", { method: "POST", body: JSON.stringify({ initial_amount, monthly_saving, annual_rate, years }) });