102 lines
5.3 KiB
JavaScript
102 lines
5.3 KiB
JavaScript
import axios from "axios";
|
|
|
|
const api = axios.create({
|
|
baseURL: "/api/v1",
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
|
|
api.interceptors.request.use((config) => {
|
|
const token = localStorage.getItem("access_token");
|
|
if (token) config.headers.Authorization = `Bearer ${token}`;
|
|
return config;
|
|
});
|
|
|
|
api.interceptors.response.use(
|
|
(r) => r,
|
|
async (error) => {
|
|
const original = error.config;
|
|
if (error.response?.status === 401 && !original._retry) {
|
|
original._retry = true;
|
|
const refresh = localStorage.getItem("refresh_token");
|
|
if (refresh) {
|
|
try {
|
|
const { data } = await axios.post("/api/v1/auth/refresh", null, {
|
|
params: { refresh_token: refresh },
|
|
});
|
|
localStorage.setItem("access_token", data.access_token);
|
|
localStorage.setItem("refresh_token", data.refresh_token);
|
|
original.headers.Authorization = `Bearer ${data.access_token}`;
|
|
return api(original);
|
|
} catch {
|
|
localStorage.clear();
|
|
window.location.href = "/login";
|
|
}
|
|
}
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
// ── Auth ────────────────────────────────────────────────
|
|
export const login = (username, password) =>
|
|
api.post("/auth/login/json", { username, password });
|
|
export const register = (data) => api.post("/auth/register", data);
|
|
export const getMe = () => api.get("/users/me");
|
|
|
|
// ── Users (admin) ────────────────────────────────────────
|
|
export const getUsers = () => api.get("/users");
|
|
export const updateUser = (id, data) => api.patch(`/users/${id}`, data);
|
|
export const deleteUser = (id) => api.delete(`/users/${id}`);
|
|
|
|
// ── Currencies ───────────────────────────────────────────
|
|
export const getCurrencies = () => api.get("/currencies");
|
|
export const createCurrency = (d) => api.post("/currencies", d);
|
|
export const updateCurrency = (id, d) => api.patch(`/currencies/${id}`, d);
|
|
export const deleteCurrency = (id) => api.delete(`/currencies/${id}`);
|
|
|
|
// ── Exchange rates ────────────────────────────────────────
|
|
export const getExchangeRates = (params) => api.get("/exchange-rates", { params });
|
|
export const createExchangeRate = (d) => api.post("/exchange-rates", d);
|
|
export const deleteExchangeRate = (id) => api.delete(`/exchange-rates/${id}`);
|
|
|
|
// ── Accounts ──────────────────────────────────────────────
|
|
export const getAccountTypes = () => api.get("/accounts/types");
|
|
export const getAccounts = () => api.get("/accounts");
|
|
export const createAccount = (d) => api.post("/accounts", d);
|
|
export const updateAccount = (id, d) => api.patch(`/accounts/${id}`, d);
|
|
export const deleteAccount = (id) => api.delete(`/accounts/${id}`);
|
|
|
|
// ── Categories ────────────────────────────────────────────
|
|
export const getCategories = () => api.get("/categories");
|
|
export const createCategory = (d) => api.post("/categories", d);
|
|
export const updateCategory = (id, d) => api.patch(`/categories/${id}`, d);
|
|
export const deleteCategory = (id) => api.delete(`/categories/${id}`);
|
|
|
|
// ── Budgets ───────────────────────────────────────────────
|
|
export const getBudgets = () => api.get("/budgets");
|
|
export const createBudget = (d) => api.post("/budgets", d);
|
|
export const updateBudget = (id, d) => api.patch(`/budgets/${id}`, d);
|
|
export const deleteBudget = (id) => api.delete(`/budgets/${id}`);
|
|
|
|
// ── Locations ─────────────────────────────────────────────
|
|
export const getLocations = () => api.get("/locations");
|
|
export const createLocation = (d) => api.post("/locations", d);
|
|
export const updateLocation = (id, d) => api.patch(`/locations/${id}`, d);
|
|
export const deleteLocation = (id) => api.delete(`/locations/${id}`);
|
|
|
|
// ── Operations ────────────────────────────────────────────
|
|
export const getOperationTypes = () => api.get("/operations/types");
|
|
export const getOperations = (params) => api.get("/operations", { params });
|
|
export const createOperation = (d) => api.post("/operations", d);
|
|
export const createTransfer = (d) => api.post("/operations/transfer", d);
|
|
export const updateOperation = (id, d) => api.patch(`/operations/${id}`, d);
|
|
export const deleteOperation = (id) => api.delete(`/operations/${id}`);
|
|
|
|
// ── Calendar ──────────────────────────────────────────────
|
|
export const getCalendarEvents = (params) => api.get("/calendar", { params });
|
|
export const createCalendarEvent = (d) => api.post("/calendar", d);
|
|
export const updateCalendarEvent = (id, d) => api.patch(`/calendar/${id}`, d);
|
|
export const deleteCalendarEvent = (id) => api.delete(`/calendar/${id}`);
|
|
|
|
export default api;
|