From dbc48c14f53fc6dfd4fc76be3fe2018a45836298 Mon Sep 17 00:00:00 2001 From: Lee Dohm Date: Thu, 30 Mar 2017 14:29:32 -0700 Subject: [PATCH] Add paginate to default github client object --- lib/paginate.js | 11 +++++++++++ lib/robot.js | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 lib/paginate.js diff --git a/lib/paginate.js b/lib/paginate.js new file mode 100644 index 0000000..cb8d7e0 --- /dev/null +++ b/lib/paginate.js @@ -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; +} diff --git a/lib/robot.js b/lib/robot.js index 9256f8f..bb188f6 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -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) {