Skip to content

Instantly share code, notes, and snippets.

@amowu
Last active October 22, 2018 08:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amowu/5566485c9a8a64f3de171528f086fb24 to your computer and use it in GitHub Desktop.
Save amowu/5566485c9a8a64f3de171528f086fb24 to your computer and use it in GitHub Desktop.
GitHub API pagination with RxJS
const GitHubApi = require("github");
const Rx = require("rx");
const github = new GitHubApi();
const getCommitsAsync = (param) => github.repos.getCommits({
owner: 'amowu',
repo: 'test-semantic-release',
...param
});
const checkNextPage = (response) =>
github.hasNextPage(response)
? Rx.Observable.fromPromise(github.getNextPage(response))
: Rx.Observable.empty();
const concatAllCommits = (acc, curr) => acc.concat(curr.data);
const getAllCommits$ = Rx.Observable
.fromPromise(getCommitsAsync({ per_page: 100 }))
.expand(checkNextPage)
.reduce(concatAllCommits, []);
// getAllCommits$.subscribe(
// (commits) => console.log(commits)
// );
const commits = await getAllCommits$.toPromise();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment