Last active
May 21, 2020 15:57
-
-
Save luispinto23/8ca51380044b3b8e7b75a27b7505c0df to your computer and use it in GitHub Desktop.
fetch paginated API
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
const apiUrl = `https://exampleAPI.example`; | |
const reqParams = { | |
params: { | |
someParam: "ACME", | |
page: 1, | |
pageSize: 180 | |
} | |
} | |
let apiData = {}; | |
let keepGoing = true; | |
while (keepGoing) { | |
let response = await client.get(apiUrl, reqParams); | |
response.data.entries.map(record => | |
{ | |
apiData = { ...apiData, [record.key]: record.text }; | |
}); | |
reqParams.params.page = ++reqParams.params.page; | |
if (response.data.page === response.data.totalPages) { | |
keepGoing = false; | |
} | |
} | |
return apiData; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment