mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 06:58:46 +00:00
/api/client/[id]/key Endpoint 삭제 및 프론트엔드와의 Zod 스키마 공유 구현
This commit is contained in:
41
src/lib/hooks/callApi.ts
Normal file
41
src/lib/hooks/callApi.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { signRequest } from "$lib/modules/crypto";
|
||||
|
||||
export const refreshToken = async () => {
|
||||
return await fetch("/api/auth/refreshToken", { method: "POST" });
|
||||
};
|
||||
|
||||
const callApi = async (input: RequestInfo, init?: RequestInit) => {
|
||||
let res = await fetch(input, init);
|
||||
if (res.status === 401) {
|
||||
res = await refreshToken();
|
||||
if (!res.ok) {
|
||||
return res;
|
||||
}
|
||||
res = await fetch(input, init);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
export const callGetApi = async (input: RequestInfo) => {
|
||||
return await callApi(input);
|
||||
};
|
||||
|
||||
export const callPostApi = async <T>(input: RequestInfo, payload: T) => {
|
||||
return await callApi(input, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
};
|
||||
|
||||
export const callSignedPostApi = async <T>(input: RequestInfo, payload: T, signKey: CryptoKey) => {
|
||||
return await callApi(input, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: await signRequest(payload, signKey),
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user