ashfortune
๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ˆ˜์ •
97207e4
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();
}