Initial setup for new frontend.

This commit is contained in:
baldo 2022-02-22 15:39:39 +01:00
parent 7671bfd4d3
commit 59f7897d8e
33 changed files with 2594 additions and 12 deletions
frontend/src/utils

37
frontend/src/utils/Api.ts Normal file
View file

@ -0,0 +1,37 @@
class Api {
private baseURL: string = import.meta.env.BASE_URL;
private apiPrefix = "api/";
constructor(apiPrefix?: string) {
if (apiPrefix) {
this.apiPrefix = apiPrefix;
}
}
private toURL(path: string): string {
return this.baseURL + this.apiPrefix + path;
}
async get<T>(path: string, isT: (arg: unknown) => arg is T): Promise<T> {
const url = this.toURL(path);
const result = await fetch(url);
const json = await result.json();
if (!isT(json)) {
console.log(json);
throw new Error(`API get result has wrong type. ${url} => ${json}`);
}
return json;
}
}
export const api = new Api();
class InternalApi extends Api {
constructor() {
super("internal/api/");
}
}
export const internalApi = new InternalApi();