i wont bother implementing this into the api, but think its a very usefull function, so thats why i wanna share it.
basicly an easy universal "wrapper" for pagination requests.
here you go :P
async function withPagination(func, args) {
try {
var result = await func(args);
while (result.pagination.cursor) {
const cursor = result.pagination.cursor;
//console.log(`paginating ${cursor}`);
const newargs = { ...args, after: cursor };
// new response with cursor
const resp = await func(newargs);
// replace cursor
result.pagination.cursor = resp.pagination.cursor;
// append data
for (let dat of resp.data) {
result.data.push(dat);
}
}
return result;
} catch (err) {
console.error(err);
}
}
example usage:
async function getClips() {
const users = await twitch.getUsers("someuser");
const user = users.data[0];
const broadcaster_id = user.id;
const clips = await withPagination(
async (args) => {
return await twitch.getClips(args);
},
{ broadcaster_id }
);
}
i wont bother implementing this into the api, but think its a very usefull function, so thats why i wanna share it.
basicly an easy universal "wrapper" for pagination requests.
here you go :P
example usage: