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 = {