Skip to content

Commit

Permalink
feat: octokit.paginate & octokit.paginate.iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Nov 17, 2018
1 parent 974dba0 commit 00a692f
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 116 deletions.
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -5,7 +5,10 @@ const CORE_PLUGINS = [
require('./plugins/pagination'),
require('./plugins/register-endpoints'),
require('./plugins/rest-api-endpoints'),
require('./plugins/validate')
require('./plugins/validate'),

// deprecated: remove in v17
require('octokit-pagination-methods')
]

module.exports = Octokit.plugin(CORE_PLUGINS)
7 changes: 0 additions & 7 deletions plugins/pagination/get-first-page.js

This file was deleted.

7 changes: 0 additions & 7 deletions plugins/pagination/get-last-page.js

This file was deleted.

7 changes: 0 additions & 7 deletions plugins/pagination/get-next-page.js

This file was deleted.

15 changes: 0 additions & 15 deletions plugins/pagination/get-page-links.js

This file was deleted.

34 changes: 0 additions & 34 deletions plugins/pagination/get-page.js

This file was deleted.

7 changes: 0 additions & 7 deletions plugins/pagination/get-previous-page.js

This file was deleted.

7 changes: 0 additions & 7 deletions plugins/pagination/has-first-page.js

This file was deleted.

7 changes: 0 additions & 7 deletions plugins/pagination/has-last-page.js

This file was deleted.

7 changes: 0 additions & 7 deletions plugins/pagination/has-next-page.js

This file was deleted.

7 changes: 0 additions & 7 deletions plugins/pagination/has-previous-page.js

This file was deleted.

17 changes: 7 additions & 10 deletions plugins/pagination/index.js
@@ -1,12 +1,9 @@
module.exports = paginationPlugin
module.exports = paginatePlugin

function paginationPlugin (octokit) {
octokit.getFirstPage = require('./get-first-page').bind(null, octokit)
octokit.getLastPage = require('./get-last-page').bind(null, octokit)
octokit.getNextPage = require('./get-next-page').bind(null, octokit)
octokit.getPreviousPage = require('./get-previous-page').bind(null, octokit)
octokit.hasFirstPage = require('./has-first-page')
octokit.hasLastPage = require('./has-last-page')
octokit.hasNextPage = require('./has-next-page')
octokit.hasPreviousPage = require('./has-previous-page')
const iterator = require('./iterator')
const paginate = require('./paginate')

function paginatePlugin (octokit) {
octokit.paginate = paginate.bind(null, octokit)
octokit.paginate.iterator = iterator.bind(null, octokit)
}
44 changes: 44 additions & 0 deletions plugins/pagination/iterator.js
@@ -0,0 +1,44 @@
module.exports = iterator

function iterator (octokit, options) {
const state = {
page: options.page
}

return {
[Symbol.asyncIterator]: () => ({
next () {
state.page = (state.page || 0) + 1

if (state.done) {
return Promise.resolve({ done: true })
}

return octokit.request(Object.assign(options, { page: state.page }))

.then((response) => {
if (!hasNextPage(response)) {
state.done = true
}

return {
value: response
}
})
}
})
}
}

function hasNextPage (response) {
const link = response.headers.link || ''
const links = {}

// link format:
// '<https://api.github.com/users/aseemk/followers?page=2>; rel="next", <https://api.github.com/users/aseemk/followers?page=2>; rel="last"'
link.replace(/<([^>]*)>;\s*rel="([\w]*)"/g, (m, uri, type) => {
links[type] = uri
})

return !!links.next
}
20 changes: 20 additions & 0 deletions plugins/pagination/paginate.js
@@ -0,0 +1,20 @@
module.exports = paginate

const iterator = require('./iterator')

function paginate (octokit, route, options) {
options = octokit.request.endpoint.merge(route, options)
return gather([], iterator(octokit, options)[Symbol.asyncIterator]())
}

function gather (results, iterator) {
return iterator.next()
.then(result => {
if (result.done) {
return results
}

results.push.apply(results, result.value.data)
return gather(results, iterator)
})
}

0 comments on commit 00a692f

Please sign in to comment.