66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
const BASE_URL = process.env.NEXT_PUBLIC_SEWERAGE_API_URL || '';
|
|
|
|
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);
|
|
if (res.ok) {
|
|
const text = await res.text();
|
|
if (text) {
|
|
return JSON.parse(text); // JSON 데이터가 있으면 파싱하여 반환
|
|
}
|
|
return true; // 응답 본문이 비어 있으면 true 반환
|
|
}
|
|
};
|
|
|
|
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 getData = async (url: string): Promise<any> => {
|
|
const res = await fetch(url.startsWith('/') ? `${url}` : `${BASE_URL}/${url}`, {
|
|
method: "GET",
|
|
});
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
return data;
|
|
}
|
|
throw new Error('Failed to fetch data');
|
|
};
|
|
|
|
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;
|
|
}; |