const API_BASE = '/api';
class ApiClient {
constructor() {
this.baseUrl = API_BASE;
}
async get(endpoint, params = {}) {
const url = new URL(`${this.baseUrl}${endpoint}`, window.location.origin);
Object.keys(params).forEach(key => {
if (params[key] !== null && params[key] !== undefined) {
url.searchParams.append(key, params[key]);
}
});
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error('API GET failed:', endpoint, error);
throw error;
}
}
async getLogs(filters = {}) {
return this.get('/logs', filters);
}
async getLog(id) {
return this.get(`/logs/${id}`);
}
async exportLogs(params = {}) {
const url = new URL(`${this.baseUrl}/logs/export`, window.location.origin);
Object.keys(params).forEach(key => {
if (params[key] !== null && params[key] !== undefined) {
url.searchParams.append(key, params[key]);
}
});
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return response.blob();
}
async getTraces(filters = {}) {
return this.get('/traces', filters);
}
async getTrace(traceId) {
return this.get(`/traces/${traceId}`);
}
async exportTraces(params = {}) {
const url = new URL(`${this.baseUrl}/traces/export`, window.location.origin);
Object.keys(params).forEach(key => {
if (params[key] !== null && params[key] !== undefined) {
url.searchParams.append(key, params[key]);
}
});
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return response.blob();
}
async getMetrics(filters = {}) {
return this.get('/metrics', filters);
}
async getMetricNames() {
return this.get('/metrics/names');
}
async getAggregatedMetrics(params = {}) {
return this.get('/metrics/aggregate', params);
}
async getMetricTimeseries(name, params = {}) {
return this.get(`/metrics/${encodeURIComponent(name)}/timeseries`, params);
}
async exportMetrics(params = {}) {
const url = new URL(`${this.baseUrl}/metrics/export`, window.location.origin);
Object.keys(params).forEach(key => {
if (params[key] !== null && params[key] !== undefined) {
url.searchParams.append(key, params[key]);
}
});
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return response.blob();
}
async getResourceKeys(signal) {
return this.get('/resource-keys', { signal });
}
async getTokenUsage(params = {}) {
return this.get('/genai/usage', params);
}
async getHealth() {
return this.get('/health');
}
async getStats() {
return this.get('/stats');
}
}
export const api = new ApiClient();