kofdai's picture
Deploy NullAI Knowledge System to Spaces
075a2b6 verified
// src/services/api.ts
import axios from 'axios';
// Get API URL from environment variable or use relative path for production
const getApiBaseUrl = () => {
// Check for Vite environment variable first
if (import.meta.env.VITE_API_URL) {
return `${import.meta.env.VITE_API_URL}/api`;
}
// In production, use relative path to work with any domain
if (import.meta.env.PROD) {
return '/api';
}
// Development fallback
return 'http://localhost:8000/api';
};
const apiClient = axios.create({
baseURL: getApiBaseUrl(),
headers: {
'Content-Type': 'application/json',
},
});
export interface Domain {
domain_id: string;
name: string;
description?: string;
}
export const fetchDomains = async (): Promise<Domain[]> => {
const response = await apiClient.get<Domain[]>('/domains');
return response.data;
};
export const fetchDomain = async (domainId: string): Promise<Domain> => {
const response = await apiClient.get<Domain>(`/domains/${domainId}`);
return response.data;
};
export const updateDomain = async (domainId: string, data: Partial<Domain>): Promise<Domain> => {
const response = await apiClient.put<Domain>(`/domains/${domainId}`, data);
return response.data;
};
// You can add interceptors here if you need to handle tokens, etc.
export default apiClient;