2023-09-07 01:17:14 +00:00
|
|
|
import axios, { AxiosInstance, ResponseType } from "axios";
|
|
|
|
import { App, Plugin } from "vue";
|
2022-11-03 15:04:57 +00:00
|
|
|
|
|
|
|
export class Advent22 {
|
2022-11-16 01:37:52 +00:00
|
|
|
private axios: AxiosInstance;
|
|
|
|
private api_auth = { username: "", password: "" };
|
|
|
|
|
|
|
|
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-12 17:46:09 +00:00
|
|
|
public name_door(day: number): string {
|
|
|
|
return `Türchen ${day}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
public set_api_auth(username: string, password: string) {
|
|
|
|
this.api_auth = { username: username, password: password };
|
|
|
|
}
|
|
|
|
|
|
|
|
public clear_api_auth() {
|
|
|
|
this.api_auth = { username: "", password: "" };
|
|
|
|
}
|
|
|
|
|
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> {
|
2022-11-16 01:37:52 +00:00
|
|
|
const req_data = {
|
|
|
|
auth: this.api_auth,
|
|
|
|
responseType: responseType,
|
|
|
|
};
|
|
|
|
|
2023-09-07 15:12:46 +00:00
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
this.axios
|
|
|
|
.get<T>(this.api_url(endpoint), req_data)
|
|
|
|
.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-07 17:00:28 +00:00
|
|
|
this._api_get<Blob>(endpoint, "blob")
|
2023-09-07 15:12:46 +00:00
|
|
|
.then((data: Blob) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsDataURL(data);
|
|
|
|
reader.onloadend = () => {
|
|
|
|
if (typeof reader.result === "string") {
|
|
|
|
resolve(reader.result);
|
|
|
|
} else {
|
2023-09-10 21:37:29 +00:00
|
|
|
reject(["failed data url", endpoint]);
|
2023-09-07 15:12:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
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> {
|
|
|
|
return new Promise<void>((resolve, reject) =>
|
|
|
|
this.axios
|
|
|
|
.put(this.api_url(endpoint), data)
|
|
|
|
.then(() => resolve())
|
|
|
|
.catch(reject),
|
|
|
|
);
|
|
|
|
}
|
2022-11-03 15:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Advent22Plugin: Plugin = {
|
|
|
|
install(app: App) {
|
|
|
|
app.config.globalProperties.$advent22 = new Advent22();
|
2023-09-07 01:17:14 +00:00
|
|
|
},
|
|
|
|
};
|