mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-14 22:08:45 +00:00
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { signRequestBody } 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 signRequestBody(payload, signKey),
|
|
});
|
|
};
|