diff --git a/ui/src/App.vue b/ui/src/App.vue index ba00bc5..d288d3d 100644 --- a/ui/src/App.vue +++ b/ui/src/App.vue @@ -133,22 +133,19 @@ export default class App extends Vue { // Update Calendar Aggregates this.$ovdashboard.api_get_list("aggregate/list", (names) => { - let promises = names.map((name: string) => - this.$ovdashboard.api_get_prepare("aggregate/get/" + name) - ); - - Promise.all(promises) - .then((responses) => { + this.$ovdashboard.api_get_object_multi( + names.map((name) => "aggregate/get/" + name), + (calendars) => { this.calendar_data = []; for (let i = 0; i < names.length; i++) { this.calendar_data.push({ title: names[i], - events: responses[i].data, + events: calendars[i], }); } - }) - .catch(this.$ovdashboard.fail("Calendar Aggregates")); + } + ); }); // Update Calendar Config diff --git a/ui/src/plugins/ovdashboard.ts b/ui/src/plugins/ovdashboard.ts index c5b7c52..5bac2c7 100644 --- a/ui/src/plugins/ovdashboard.ts +++ b/ui/src/plugins/ovdashboard.ts @@ -45,16 +45,16 @@ export class OVDashboardPlugin { return this.api_baseurl + "/" + endpoint; } - public fail(name: string): (reason: unknown) => void { + private fail(name: string): (reason: unknown) => void { return (reason: unknown) => console.warn("Failed to query", name, "-", reason); } - public api_get_prepare(endpoint: string): AxiosPromise { + private api_get_prepare(endpoint: string): AxiosPromise { return this.axios.get(this.api_url(endpoint)); } - public api_get( + private api_get( endpoint: string, on_success: (data: T) => void ): void { @@ -77,17 +77,19 @@ export class OVDashboardPlugin { }); } + private check_array(value: unknown): value is T[] { + if (!Array.isArray(value)) return false; + if (!value.every((entry) => typeof entry === "string")) return false; + + return true; + } + public api_get_list( endpoint: string, on_success: (data: string[]) => void ): void { - this.api_get(endpoint, (data) => { - if (!Array.isArray(data)) { - console.error("Endpoint", endpoint, "did not return an Array:", data); - return; - } - - if (!data.every((value) => typeof value === "string")) { + this.api_get(endpoint, (data) => { + if (!this.check_array(data)) { console.error("Endpoint", endpoint, "did not return a string[]:", data); return; } @@ -96,24 +98,48 @@ export class OVDashboardPlugin { }); } + private check_object(value: unknown): value is object { + if (typeof value !== "object") return false; + if (value === null) return false; + + return true; + } + public api_get_object( endpoint: string, on_success: (data: Type) => void ): void { this.api_get(endpoint, (data) => { - if (typeof data !== "object") { + if (!this.check_object(data)) { console.error("Endpoint", endpoint, "did not return an Object:", data); return; } - if (data === null) { - console.error("Endpoint", endpoint, "returned NULL!"); - return; - } - on_success(data); }); } + + public api_get_object_multi( + endpoints: string[], + on_success: (data: Type[]) => void + ): void { + const promises = endpoints.map((endpoint) => this.api_get_prepare(endpoint)); + + Promise.all(promises) + .then((responses) => { + const data = responses.map((response) => response.data); + + for (let i = 0; i < data.length; i++) { + if (!this.check_object(data[i])) { + console.error("Response data was not an Object:", data[i]); + return; + } + } + + on_success(data); + }) + .catch(this.fail(endpoints.join(", "))); + } } export default new OVDashboardPlugin();