자료관 앱을 원앤올 공통 패키지 기준으로 최신화
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
import { PROXY_HOST } from "@/shared/config/env";
|
||||
|
||||
export const LOCAL_URL = PROXY_HOST;
|
||||
export const DEV_URL_DC = "/dc-service/api/v1";
|
||||
@@ -0,0 +1 @@
|
||||
export * from './env';
|
||||
@@ -0,0 +1,88 @@
|
||||
import { DEV_URL_DC, LOCAL_URL } from "./config/env";
|
||||
import { LoginApi } from "@/entities/auth";
|
||||
import { SITE_ID } from "@/shared/config/env";
|
||||
|
||||
export async function fetchWithSilentRefresh(
|
||||
input: RequestInfo | URL,
|
||||
init: RequestInit,
|
||||
): Promise<Response> {
|
||||
const response = await fetch(input, init);
|
||||
|
||||
if (response.status !== 401) {
|
||||
return response;
|
||||
}
|
||||
|
||||
init.signal?.throwIfAborted();
|
||||
await LoginApi.silentRefresh();
|
||||
init.signal?.throwIfAborted();
|
||||
return fetch(input, init);
|
||||
}
|
||||
|
||||
export const getFetch = async (part : string, url: string, options?: Pick<RequestInit, "signal" | "cache">) => {
|
||||
let reqUrl = "";
|
||||
reqUrl = part === "dc" ? DEV_URL_DC+url : part === 'local' ? LOCAL_URL+url : DEV_URL_DC+url;
|
||||
const response = await fetchWithSilentRefresh(reqUrl , {
|
||||
method: "GET",
|
||||
headers: {
|
||||
accept: "application/json",
|
||||
"X-Site-Id": SITE_ID,
|
||||
},
|
||||
...options,
|
||||
});
|
||||
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 === "dc" ? DEV_URL_DC+url : LOCAL_URL+url;
|
||||
const response = await fetchWithSilentRefresh(reqUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-Site-Id": SITE_ID,
|
||||
},
|
||||
body: JSON.stringify(params),
|
||||
});
|
||||
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 === "dc" ? DEV_URL_DC+url : LOCAL_URL+url;
|
||||
const response = await fetchWithSilentRefresh(reqUrl, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-Site-Id": SITE_ID,
|
||||
},
|
||||
body: JSON.stringify(params),
|
||||
});
|
||||
if(!response.ok) {
|
||||
throw new Error(`오류 발생: ${response.status}`);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export const deleteFetch = async (part : string, url: string, params: unknown) => {
|
||||
let reqUrl = "";
|
||||
reqUrl = part === "dc" ? DEV_URL_DC+url : LOCAL_URL+url;
|
||||
const response = await fetchWithSilentRefresh(reqUrl, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-Site-Id": SITE_ID,
|
||||
},
|
||||
body: JSON.stringify(params),
|
||||
});
|
||||
if(!response.ok) {
|
||||
throw new Error(`오류 발생: ${response.status}`);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
Reference in New Issue
Block a user