// /js/api-base.js // API 기본 설정 및 보안 유틸리티 (비모듈 - 빠른 로딩용) (function() { 'use strict'; // ==================== 보안 유틸리티 (XSS 방지) ==================== /** * HTML 특수문자 이스케이프 (XSS 방지) * innerHTML에 사용자 입력/API 데이터를 삽입할 때 반드시 사용 * * @param {string} str - 이스케이프할 문자열 * @returns {string} 이스케이프된 문자열 */ window.escapeHtml = function(str) { if (str === null || str === undefined) return ''; if (typeof str !== 'string') str = String(str); const htmlEntities = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '/': '/', '`': '`', '=': '=' }; return str.replace(/[&<>"'`=\/]/g, function(char) { return htmlEntities[char]; }); }; /** * URL 파라미터 이스케이프 */ window.escapeUrl = function(str) { if (str === null || str === undefined) return ''; return encodeURIComponent(String(str)); }; // ==================== API 설정 ==================== 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); })();