eris/ui/apiClient.ts

16 lines
425 B
TypeScript
Raw Normal View History

2023-10-08 21:23:54 +00:00
import { createFetcher, Output } from "t_rest/client";
import { ApiHandler } from "../api/serveApi.ts";
2023-10-05 09:00:51 +00:00
2023-10-08 21:23:54 +00:00
export const fetchApi = createFetcher<ApiHandler>({
baseUrl: `${location.origin}/api/`,
});
2023-10-05 09:00:51 +00:00
2023-10-08 21:23:54 +00:00
export function handleResponse<T extends Output>(
response: T,
): (T & { status: 200 })["body"]["data"] {
2023-10-05 09:00:51 +00:00
if (response.status !== 200) {
2023-10-08 21:23:54 +00:00
throw new Error(String(response.body.data));
2023-10-05 09:00:51 +00:00
}
2023-10-08 21:23:54 +00:00
return response.body.data;
2023-10-05 09:00:51 +00:00
}