Files
tesseract/web/src/api.ts

33 lines
645 B
TypeScript
Raw Normal View History

2024-11-12 00:31:10 +00:00
import { promiseOrThrow } from "./lib/errors";
enum ApiError {
NotFound = "NOT_FOUND",
BadRequest = "BAD_REQUEST",
Internal = "INTERNAL",
Network = "NETWORK",
}
async function fetchApi(
url: URL | RequestInfo,
init?: RequestInit,
): Promise<Response> {
const res = await promiseOrThrow(
fetch(`${import.meta.env.VITE_API_URL}/api${url}`, init),
() => ApiError.Network,
);
if (res.status !== 200) {
2024-11-17 18:10:35 +00:00
console.log(res.status);
2024-11-12 00:31:10 +00:00
switch (res.status) {
case 401:
throw ApiError.BadRequest;
case 404:
throw ApiError.NotFound;
default:
throw ApiError.Internal;
}
}
return res;
}
export { ApiError, fetchApi };