148 lines
3.8 KiB
TypeScript
148 lines
3.8 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
|
|
const BASE_URL = process.env.NEXT_PUBLIC_SEWERAGE_API_URL || '';
|
|
|
|
export const getData = async (url: string, data?: object): Promise<any> => {
|
|
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;
|
|
} 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<any> => {
|
|
const options: RequestInit = {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(data),
|
|
};
|
|
const res = await fetch(`${BASE_URL}/${url}`, options);
|
|
console.log('res', res);
|
|
console.log('res.status', res.status);
|
|
if (res.ok) {
|
|
const resData = await res.json();
|
|
return resData;
|
|
}
|
|
};
|
|
|
|
export const postMutiPart = async (url: string, data: FormData): Promise<any> => {
|
|
const options: RequestInit = {
|
|
method: "POST",
|
|
body: data,
|
|
};
|
|
const res = await fetch(`${BASE_URL}/${url}`, options);
|
|
const resData = await res.json();
|
|
return resData;
|
|
};
|
|
|
|
export const putData = async (url: string, data: object): Promise<any> => {
|
|
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();
|
|
return resData;
|
|
};
|
|
|
|
export const deleteData = async (url: string, data?: object): Promise<any> => {
|
|
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, data?: object): Promise<any> => {
|
|
const res = await fetch('/dc-service' + url, {
|
|
method: "GET",
|
|
});
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
return data;
|
|
}
|
|
throw new Error('Failed to fetch data');
|
|
};
|
|
|
|
// CMS API 호출
|
|
export const getCmsData = async (url: string, data?: object): Promise<any> => {
|
|
console.log(url);
|
|
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<any> => {
|
|
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;
|
|
}; |