-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: octokit.paginate & octokit.paginate.iterator
- Loading branch information
Showing
14 changed files
with
75 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} |