| |
| |
| |
| |
| |
| |
|
|
| 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; |
| } |
|
|
| |
| 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"); |
|
|
| |
| 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" }); |
|
|
| |
| 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" }); |
|
|
| |
| 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" }); |
|
|
| |
| export const chat = (message, history = []) => |
| req("/api/advisor/chat", { method: "POST", body: JSON.stringify({ message, history }) }); |
|
|
| |
| 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 }) }); |
|
|