Files
CaddyProxyManager/frontend/src/lib/api.ts
Pacerino 5e6c6b083c [Frontend] Rewrite SPA with Vite, React, TypeScript, Tailwind and shadcn/ui
Replace the Create React App frontend with a modern Vite stack. Adds a
login page (local form or SSO depending on auth mode), an OIDC callback
page, and a hosts page with create/edit/delete. Build output is emitted
into backend/embed/assets for embedding in the binary.
2026-06-14 02:35:41 +02:00

39 lines
905 B
TypeScript

import axios from "axios";
// Same-origin in production (served by the Go binary); the Vite dev server
// proxies /api to the backend.
export const api = axios.create({
baseURL: "/api",
});
api.interceptors.request.use((config) => {
const token = localStorage.getItem("token");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
localStorage.removeItem("token");
if (window.location.pathname !== "/login") {
window.location.href = "/login";
}
}
return Promise.reject(error);
}
);
// The backend wraps payloads as { result, error }.
export interface ApiEnvelope<T> {
result: T;
error?: { code: number; message: string };
}
export function unwrap<T>(data: ApiEnvelope<T>): T {
return data.result;
}