/**
* Paginates results from a URL list with a timeout for temporary use.
* @param {string} urlListUrl - The URL to fetch the list of URLs from.
* @param {number} pageSize - The number of URLs to fetch per page.
* @param {number} timeoutMs - The timeout in milliseconds for each request.
* @returns {Promise<Array<string>>} - A promise that resolves to an array of URLs, or rejects on error.
*/
async function paginateUrls(urlListUrl, pageSize, timeoutMs) {
try {
const response = await fetch(urlListUrl);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json(); // Assuming the response is JSON
if (!Array.isArray(data)) {
throw new Error("Expected a JSON array of URLs.");
}
const allUrls = data;
const totalItems = allUrls.length;
const totalPages = Math.ceil(totalItems / pageSize);
async function getPage(pageNumber) {
const start = (pageNumber - 1) * pageSize;
const end = start + pageSize;
const pageUrls = allUrls.slice(start, end);
// Simulate a timeout for each request. In a real-world scenario,
// this would be handled by the underlying fetch API.
await new Promise(resolve => setTimeout(resolve, timeoutMs));
return pageUrls;
}
const allPages = [];
for (let page = 1; page <= totalPages; page++) {
allPages.push(getPage(page));
}
return allPages.flat(); // Flatten the array of arrays into a single array.
} catch (error) {
console.error("Error paginating URLs:", error);
throw error; // Re-throw the error for handling by the caller.
}
}
export default paginateUrls;
Add your comment