226 lines
5.6 KiB
TypeScript
226 lines
5.6 KiB
TypeScript
import { DEV_URL_FO, DEV_URL_WR, LOCAL_URL } from "../config/";
|
|
import { CLAIM_NAME } from "@/src/shared/config";
|
|
import { tokenManager } from "@/src/entities/auth/lib/token-manager";
|
|
|
|
export const fetchType = {
|
|
COMMON: "common",
|
|
WR: "wr",
|
|
LOCAL: "LOCAL",
|
|
CS: "CS",
|
|
};
|
|
|
|
const LOGIN_PAGE_URL =
|
|
process.env.NEXT_PUBLIC_CM_LOGIN_PAGE_URL || "/auth/login";
|
|
|
|
const handleUnauthorizedResponse = () => {
|
|
if (typeof window === "undefined") return;
|
|
|
|
const remainingMs = tokenManager.getRemainingMsFromToken(CLAIM_NAME);
|
|
const isExpired = remainingMs === null || remainingMs <= 0;
|
|
|
|
if (!isExpired) return;
|
|
|
|
tokenManager.clearAuthToken();
|
|
|
|
const isLoginPage =
|
|
window.location.pathname === "/auth/login" ||
|
|
window.location.pathname.startsWith("/auth/login/");
|
|
|
|
if (!isLoginPage) {
|
|
window.location.assign(LOGIN_PAGE_URL);
|
|
}
|
|
};
|
|
|
|
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 apiUrl = div + url;
|
|
const response = await fetch(apiUrl, {
|
|
method: "GET",
|
|
headers: {
|
|
accept: "application/json",
|
|
},
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
handleUnauthorizedResponse();
|
|
}
|
|
|
|
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 if(contentType.includes('application/octet-stream')) {
|
|
// 파일 응답 처리 (ex. PDF, Excel 등)
|
|
|
|
console.log('contentType ' ,contentType)
|
|
// 파일 응답 처리 (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; // 다운로드이므로 반환값 없음
|
|
}else{
|
|
throw Error('오류 발생')
|
|
}
|
|
};
|
|
|
|
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 apiUrl = div + url;
|
|
|
|
const response = await fetch(apiUrl, {
|
|
method: "GET",
|
|
headers: {
|
|
accept: "application/json",
|
|
},
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
handleUnauthorizedResponse();
|
|
}
|
|
|
|
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 if(contentType.includes('application/octet-stream')) {
|
|
// 파일 응답 처리 (ex. PDF, Excel 등)
|
|
|
|
console.log('contentType ' ,contentType)
|
|
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; // 다운로드이므로 반환값 없음
|
|
}else{
|
|
throw Error('오류 발생')
|
|
}
|
|
};
|
|
|
|
export const postFectch = async (url: string, params: any, type?: string) => {
|
|
const div = type === fetchType.COMMON ? DEV_URL_FO : DEV_URL_WR;
|
|
const apiUrl = div + url;
|
|
const response = await fetch(apiUrl, {
|
|
method: "POST",
|
|
headers: {
|
|
accept: "application/json",
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(params),
|
|
});
|
|
if (response.status === 401) {
|
|
handleUnauthorizedResponse();
|
|
}
|
|
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 apiUrl = div + url;
|
|
|
|
const response = await fetch(apiUrl, {
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(params),
|
|
});
|
|
if (response.status === 401) {
|
|
handleUnauthorizedResponse();
|
|
}
|
|
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 apiUrl = div + url;
|
|
|
|
const response = await fetch(apiUrl, {
|
|
method: "DELETE",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: params ? JSON.stringify(params) : undefined,
|
|
});
|
|
if (response.status === 401) {
|
|
handleUnauthorizedResponse();
|
|
}
|
|
if (!response.ok) {
|
|
// 에러 응답 본문 파싱
|
|
const errorText = await response.text();
|
|
let errorMessage = `오류 발생: ${response.status}`;
|
|
|
|
try {
|
|
const errorJson = JSON.parse(errorText);
|
|
errorMessage = errorJson.message || errorMessage;
|
|
} catch {
|
|
errorMessage = errorText || errorMessage;
|
|
}
|
|
|
|
throw new Error(errorMessage);
|
|
}
|
|
return await response.text();
|
|
};
|