94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
/**
|
|
* API utilities for fetching celestial data
|
|
*/
|
|
import axios from 'axios';
|
|
import type { CelestialDataResponse, BodyInfo } from '../types';
|
|
|
|
// Dynamically determine the API base URL
|
|
// If VITE_API_BASE_URL is set, use it; otherwise use the current host with port 8000
|
|
const getApiBaseUrl = () => {
|
|
if (import.meta.env.VITE_API_BASE_URL) {
|
|
console.log('[API] Using VITE_API_BASE_URL:', import.meta.env.VITE_API_BASE_URL);
|
|
return import.meta.env.VITE_API_BASE_URL;
|
|
}
|
|
|
|
// Use the same host as the frontend, but with port 8000
|
|
const protocol = window.location.protocol;
|
|
const hostname = window.location.hostname;
|
|
const apiUrl = `${protocol}//${hostname}:8000/api`;
|
|
console.log('[API] Constructed API URL:', apiUrl);
|
|
console.log('[API] Protocol:', protocol, 'Hostname:', hostname);
|
|
return apiUrl;
|
|
};
|
|
|
|
const API_BASE_URL = getApiBaseUrl();
|
|
console.log('[API] Final API_BASE_URL:', API_BASE_URL);
|
|
|
|
export const api = axios.create({
|
|
baseURL: API_BASE_URL,
|
|
timeout: 30000,
|
|
});
|
|
|
|
// Add request interceptor for debugging
|
|
api.interceptors.request.use(
|
|
(config) => {
|
|
console.log('[API Request]', config.method?.toUpperCase(), config.url, config.params);
|
|
return config;
|
|
},
|
|
(error) => {
|
|
console.error('[API Request Error]', error);
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
// Add response interceptor for debugging
|
|
api.interceptors.response.use(
|
|
(response) => {
|
|
console.log('[API Response]', response.config.url, response.status, 'Data:', response.data);
|
|
return response;
|
|
},
|
|
(error) => {
|
|
console.error('[API Error]', error.config?.url, error.message);
|
|
if (error.response) {
|
|
console.error('[API Error Response]', error.response.status, error.response.data);
|
|
} else if (error.request) {
|
|
console.error('[API Error Request]', error.request);
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
/**
|
|
* Fetch celestial positions
|
|
*/
|
|
export async function fetchCelestialPositions(
|
|
startTime?: string,
|
|
endTime?: string,
|
|
step: string = '1d'
|
|
): Promise<CelestialDataResponse> {
|
|
const params: Record<string, string> = { step };
|
|
if (startTime) params.start_time = startTime;
|
|
if (endTime) params.end_time = endTime;
|
|
|
|
const response = await api.get<CelestialDataResponse>('/celestial/positions', {
|
|
params,
|
|
});
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* Fetch body information
|
|
*/
|
|
export async function fetchBodyInfo(bodyId: string): Promise<BodyInfo> {
|
|
const response = await api.get<BodyInfo>(`/celestial/info/${bodyId}`);
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* List all bodies
|
|
*/
|
|
export async function fetchAllBodies(): Promise<{ bodies: BodyInfo[] }> {
|
|
const response = await api.get('/celestial/list');
|
|
return response.data;
|
|
}
|