2023-11-17 23:52:51 +00:00
|
|
|
import { edenFetch } from "elysia/eden";
|
|
|
|
import { Api } from "../api/serveApi.ts";
|
2023-10-05 09:00:51 +00:00
|
|
|
|
2023-11-20 00:33:38 +00:00
|
|
|
export const API_URL = "/api";
|
2023-10-05 09:00:51 +00:00
|
|
|
|
2023-11-20 00:33:38 +00:00
|
|
|
export const fetchApi = edenFetch<Api>(API_URL);
|
|
|
|
|
|
|
|
export function handleResponse<
|
|
|
|
T extends
|
|
|
|
| { data: unknown; error: null }
|
|
|
|
| { data: null; error: { status: number; value: unknown } },
|
|
|
|
>(
|
|
|
|
response: T,
|
|
|
|
): (T & { error: null })["data"] {
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(`${response.error?.status}: ${response.error?.value}`);
|
2023-10-05 09:00:51 +00:00
|
|
|
}
|
2023-11-20 00:33:38 +00:00
|
|
|
return response.data;
|
2023-10-05 09:00:51 +00:00
|
|
|
}
|