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