Fix item loading
This commit is contained in:
19
web/lib/fetchAllPages.js
Normal file
19
web/lib/fetchAllPages.js
Normal file
@@ -0,0 +1,19 @@
|
||||
export async function fetchAllPages(urlBuilder) {
|
||||
const items = [];
|
||||
let page = 0;
|
||||
let totalPages = 1;
|
||||
|
||||
while (page < totalPages) {
|
||||
const res = await fetch(urlBuilder(page));
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status} – ${res.statusText}`);
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
items.push(...(data.content ?? []));
|
||||
totalPages = Math.max(data.totalPages ?? 1, 1);
|
||||
page += 1;
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
Reference in New Issue
Block a user