File size: 2,701 Bytes
7657e9f
 
 
 
 
 
 
83c80d0
7657e9f
 
 
 
 
 
 
 
 
 
 
 
83c80d0
7657e9f
 
 
83c80d0
 
 
 
 
 
 
 
 
 
 
7657e9f
 
 
 
 
 
 
 
83c80d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7657e9f
83c80d0
 
 
 
 
 
 
 
 
 
 
 
7657e9f
 
 
 
 
 
 
 
83c80d0
7657e9f
 
 
 
83c80d0
 
 
 
7657e9f
 
 
 
83c80d0
7657e9f
83c80d0
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
export interface Integration {
  id: string
  name: string
  description: string
  logo_url: string
  category: string
  auth_type: string
  oauth_scopes?: string[] | null
  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 | null
  is_active: boolean
}

export interface Connection {
  id: string
  integration_id: string
  integration_name: string
  account_label: string | null
  status: string
  connected_at: string
  last_used: string | null
  metadata: Record<string, unknown> | null
}

export interface Stats {
  total_executions: number
  successful: number
  failed: number
  success_rate: number
  avg_latency_ms: number
}

export interface AuthField {
  name: string
  label: string
  type: string
  required: boolean
  description: string
  default: string
}

export interface AuthConfig {
  integration_id: string
  auth_type: string
  fields: AuthField[]
  oauth_authorize_url: string | null
  oauth_token_url: string | null
}

export interface ToolExecution {
  execution_id: string
  status: string
  result: Record<string, unknown> | null
  latency_ms: number
  executed_at: string
  error: string | null
}

export interface APIKey {
  id: string
  name: string
  key_preview: string
  created_at: string
  last_used: string | null
  is_active: boolean
}

const API_BASE = ''

async function request<T>(path: string, opts: RequestInit = {}): Promise<T> {
  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')
      localStorage.removeItem('email')
      window.location.href = '/login'
    }
    throw new Error('Unauthorized')
  }
  if (!res.ok) {
    const data = await res.json().catch(() => ({ detail: 'Request failed' }))
    throw new Error(data.detail || `HTTP ${res.status}`)
  }
  return res.json()
}

export const api = {
  get: <T>(path: string) => request<T>(path),
  post: <T>(path: string, body?: unknown) =>
    request<T>(path, { method: 'POST', body: body ? JSON.stringify(body) : undefined }),
  put: <T>(path: string, body?: unknown) =>
    request<T>(path, { method: 'PUT', body: body ? JSON.stringify(body) : undefined }),
  delete: <T>(path: string) => request<T>(path, { method: 'DELETE' }),
}