embalses all
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user