RavindranadhM's picture
Deploy manufacturing monitoring system
201b13c verified
Raw
History Blame Contribute Delete
1.63 kB
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),
})
}