TraceStudio-dist/web/src/core/api/api.ts

139 lines
2.9 KiB
TypeScript
Raw Normal View History

2026-01-13 16:41:31 +08:00
/**
* TraceStudio API
* CORS
*/
import { API_BASE_URL, WS_BASE_URL } from './base'
if (import.meta.env.DEV) {
console.log('🔗 API Base URL:', API_BASE_URL)
}
interface ApiResponse<T = any> {
data?: T
error?: string
}
/**
*
*/
async function request<T = any>(
endpoint: string,
options: RequestInit = {}
): Promise<ApiResponse<T>> {
try {
const url = `${API_BASE_URL}${endpoint}`
const response = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
...options.headers,
},
})
if (!response.ok) {
// Try to include response body (JSON or text) to aid debugging (eg. 422 validation errors)
let bodyText = ''
try {
bodyText = await response.text()
} catch (e) {
bodyText = ''
}
throw new Error(`HTTP ${response.status}: ${response.statusText} ${bodyText}`)
}
const data = await response.json()
return { data }
} catch (error) {
console.error(`API 请求失败 [${endpoint}]:`, error)
return { error: error instanceof Error ? error.message : String(error) }
}
}
/**
* GET /api/plugins -
*/
export async function getPlugins(): Promise<ApiResponse<{
plugins: Record<string, any>
}>> {
return request('/api/plugins', { method: 'GET' })
}
/**
* POST /api/node/preview -
*/
export async function previewNode(payload: {
node: any
limit?: number
}): Promise<ApiResponse<{
columns?: string[]
preview?: any[]
error?: string
}>> {
return request('/api/node/preview', {
method: 'POST',
body: JSON.stringify(payload),
})
}
/**
* POST /api/graph/execute -
*/
export async function executeGraph(payload: {
nodes: any[]
edges: any[]
settings?: any
global_context?: any
}): Promise<ApiResponse<{
status: string
message?: string
execution_time?: number
error?: string
}>> {
return request('/api/graph/execute', {
method: 'POST',
body: JSON.stringify(payload),
})
}
/**
* WS /ws/graph/execute -
* WebSocket message/close/error
*/
export function executeGraphWS(payload: {
nodes: any[]
edges: any[]
settings?: any
}): WebSocket {
const url = `${WS_BASE_URL}/api/ws/graph/execute`
const ws = new WebSocket(url)
ws.onopen = () => {
try {
ws.send(JSON.stringify(payload))
} catch (e) {
console.error('WS 发送初始化消息失败:', e)
}
}
return ws
}
/**
*
*/
export async function healthCheck(): Promise<ApiResponse<{
status: string
service: string
version: string
}>> {
return request('/', { method: 'GET' })
}
/**
*
*/
export async function listUsers(): Promise<ApiResponse<{
users: string[]
}>> {
return request('/api/users/list', { method: 'GET' })
}