File size: 1,319 Bytes
da8e27d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios, { AxiosError } from 'axios'
import type { PredictResponse } from '../types'

const API_BASE = '/api'   // proxied to http://localhost:8000 by Vite

export async function predictPathology(files: File[]): Promise<PredictResponse> {
  const formData = new FormData()
  files.forEach((file) => formData.append('files', file))

  try {
    const { data } = await axios.post<PredictResponse>(
      `${API_BASE}/v1/predict`,
      formData,
      { headers: { 'Content-Type': 'multipart/form-data' } },
    )
    return data
  } catch (err: unknown) {
    if (err instanceof AxiosError) {
      // No response at all → server not running or CORS/network block
      if (!err.response) {
        throw new Error(
          'Không thể kết nối đến máy chủ (cổng 8000). ' +
          'Vui lòng khởi động Python Backend Server rồi thử lại.',
        )
      }

      // Server responded with an HTTP error status
      const body = err.response.data as Record<string, unknown> | undefined
      const detail =
        (body?.detail as string | undefined) ??
        (body?.error  as string | undefined) ??
        err.message
      throw new Error(`Lỗi máy chủ (HTTP ${err.response.status}): ${detail}`)
    }

    // Re-throw anything that isn't an Axios error
    throw err
  }
}