Spaces:
Runtime error
Runtime error
File size: 1,497 Bytes
cd6f98e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import axios from "axios";
import type { Session } from "next-auth";
import { env } from "../env/client.mjs";
export const post = async <T>(url: string, body: unknown, session?: Session) => {
const headers = getHeaders(session);
url = getUrl(url);
return (
await axios.post(url, body, {
headers,
})
).data as T;
};
export const get = async <T>(url: string, session?: Session) => {
const headers = getHeaders(session);
url = getUrl(url);
return (
await axios.get(url, {
headers,
})
).data as T;
};
export const delete_ = async <T>(url: string, accessToken?: string) => {
const headers: Record<string, string> = {};
if (accessToken) headers.Authorization = `Bearer ${accessToken}`;
url = getUrl(url);
return (
await axios.delete(url, {
headers,
})
).data as T;
};
export function getHeaders(session?: Session) {
const headers: Record<string, string> = {};
if (session?.accessToken) {
headers.Authorization = `Bearer ${session.accessToken}`;
}
return headers;
}
function getUrl(url: string) {
return env.NEXT_PUBLIC_BACKEND_URL + url;
}
export async function withRetries(
fn: () => Promise<void>,
onError: (error: unknown) => Promise<boolean>, // Function to handle the error and return whether to continue retrying
retries = 3
): Promise<void> {
for (let i = 0; i < retries + 1; i++) {
try {
return await fn();
} catch (error) {
if (!(await onError(error))) return;
}
}
}
|