Skip to content

Instantly share code, notes, and snippets.

@celsowhite
Last active September 23, 2022 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celsowhite/897e54af3684ec1ea006de72f2589a04 to your computer and use it in GitHub Desktop.
Save celsowhite/897e54af3684ec1ea006de72f2589a04 to your computer and use it in GitHub Desktop.
Call an api recursively to gather all data across pages.
// Data
const data = [];
// Recursive function to call the api in paginated batches. Depending on the api may need a limiter.
async function getDataRecursive(page) {
// Query for assets at a certain page.
const response = await axios.get(
`${apiUrl}?page=${page}&limit=250`,
{
headers: {
"X-Auth-Token": "",
},
}
);
// Add the items to the data set.
data.push(...response.data);
// If the amount of items in the response is zero then we've reached the end of pagination. Some apis may have more details about current page and total pages.
if (response.data.length === 0) {
return data;
}
// Else make a recursive call to the api for more assets.
else {
await getDataRecursive(page + 1);
}
}
// Initial call to kick requests off.
await getDataRecursive(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment