Files
khata-ui/react-openapi/src/hooks/useApi.ts
Vishesh 'ironeagle' Bangotra 5d34c51e98 wire profile components, login/register routes, spec-driven auth
- src/main.jsx: add /login, /register, /profile/* routes; wire
  profileComponents from react-auth; pass basePath to Admin
- src/Header.tsx: profile button nav to /profile/me; Login to /login
- src/openapi-config.ts: wire getToken and profileComponents
- react-auth local: sync ProfileCreate, ProfileEdit, ProfileView
- react-openapi local: sync all auth + x-profile changes
  (AppProvider, Admin, ProfileRoutes, SideMenu, types, hooks, etc.)
2026-07-17 21:00:47 +05:30

32 lines
766 B
TypeScript

import axios, { AxiosInstance } from "axios";
let apiClient: AxiosInstance | null = null;
export function initApi(baseUrl: string, getToken?: () => string | null): AxiosInstance {
if (apiClient && apiClient.defaults.baseURL === baseUrl) {
return apiClient;
}
apiClient = axios.create({
baseURL: baseUrl,
headers: { "Content-Type": "application/json" },
});
apiClient.interceptors.request.use((config) => {
const token = getToken?.();
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
return apiClient;
}
export function getApi(): AxiosInstance {
if (!apiClient) {
throw new Error("API client not initialized. Make sure AppProvider is mounted.");
}
return apiClient;
}