Spaces:
Sleeping
Sleeping
Download src/lib/api-client.ts from GodRad/Rag_SaaS_Tenant_UI_v4: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/GodRad/Rag_SaaS_Tenant_UI_v4/resolve/main/src/lib/api-client.ts
- Command line
-
hf download hf://spaces/GodRad/Rag_SaaS_Tenant_UI_v4/src/lib/api-client.ts
-
curl -L -o api-client.ts https://huggingface.co/spaces/GodRad/Rag_SaaS_Tenant_UI_v4/resolve/main/src/lib/api-client.ts
1.52 kB
| /// <reference types="vite/client" /> | |
| import axios from "axios" | |
| const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "http://localhost:7860" | |
| console.log("Using API Base URL:", API_BASE_URL); | |
| const apiClient = axios.create({ | |
| baseURL: API_BASE_URL, | |
| headers: { | |
| "Content-Type": "application/json", | |
| }, | |
| }) | |
| // Add a request interceptor to include tenant_id and auth token | |
| apiClient.interceptors.request.use((config) => { | |
| const tenant_id = localStorage.getItem("tenant_id") | |
| const access_token = localStorage.getItem("access_token") | |
| if (tenant_id) { | |
| config.headers["X-Tenant-ID"] = tenant_id | |
| } | |
| if (access_token) { | |
| config.headers.Authorization = `Bearer ${access_token}` | |
| } | |
| return config | |
| }) | |
| // Add a response interceptor for error handling | |
| apiClient.interceptors.response.use( | |
| (response) => response, | |
| (error) => { | |
| // Handle transient errors or backend cold starts | |
| if (error.code === 'ECONNABORTED' || error.message.includes('Network Error')) { | |
| console.error("Backend might be cold-starting or unreachable.") | |
| } | |
| // Redirect to login on 401 Unauthorized (except for login/register endpoints) | |
| if (error.response?.status === 401 && !error.config.url?.includes('/auth/')) { | |
| localStorage.removeItem("access_token") | |
| window.location.href = "/login" | |
| } | |
| return Promise.reject(error) | |
| } | |
| ) | |
| export default apiClient | |