Spaces:
Sleeping
Sleeping
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();
}
|