Add paginate to default github client object

This commit is contained in:
Lee Dohm 2017-03-30 14:29:32 -07:00
parent 4b483938f8
commit dbc48c14f5
No known key found for this signature in database
GPG Key ID: 9D43039B3B9C8C31
2 changed files with 20 additions and 1 deletions

11
lib/paginate.js Normal file
View File

@ -0,0 +1,11 @@
module.exports = async function paginate(responsePromise, callback) {
let response = await responsePromise;
let collection = await callback(response);
while (this.hasNextPage(response)) {
response = await this.getNextPage(response);
collection = collection.concat(await callback(response));
}
return collection;
}

View File

@ -35,7 +35,7 @@ class Robot {
const github = new GitHubApi({debug: process.env.LOG_LEVEL === 'trace'});
github.authenticate({type: 'token', token: token.token});
return rateLimitedClient(github);
return probotEnhancedClient(github);
}
log(...args) {
@ -43,6 +43,14 @@ class Robot {
}
}
function probotEnhancedClient(github) {
github = rateLimitedClient(github);
github.paginate = require('./lib/paginate');
return github;
}
// Hack client to only allow one request at a time with a 1s delay
// https://github.com/mikedeboer/node-github/issues/526
function rateLimitedClient(github) {