forked from pinks/eris
1
0
Fork 0
eris/ui/apiClient.ts

20 lines
495 B
TypeScript
Raw Permalink Normal View History

import { edenFetch } from "elysia/eden";
import { Api } from "../api/serveApi.ts";
2023-10-05 09:00:51 +00:00
export const API_URL = "/api";
2023-10-05 09:00:51 +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 } },
>(
2023-10-08 21:23:54 +00:00
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
}
return response.data;
2023-10-05 09:00:51 +00:00
}