feat(aio): implement GithubApi.getPaginated()

This commit is contained in:
Georgios Kalpakas
2017-02-28 14:01:20 +02:00
committed by Chuck Jazdzewski
parent 37348989f0
commit 951e653b0c
4 changed files with 110 additions and 88 deletions

View File

@ -51,6 +51,23 @@ export class GithubApi {
return `${pathname}${joiner}${search}`;
}
protected getPaginated<T>(pathname: string, baseParams: RequestParams = {}, currentPage: number = 0): Promise<T[]> {
const perPage = 100;
const params = {
...baseParams,
page: currentPage,
per_page: perPage,
};
return this.get<T[]>(pathname, params).then(items => {
if (items.length < perPage) {
return items;
}
return this.getPaginated(pathname, baseParams, currentPage + 1).then(moreItems => [...items, ...moreItems]);
});
}
protected request<T>(method: string, path: string, data: any = null): Promise<T> {
return new Promise<T>((resolve, reject) => {
const options = {

View File

@ -22,30 +22,11 @@ export class GithubPullRequests extends GithubApi {
}
public fetchAll(state: PullRequestState = 'all'): Promise<PullRequest[]> {
process.stdout.write(`Fetching ${state} pull requests...`);
return this.fetchUntilDone(state, 0);
}
console.log(`Fetching ${state} pull requests...`);
// Methods - Protected
protected fetchUntilDone(state: PullRequestState, currentPage: number): Promise<PullRequest[]> {
process.stdout.write('.');
const perPage = 100;
const pathname = `/repos/${this.repoSlug}/pulls`;
const params = {
page: currentPage,
per_page: perPage,
state,
};
const params = {state};
return this.get<PullRequest[]>(pathname, params).then(pullRequests => {
if (pullRequests.length < perPage) {
console.log('done');
return pullRequests;
}
return this.fetchUntilDone(state, currentPage + 1).
then(morePullRequests => [...pullRequests, ...morePullRequests]);
});
return this.getPaginated<PullRequest>(pathname, params);
}
}