/api/client/[id]/key Endpoint 삭제 및 프론트엔드와의 Zod 스키마 공유 구현

This commit is contained in:
static
2025-01-02 04:44:02 +09:00
parent 45df24b416
commit b07d67b958
27 changed files with 241 additions and 169 deletions

41
src/lib/hooks/callApi.ts Normal file
View 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),
});
};