embalses all

This commit is contained in:
2024-10-17 18:31:42 +02:00
parent b07af64247
commit 1e6c9c7fb2
8 changed files with 142 additions and 25 deletions

View File

@ -0,0 +1,44 @@
import ApiResponse from "../interfaces/ApiResponse";
import Embalse from "../interfaces/Embalse";
import Env from "./Env";
export default class Api {
static async embalses(): Promise<Embalse[]> {
return Api._makeReq('/embalses');
}
static async _makeReq<T>(endpoint: string): Promise<T[]> {
let hasMore = true;
let url = Env.api_base() + endpoint;
let data: T[] = [];
while (hasMore) {
const res = await fetch(url);
if (!res.ok) {
throw new Error("Api error");
}
const json = await res.json() as ApiResponse<T>;
data.push(...json.items);
hasMore = json.hasMore;
if (hasMore) {
let found = false;
let i = 0;
while (!found) {
if (json.links[i].rel === 'next') {
url = json.links[i].href;
found = true;
}
i++;
}
}
}
return data;
}
}