Compost / frontend /_src /lib /api.ts
abc1181's picture
Fix health endpoint routing order
de886f2
Raw
History Blame Contribute Delete
1.55 kB
export interface Integration {
id: string
name: string
description: string
logo_url: string
category: string
auth_type: string
tool_count: number
trigger_count: number
is_active: boolean
}
export interface Tool {
id: string
integration_id: string
name: string
description: string
input_schema: Record<string, unknown>
output_schema?: Record<string, unknown>
category: string
is_active: boolean
}
export interface Stats {
total_executions: number
successful: number
failed: number
success_rate: number
avg_latency_ms: number
}
const API_BASE = typeof window !== 'undefined' ? '' : process.env.NEXT_PUBLIC_API_URL || ''
async function fetchApi(path: string, opts: RequestInit = {}) {
const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null
const headers: Record<string, string> = { 'Content-Type': 'application/json' }
if (token) headers['Authorization'] = `Bearer ${token}`
const res = await fetch(`${API_BASE}${path}`, { ...opts, headers })
if (res.status === 401) {
if (typeof window !== 'undefined') {
localStorage.removeItem('token')
window.location.href = '/login'
}
throw new Error('Unauthorized')
}
return res.json()
}
export const api = {
get: <T>(path: string) => fetchApi(path) as Promise<T>,
post: <T>(path: string, body?: unknown) =>
fetchApi(path, { method: 'POST', body: body ? JSON.stringify(body) : undefined }) as Promise<T>,
delete: <T>(path: string) => fetchApi(path, { method: 'DELETE' }) as Promise<T>,
}