advent22/ui/src/plugins/advent22.ts

162 lines
4.3 KiB
TypeScript
Raw Normal View History

2023-11-02 00:37:00 +00:00
import axios, { AxiosError, AxiosInstance, ResponseType } from "axios";
2023-09-07 01:17:14 +00:00
import { App, Plugin } from "vue";
2023-11-02 00:37:00 +00:00
import { advent22Store } from "./store";
2022-11-03 15:04:57 +00:00
export class Advent22 {
2022-11-16 01:37:52 +00:00
private axios: AxiosInstance;
public constructor() {
2023-09-07 15:13:10 +00:00
this.axios = axios.create({
timeout: 10000,
});
2022-11-16 01:37:52 +00:00
}
2022-11-03 15:04:57 +00:00
private get api_baseurl(): string {
// in production mode, return "//host/api"
if (process.env.NODE_ENV === "production") {
return `//${window.location.host}/api`;
} else if (process.env.NODE_ENV !== "development") {
// not in prouction or development mode
console.warn("Unexpected NODE_ENV value");
}
// in development mode, return "//hostname:8000/api"
return `//${window.location.hostname}:8000/api`;
}
2023-09-21 09:53:30 +00:00
public name_door(day: number): string {
2023-09-12 17:46:09 +00:00
return `Türchen ${day}`;
}
2023-09-21 09:53:30 +00:00
public format_user_error([reason, endpoint]: [unknown, string]): string {
2023-09-13 15:24:25 +00:00
let msg =
"Unbekannter Fehler, bitte wiederholen! Besteht das Problem länger, bitte Admin benachrichtigen!";
let code = "U";
const result = () => `${msg} (Fehlercode: ${code}/${endpoint})`;
if (!(reason instanceof AxiosError)) return result();
switch (reason.code) {
case "ECONNABORTED":
// API unerreichbar
msg =
"API antwortet nicht, bitte später wiederholen! Besteht das Problem länger, bitte Admin benachrichtigen!";
code = "D";
break;
case "ERR_NETWORK":
// Netzwerk nicht verbunden
msg = "Sieht aus, als sei deine Netzwerkverbindung gestört.";
code = "N";
break;
default:
if (reason.response === undefined) return result();
switch (reason.response.status) {
case 401:
// UNAUTHORIZED
msg = "Netter Versuch :)";
code = "A";
break;
case 422:
// UNPROCESSABLE ENTITY
msg = "Funktion ist kaputt, bitte Admin benachrichtigen!";
code = "I";
break;
default:
// HTTP
code = `H${reason.response.status}`;
break;
}
break;
}
return result();
}
2023-09-07 15:15:12 +00:00
public api_url(): string;
public api_url(endpoint: string): string;
2022-11-03 15:04:57 +00:00
public api_url(endpoint?: string): string {
2023-09-07 01:17:14 +00:00
if (endpoint === undefined) {
2022-11-22 22:27:57 +00:00
return this.api_baseurl;
2023-09-07 01:17:14 +00:00
}
2022-11-03 15:04:57 +00:00
2023-09-07 01:17:14 +00:00
while (endpoint.startsWith("/")) {
2022-12-22 00:15:59 +00:00
endpoint = endpoint.substring(1);
2023-09-07 01:17:14 +00:00
}
2022-12-22 00:15:59 +00:00
2022-11-03 15:04:57 +00:00
return `${this.api_baseurl}/${endpoint}`;
}
2022-11-16 01:37:52 +00:00
2023-09-07 17:00:28 +00:00
private _api_get<T>(endpoint: string): Promise<T>;
private _api_get<T>(endpoint: string, responseType: ResponseType): Promise<T>;
private _api_get<T>(
2022-11-16 01:37:52 +00:00
endpoint: string,
responseType: ResponseType = "json",
2023-09-07 15:12:46 +00:00
): Promise<T> {
2023-09-12 22:35:40 +00:00
const req_config = {
2023-11-02 00:37:00 +00:00
auth: advent22Store().axios_creds,
2022-11-16 01:37:52 +00:00
responseType: responseType,
};
2023-09-07 15:12:46 +00:00
return new Promise<T>((resolve, reject) => {
this.axios
2023-09-12 22:35:40 +00:00
.get<T>(this.api_url(endpoint), req_config)
2023-09-07 15:12:46 +00:00
.then((response) => resolve(response.data))
.catch((reason) => {
console.error(`Failed to query ${endpoint}: ${reason}`);
reject([reason, endpoint]);
});
});
2022-11-16 01:37:52 +00:00
}
2023-09-07 17:00:28 +00:00
public api_get<T>(endpoint: string): Promise<T> {
return this._api_get<T>(endpoint);
}
2023-09-07 15:12:46 +00:00
public api_get_blob(endpoint: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
2023-09-14 14:44:41 +00:00
this._api_get<Blob>(endpoint, "blob")
.then((data: Blob) => {
const reader = new FileReader();
reader.readAsDataURL(data);
reader.onloadend = () => {
if (typeof reader.result === "string") {
resolve(reader.result);
} else {
reject(["failed data url", endpoint]);
}
};
})
.catch(reject);
2023-09-07 15:12:46 +00:00
});
2022-11-16 01:37:52 +00:00
}
2022-12-08 23:21:52 +00:00
2023-09-07 16:44:44 +00:00
public api_put(endpoint: string, data: unknown): Promise<void> {
2023-09-12 22:35:40 +00:00
const req_config = {
2023-11-02 00:37:00 +00:00
auth: advent22Store().axios_creds,
2023-09-12 22:35:40 +00:00
};
2023-09-13 15:24:25 +00:00
return new Promise<void>((resolve, reject) => {
2023-09-07 16:44:44 +00:00
this.axios
2023-09-12 22:35:40 +00:00
.put(this.api_url(endpoint), data, req_config)
2023-09-07 16:44:44 +00:00
.then(() => resolve())
2023-09-13 15:24:25 +00:00
.catch((reason) => {
console.error(`Failed to query ${endpoint}: ${reason}`);
reject([reason, endpoint]);
});
});
2023-09-07 16:44:44 +00:00
}
2022-11-03 15:04:57 +00:00
}
2023-11-02 00:37:00 +00:00
export const ADVENT22 = new Advent22();
2022-11-03 15:04:57 +00:00
export const Advent22Plugin: Plugin = {
install(app: App) {
2023-11-02 00:37:00 +00:00
app.config.globalProperties.$advent22 = ADVENT22;
2023-09-07 01:17:14 +00:00
},
};