File size: 1,549 Bytes
de886f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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>,
}