File size: 1,229 Bytes
97207e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa53da8
 
97207e4
 
 
aa53da8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const getApiBaseUrl = () => {
  let url = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
  
  // ๋๋ถ€๋ถ„์˜ ์Šฌ๋ž˜์‹œ(/) ์ œ๊ฑฐ
  url = url.replace(/\/+$/, "");
  
  // url์— /api๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด ์ถ”๊ฐ€ (๋‹จ, ๋น„์–ด์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ)
  if (url && !url.endsWith("/api")) {
    url = `${url}/api`;
  }
  
  return url;
};

const API_BASE_URL = getApiBaseUrl();

export async function post(endpoint: string, data: any) {
  // endpoint๊ฐ€ /๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š์œผ๋ฉด ์ถ”๊ฐ€
  const path = endpoint.startsWith("/") ? endpoint : `/${endpoint}`;
  const response = await fetch(`${API_BASE_URL}${path}`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(data),
  });
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.detail || "Something went wrong");
  }
  return response.json();
}

export async function uploadImage(file: File) {
  const formData = new FormData();
  formData.append("file", file);

  const response = await fetch(`${API_BASE_URL}/ocr`, {
    method: "POST",
    body: formData,
  });

  if (!response.ok) {
    throw new Error("OCR failed");
  }

  return response.json();
}