initial commit

This commit is contained in:
2024-11-12 00:31:10 +00:00
commit a7933f8b06
99 changed files with 6836 additions and 0 deletions

31
web/src/api.ts Normal file
View File

@@ -0,0 +1,31 @@
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) {
switch (res.status) {
case 401:
throw ApiError.BadRequest;
case 404:
throw ApiError.NotFound;
default:
throw ApiError.Internal;
}
}
return res;
}
export { ApiError, fetchApi };