use Advent22 api_get<T>

This commit is contained in:
Jörn-Michael Miehe 2023-09-07 17:00:28 +00:00
parent 27db0ea908
commit a9754f77bc
3 changed files with 16 additions and 26 deletions

View file

@ -41,11 +41,11 @@ export default class extends Vue {
public mounted() { public mounted() {
this.$advent22 this.$advent22
.api_get_string("days/date") .api_get<string>("days/date")
.then((date: string) => (this.date = date)); .then((date: string) => (this.date = date));
this.$advent22 this.$advent22
.api_get_number("days/visible_days") .api_get<number>("days/visible_days")
.then((visible_days: number) => (this.visible_days = visible_days)); .then((visible_days: number) => (this.visible_days = visible_days));
} }
} }

View file

@ -51,15 +51,13 @@ export default class extends Vue {
public doors: Door[] = []; public doors: Door[] = [];
public load_doors() { public load_doors() {
this.$advent22 this.$advent22.api_get<DoorSerialized[]>("/general/doors").then((data) => {
.api_get_json<DoorSerialized[]>("/general/doors") this.doors.length = 0;
.then((data) => {
this.doors.length = 0;
for (const value of data) { for (const value of data) {
this.doors.push(Door.deserialize(value)); this.doors.push(Door.deserialize(value));
} }
}); });
} }
public save_doors() { public save_doors() {

View file

@ -42,9 +42,9 @@ export class Advent22 {
this.api_auth = { username: username, password: password }; this.api_auth = { username: username, password: password };
} }
private api_get<T>(endpoint: string): Promise<T>; private _api_get<T>(endpoint: string): Promise<T>;
private api_get<T>(endpoint: string, responseType: ResponseType): Promise<T>; private _api_get<T>(endpoint: string, responseType: ResponseType): Promise<T>;
private api_get<T>( private _api_get<T>(
endpoint: string, endpoint: string,
responseType: ResponseType = "json", responseType: ResponseType = "json",
): Promise<T> { ): Promise<T> {
@ -64,9 +64,13 @@ export class Advent22 {
}); });
} }
public api_get<T>(endpoint: string): Promise<T> {
return this._api_get<T>(endpoint);
}
public api_get_blob(endpoint: string): Promise<string> { public api_get_blob(endpoint: string): Promise<string> {
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
this.api_get<Blob>(endpoint, "blob") this._api_get<Blob>(endpoint, "blob")
.then((data: Blob) => { .then((data: Blob) => {
const reader = new FileReader(); const reader = new FileReader();
reader.readAsDataURL(data); reader.readAsDataURL(data);
@ -82,18 +86,6 @@ export class Advent22 {
}); });
} }
public api_get_string(endpoint: string): Promise<string> {
return this.api_get<string>(endpoint);
}
public api_get_number(endpoint: string): Promise<number> {
return this.api_get<number>(endpoint);
}
public api_get_json<T>(endpoint: string): Promise<T> {
return this.api_get<T>(endpoint);
}
public api_put(endpoint: string, data: unknown): Promise<void> { public api_put(endpoint: string, data: unknown): Promise<void> {
return new Promise<void>((resolve, reject) => return new Promise<void>((resolve, reject) =>
this.axios this.axios