File size: 1,628 Bytes
201b13c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const isLocalhost = typeof window !== "undefined" && ["127.0.0.1", "localhost"].includes(window.location.hostname)
const defaultApiBaseUrl = typeof window !== "undefined"
  ? (isLocalhost ? "http://127.0.0.1:8000" : window.location.origin)
  : "http://127.0.0.1:8000"

const API_BASE_URL = (import.meta.env.VITE_API_BASE_URL || defaultApiBaseUrl).replace(/\/$/, "")
const WS_BASE_URL = (
  import.meta.env.VITE_WS_BASE_URL ||
  API_BASE_URL.replace(/^http/, "ws")
).replace(/\/$/, "")

async function request(path, options = {}) {
  const response = await fetch(`${API_BASE_URL}${path}`, {
    headers: {
      "Content-Type": "application/json",
      ...(options.headers || {}),
    },
    ...options,
  })

  if (!response.ok) {
    let detail = "Request failed"

    try {
      const payload = await response.json()
      detail = payload.detail || detail
    } catch {
      detail = response.statusText || detail
    }

    throw new Error(detail)
  }

  return response.json()
}

export function getWebSocketUrl(path = "/ws") {
  return `${WS_BASE_URL}${path}`
}

export function fetchReports(limit = 50) {
  return request(`/reports?limit=${limit}`)
}

export function fetchLatestReport() {
  return request("/reports/latest")
}

export function fetchAnalyticsSummary(limit = 120) {
  return request(`/analytics/summary?limit=${limit}`)
}

export function inspectImage(payload) {
  return request("/inspect/image", {
    method: "POST",
    body: JSON.stringify(payload),
  })
}

export function inspectFrame(payload) {
  return request("/inspect/frame", {
    method: "POST",
    body: JSON.stringify(payload),
  })
}