34 lines
675 B
TypeScript
34 lines
675 B
TypeScript
|
import axios from "axios";
|
||
|
import type { Error } from "./entities/error";
|
||
|
|
||
|
export async function apiFetch<T>(
|
||
|
path: string,
|
||
|
data:
|
||
|
| {
|
||
|
method?: string;
|
||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||
|
data?: any;
|
||
|
token?: string;
|
||
|
headers?: Record<string, string>;
|
||
|
version?: number;
|
||
|
}
|
||
|
| undefined = undefined,
|
||
|
) {
|
||
|
try {
|
||
|
const resp = await axios<T>({
|
||
|
method: data?.method || "GET",
|
||
|
url: `/api/v${data?.version || 1}${path}`,
|
||
|
data: data,
|
||
|
});
|
||
|
|
||
|
return resp.data;
|
||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||
|
} catch (err: any) {
|
||
|
if (err.response) {
|
||
|
throw err as Error;
|
||
|
}
|
||
|
|
||
|
throw err;
|
||
|
}
|
||
|
}
|