/** * ๐Ÿ“„ API Utility * API ํ˜ธ์ถœ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํŒŒ์ผ * * - ๋‹ค์–‘ํ•œ HTTP ๋ฉ”์†Œ๋“œ์˜ API ํ˜ธ์ถœ ํ•จ์ˆ˜ (GET, POST, PUT, DELETE) * - ์ฟ ํ‚ค ๊ธฐ๋ฐ˜ ์ธ์ฆ ํ† ํฐ ๊ด€๋ฆฌ ๋ฐ ์‚ฌ์šฉ์ž ์กฐํšŒ * - ํŠน์ • ์„œ๋น„์Šค (DC, CMS, GeoServer) ํ˜ธ์ถœ ์ง€์› * - Axios ๋ฐ Fetch ๊ธฐ๋ฐ˜ HTTP ์š”์ฒญ ์ฒ˜๋ฆฌ * - ํƒ€์ž… ์•ˆ์ „์„ฑ ๋ณด์žฅ์„ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค ํ™œ์šฉ * - ์—๋Ÿฌ ํ•ธ๋“ค๋ง ๋ฐ ์‘๋‹ต ์ฒ˜๋ฆฌ ํ‘œ์ค€ํ™” */ import { useEffect, useState } from "react"; import axios from 'axios'; const BASE_URL = '/sewerage-service/api'; export const getData = async (url: string): Promise => { const res = await fetch(url.startsWith('/') ? `${url}` : `${BASE_URL}/${url}`, { method: "GET", }); if (res.ok) { try { // ์‘๋‹ต์ด ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ const contentLength = res.headers.get('content-length'); if (contentLength === '0') { return null; } // ์‘๋‹ต ํ…์ŠคํŠธ๋ฅผ ๋จผ์ € ๊ฐ€์ ธ์™€์„œ ๋นˆ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธ const text = await res.text(); if (!text || text.trim() === '') { return null; } // JSON ํŒŒ์‹ฑ ์‹œ๋„ const data = JSON.parse(text); return data.data; } catch (error) { console.error('JSON parsing error:', error); // JSON ํŒŒ์‹ฑ์— ์‹คํŒจํ•œ ๊ฒฝ์šฐ, ์›๋ณธ ํ…์ŠคํŠธ ๋˜๋Š” null ๋ฐ˜ํ™˜ throw new Error('Invalid JSON response from server'); } } throw new Error('Failed to fetch data'); }; export const postData = async (url: string, data: object): Promise => { const options: RequestInit = { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(data), }; const res = await fetch(`${BASE_URL}/${url}`, options); if (res.ok) { const resData = await res.json(); console.log('postData response:', resData); return resData.data; } }; export const postMutiPart = async (url: string, data: FormData): Promise => { const res = await axios.post(`${BASE_URL}/${url}`, data, { timeout: 600000, // 10๋ถ„ }); console.log('postMultiPart response:', res); return res.data.data; }; export const putData = async (url: string, data: object): Promise => { const options: RequestInit = { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify(data), }; const res = await fetch(`${BASE_URL}/${url}`, options); const resData = await res.json(); console.log('putData response:', resData); return resData.data; }; export const deleteData = async (url: string, data?: object): Promise => { const options: RequestInit = { method: "DELETE", headers: { "Content-Type": "application/json", }, body: data ? JSON.stringify(data) : undefined, }; const res = await fetch(url.startsWith('/') ? `${url}` : `${BASE_URL}/${url}`, options); if (res.status === 204) return true return res; }; // DC API ํ˜ธ์ถœ export const getDcData = async (url: string, resType: string = "json"): Promise => { const res = await fetch('/dc-service' + url, { method: "GET", }); if (res.ok) { let data; if (resType === "json") data = await res.json(); else if (resType === "blob") data = await res.blob(); return data; } throw new Error('Failed to fetch data'); }; // CMS API ํ˜ธ์ถœ export const getCmsData = async (url: string, data?: object): Promise => { const res = await fetch('/cms-service' + url, { method: "GET", }); if (res.ok) { const data = await res.json(); return data; } throw new Error('Failed to fetch data'); }; // GeoServer API ํ˜ธ์ถœ export const getGeoData = async (url: string, data?: object): Promise => { const res = await fetch('/geoserver' + url, { method: "GET", }); if (res.ok) { const data = await res.json(); return data; } throw new Error('Failed to fetch data'); }; export function getCookie(name: string) { const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); if (match && match[2]) { return decodeURIComponent(match[2]); } return null; } export const useCookieAuth = () => { const [data, setData] = useState(""); useEffect(() => { const fetch = async () => { const result = getCookie("Authorization"); if (result) { setData(result); } }; fetch(); console.log("useCookieAuth ํ˜ธ์ถœ"); }, []); return data; };