Skip to content

Instantly share code, notes, and snippets.

@luispinto23
Last active May 21, 2020 15:57
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 luispinto23/8ca51380044b3b8e7b75a27b7505c0df to your computer and use it in GitHub Desktop.
Save luispinto23/8ca51380044b3b8e7b75a27b7505c0df to your computer and use it in GitHub Desktop.
fetch paginated API
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