better typing for queries

This commit is contained in:
Jörn-Michael Miehe 2022-09-16 21:45:59 +00:00
parent 1ebaf9389f
commit 208af9d55b
2 changed files with 61 additions and 22 deletions

View file

@ -89,10 +89,18 @@ export default class App extends Vue {
private update(): void {
// Update Logo Config
this.$ovdashboard.api_query_simple_object("misc/config/logo", (data) => {
this.logo_above = data.above as string;
this.logo_below = data.below as string;
});
type LogoConfig = {
above: string;
below: string;
};
this.$ovdashboard.api_query_simple_object<LogoConfig>(
"misc/config/logo",
(data) => {
this.logo_above = data.above;
this.logo_below = data.below;
}
);
// Update Title
this.$ovdashboard.api_query_simple_string("text/get/html/title", (data) => {
@ -107,11 +115,20 @@ export default class App extends Vue {
});
// Update Image Config
this.$ovdashboard.api_query_simple_object("image/config", (data) => {
this.image_height = data.height as number;
this.image_contain = data.contain as boolean;
this.image_speed = data.speed as number;
});
type ImageConfig = {
height: number;
contain: boolean;
speed: number;
};
this.$ovdashboard.api_query_simple_object<ImageConfig>(
"image/config",
(data) => {
this.image_height = data.height;
this.image_contain = data.contain;
this.image_speed = data.speed;
}
);
// Update Message
this.$ovdashboard.api_query_simple_string(
@ -140,15 +157,30 @@ export default class App extends Vue {
});
// Update Calendar Config
this.$ovdashboard.api_query_simple_object("calendar/config", (data) => {
this.calendar_speed = data.speed as number;
});
type CalendarConfig = {
speed: number;
};
this.$ovdashboard.api_query_simple_object<CalendarConfig>(
"calendar/config",
(data) => {
this.calendar_speed = data.speed;
}
);
// Update Server Config
this.$ovdashboard.api_query_simple_object("misc/config/server", (data) => {
this.server_host = data.host as string;
this.server_name = data.name as string;
});
type ServerConfig = {
host: string;
name: string;
};
this.$ovdashboard.api_query_simple_object<ServerConfig>(
"misc/config/server",
(data) => {
this.server_host = data.host;
this.server_name = data.name;
}
);
// Update Version
this.$ovdashboard.api_query_simple_string("misc/version", (data) => {
@ -166,9 +198,16 @@ export default class App extends Vue {
});
// Update Ticker Config
this.$ovdashboard.api_query_simple_object("ticker/config", (data) => {
this.ticker_color = data.color as string;
});
type TickerConfig = {
color: string;
};
this.$ovdashboard.api_query_simple_object<TickerConfig>(
"ticker/config",
(data) => {
this.ticker_color = data.color;
}
);
}
public created(): void {

View file

@ -83,15 +83,15 @@ export class OVDashboardPlugin {
});
}
public api_query_simple_object(
public api_query_simple_object<Type extends object>(
endpoint: string,
on_success: (data: Record<string, unknown>) => void
on_success: (data: Type) => void
): void {
this.api_query_simple(endpoint, (data) => {
if (typeof data !== "object") return;
if (data === null) return;
on_success(data as Record<string, unknown>);
on_success(data as Type);
});
}
}