주식 매매 api 및 화면 오류 수정
This commit is contained in:
31
src/api.js
31
src/api.js
@@ -1,6 +1,31 @@
|
||||
// src/api.js
|
||||
const API_BASE = import.meta.env.VITE_API_BASE || "";
|
||||
|
||||
const toApiUrl = (path) => {
|
||||
if (!API_BASE) return path;
|
||||
|
||||
const baseClean = API_BASE.replace(/\/+$/, "");
|
||||
const baseForJoin = `${baseClean}/`;
|
||||
const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
|
||||
let pathForJoin = normalizedPath;
|
||||
|
||||
if (baseClean.endsWith("/api") && normalizedPath.startsWith("api/")) {
|
||||
pathForJoin = normalizedPath.slice(4);
|
||||
}
|
||||
|
||||
try {
|
||||
const baseUrl = new URL(baseForJoin, window.location.origin);
|
||||
return new URL(pathForJoin, baseUrl).toString();
|
||||
} catch (error) {
|
||||
console.warn("Invalid VITE_API_BASE, falling back to relative URL.", error);
|
||||
return path;
|
||||
}
|
||||
};
|
||||
|
||||
export async function apiGet(path) {
|
||||
const res = await fetch(path, { headers: { "Accept": "application/json" } });
|
||||
const res = await fetch(toApiUrl(path), {
|
||||
headers: { "Accept": "application/json" },
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
throw new Error(`HTTP ${res.status} ${res.statusText}: ${text}`);
|
||||
@@ -9,7 +34,7 @@ export async function apiGet(path) {
|
||||
}
|
||||
|
||||
export async function apiDelete(path) {
|
||||
const res = await fetch(path, { method: "DELETE" });
|
||||
const res = await fetch(toApiUrl(path), { method: "DELETE" });
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
throw new Error(`HTTP ${res.status} ${res.statusText}: ${text}`);
|
||||
@@ -18,7 +43,7 @@ export async function apiDelete(path) {
|
||||
}
|
||||
|
||||
export async function apiPost(path, body) {
|
||||
const res = await fetch(path, {
|
||||
const res = await fetch(toApiUrl(path), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Accept": "application/json",
|
||||
|
||||
Reference in New Issue
Block a user