// /js/api-base.js // API 기본 설정 (비모듈 - 빠른 로딩용) (function() { 'use strict'; const API_PORT = 20005; const API_PATH = '/api'; function getApiBaseUrl() { const hostname = window.location.hostname; const protocol = window.location.protocol; return `${protocol}//${hostname}:${API_PORT}${API_PATH}`; } // 전역 API 설정 const apiUrl = getApiBaseUrl(); window.API_BASE_URL = apiUrl; window.API = apiUrl; // 이전 호환성 // 인증 헤더 생성 window.getAuthHeaders = function() { const token = localStorage.getItem('token'); return { 'Content-Type': 'application/json', 'Authorization': token ? `Bearer ${token}` : '' }; }; // API 호출 헬퍼 (기존 시그니처 유지: endpoint, method, data) // JSON 파싱하여 반환 window.apiCall = async function(endpoint, method = 'GET', data = null) { const url = `${window.API_BASE_URL}${endpoint}`; const config = { method: method, headers: window.getAuthHeaders() }; if (data && (method === 'POST' || method === 'PUT' || method === 'PATCH' || method === 'DELETE')) { config.body = JSON.stringify(data); } const response = await fetch(url, config); // 401 Unauthorized 처리 if (response.status === 401) { localStorage.removeItem('token'); localStorage.removeItem('user'); window.location.href = '/index.html'; throw new Error('인증이 만료되었습니다.'); } // JSON 파싱하여 반환 return response.json(); }; console.log('✅ API 설정 완료:', window.API_BASE_URL); })();