Last active
September 23, 2022 13:48
-
-
Save celsowhite/897e54af3684ec1ea006de72f2589a04 to your computer and use it in GitHub Desktop.
Call an api recursively to gather all data across pages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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