124 lines
3.3 KiB
TypeScript
124 lines
3.3 KiB
TypeScript
import { CLAIM_NAME, SITE_ID } from "@/src/shared/config/env";
|
|
import { tokenManager } from "@/src/entities/auth/lib/token-manager";
|
|
import { DEV_URL_FO, DEV_URL_WR, LOCAL_URL } from "./config/env";
|
|
|
|
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 (part : string, url: string) => {
|
|
let reqUrl = "";
|
|
reqUrl = part === "fo" ? DEV_URL_FO+url : part === 'local' ? LOCAL_URL+url : DEV_URL_WR+url;
|
|
const response = await fetch(reqUrl , {
|
|
method: "GET",
|
|
headers: {
|
|
accept: "application/json",
|
|
"X-Site-Id": SITE_ID,
|
|
}
|
|
});
|
|
if (response.status === 401) {
|
|
handleUnauthorizedResponse();
|
|
}
|
|
if(!response.ok) {
|
|
throw new Error(`오류 발생: ${response.status}`);
|
|
}
|
|
const res = await response.json()
|
|
return res;
|
|
}
|
|
|
|
export const getFetchAreaList = async (url: string) => {
|
|
const response = await fetch(url , {
|
|
method: "GET",
|
|
headers: {
|
|
accept: "application/json",
|
|
"X-Site-Id": SITE_ID,
|
|
}
|
|
});
|
|
if (response.status === 401) {
|
|
handleUnauthorizedResponse();
|
|
}
|
|
if(!response.ok) {
|
|
throw new Error(`오류 발생: ${response.status}`);
|
|
}
|
|
const res = await response.json()
|
|
return res;
|
|
}
|
|
|
|
export const postFetch = async (part : string, url: string, params: unknown) => {
|
|
let reqUrl = "";
|
|
reqUrl = part === "fo" ? DEV_URL_FO+url : DEV_URL_WR+url;
|
|
const response = await fetch(reqUrl, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-Site-Id": SITE_ID,
|
|
},
|
|
body: JSON.stringify(params),
|
|
});
|
|
if (response.status === 401) {
|
|
handleUnauthorizedResponse();
|
|
}
|
|
if(!response.ok) {
|
|
throw new Error(`오류 발생: ${response.status}`);
|
|
}
|
|
return await response.json();
|
|
}
|
|
|
|
export const putFetch = async (part : string, url: string, params: unknown) => {
|
|
let reqUrl = "";
|
|
reqUrl = part === "fo" ? DEV_URL_FO+url : DEV_URL_WR+url;
|
|
const response = await fetch(reqUrl, {
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-Site-Id": SITE_ID,
|
|
},
|
|
body: JSON.stringify(params),
|
|
});
|
|
if (response.status === 401) {
|
|
handleUnauthorizedResponse();
|
|
}
|
|
if(!response.ok) {
|
|
throw new Error(`오류 발생: ${response.status}`);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
export const deleteFetch = async (part : string, url: string, params: unknown) => {
|
|
let reqUrl = "";
|
|
reqUrl = part === "fo" ? DEV_URL_FO+url : DEV_URL_WR+url;
|
|
const response = await fetch(reqUrl, {
|
|
method: "DELETE",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-Site-Id": SITE_ID,
|
|
},
|
|
body: JSON.stringify(params),
|
|
});
|
|
if (response.status === 401) {
|
|
handleUnauthorizedResponse();
|
|
}
|
|
if(!response.ok) {
|
|
throw new Error(`오류 발생: ${response.status}`);
|
|
}
|
|
return response;
|
|
}
|