156 lines
3.8 KiB
TypeScript
156 lines
3.8 KiB
TypeScript
//8880
|
|
|
|
import { CS_URL, DEV_URL_FO, DEV_URL_WR, LOCAL_URL } from "../config/";
|
|
|
|
export const fetchType = { COMMON: "common", WR: "wr", LOCAL: "LOCAL" , CS: "CS" };
|
|
|
|
export const getFetch = async (type: string, url: string) => {
|
|
const div =
|
|
type === fetchType.COMMON
|
|
? DEV_URL_FO
|
|
: type === fetchType.LOCAL
|
|
? LOCAL_URL
|
|
: type === fetchType.CS
|
|
? ''
|
|
: DEV_URL_WR;
|
|
|
|
const response = await fetch(div + url, {
|
|
method: "GET",
|
|
headers: {
|
|
accept: "application/json",
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
console.log(`오류 발생: ${response.status}`);
|
|
}
|
|
const contentType = response.headers.get("content-type") ?? "";
|
|
|
|
if (contentType.includes("application/json")) {
|
|
// JSON 응답 처리
|
|
const res = await response.json();
|
|
return res.data;
|
|
} else {
|
|
// 파일 응답 처리 (ex. PDF, Excel 등)
|
|
const blob = await response.blob();
|
|
const blobUrl = window.URL.createObjectURL(blob);
|
|
const a = document.createElement("a");
|
|
const filename = "downloaded_file.PDF";
|
|
|
|
a.href = blobUrl;
|
|
a.download = filename;
|
|
a.click();
|
|
window.URL.revokeObjectURL(blobUrl);
|
|
|
|
return null; // 다운로드이므로 반환값 없음
|
|
}
|
|
};
|
|
|
|
export const getFetchWithCount = async (type: string, url: string) => {
|
|
const div =
|
|
type === fetchType.COMMON
|
|
? DEV_URL_FO
|
|
: type === fetchType.LOCAL
|
|
? LOCAL_URL
|
|
: type === fetchType.CS
|
|
? ''
|
|
: DEV_URL_WR;
|
|
|
|
const response = await fetch(div + url, {
|
|
method: "GET",
|
|
headers: {
|
|
accept: "application/json",
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
console.log(`오류 발생: ${response.status}`);
|
|
}
|
|
|
|
const contentType = response.headers.get("content-type") ?? "";
|
|
|
|
if (contentType.includes("application/json")) {
|
|
// JSON 응답 처리
|
|
const res = await response.json();
|
|
|
|
|
|
return res;
|
|
} else {
|
|
// 파일 응답 처리 (ex. PDF, Excel 등)
|
|
const blob = await response.blob();
|
|
const blobUrl = window.URL.createObjectURL(blob);
|
|
const a = document.createElement("a");
|
|
const filename = "downloaded_file.PDF";
|
|
|
|
a.href = blobUrl;
|
|
a.download = filename;
|
|
a.click();
|
|
window.URL.revokeObjectURL(blobUrl);
|
|
|
|
return null; // 다운로드이므로 반환값 없음
|
|
}
|
|
};
|
|
|
|
export const postFectch = async (url: string, params: any, type?: string) => {
|
|
const div = type === fetchType.COMMON ? DEV_URL_FO : DEV_URL_WR;
|
|
|
|
const response = await fetch(div + url, {
|
|
method: "POST",
|
|
headers: {
|
|
accept: "application/json",
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(params),
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`오류 발생: ${response.status}`);
|
|
}
|
|
return await response.json();
|
|
};
|
|
|
|
export const putFectch = async (url: string, params: any,type?:string) => {
|
|
const div =
|
|
type === fetchType.COMMON
|
|
? DEV_URL_FO
|
|
: type === fetchType.LOCAL
|
|
? LOCAL_URL
|
|
: type === fetchType.CS
|
|
? ''
|
|
: DEV_URL_WR;
|
|
|
|
|
|
const response = await fetch(div + url, {
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(params),
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`오류 발생: ${response.status}`);
|
|
}
|
|
return response;
|
|
};
|
|
|
|
export const deleteFectch = async (url: string, params?: any, type?: string) => {
|
|
const div =
|
|
type === fetchType.COMMON
|
|
? DEV_URL_FO
|
|
: type === fetchType.LOCAL
|
|
? LOCAL_URL
|
|
: type === fetchType.CS
|
|
? ''
|
|
: DEV_URL_WR;
|
|
|
|
const response = await fetch(div + url, {
|
|
method: "DELETE",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: params?JSON.stringify(params):undefined,
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`오류 발생: ${response.status}`);
|
|
}
|
|
return await response.text();
|
|
}; |