Skip to content

Commit 6d5f502

Browse files
committed
Merge pull request #82 from raphink/dev/auto-paging
Auto paging
2 parents 7b45b4d + 1599cbc commit 6d5f502

1 file changed

Lines changed: 36 additions & 6 deletions

File tree

github.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
function _request(method, path, data, cb, raw, sync) {
3737
function getURL() {
38-
var url = API_URL + path;
38+
var url = path.indexOf('//') >= 0 ? path : API_URL + path;
3939
return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime();
4040
}
4141

@@ -47,9 +47,9 @@
4747
xhr.onreadystatechange = function () {
4848
if (this.readyState == 4) {
4949
if (this.status >= 200 && this.status < 300 || this.status === 304) {
50-
cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true);
50+
cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true, this);
5151
} else {
52-
cb({request: this, error: this.status});
52+
cb({path: path, request: this, error: this.status});
5353
}
5454
}
5555
}
@@ -66,14 +66,42 @@
6666
if (sync) return xhr.response;
6767
}
6868

69+
function _requestAllPages(path, cb) {
70+
var results = [];
71+
(function iterate() {
72+
_request("GET", path, null, function(err, res, xhr) {
73+
if (err) {
74+
return cb(err);
75+
}
76+
77+
results.push.apply(results, res);
78+
79+
var links = (xhr.getResponseHeader('link') || '').split(/\s*,\s*/g),
80+
next = _.find(links, function(link) { return /rel="next"/.test(link); });
81+
82+
if (next) {
83+
next = (/<(.*)>/.exec(next) || [])[1];
84+
}
85+
86+
if (!next) {
87+
cb(err, results);
88+
} else {
89+
path = next;
90+
iterate();
91+
}
92+
});
93+
})();
94+
}
95+
6996

7097

7198
// User API
7299
// =======
73100

74101
Github.User = function() {
75102
this.repos = function(cb) {
76-
_request("GET", "/user/repos?type=all&per_page=1000&sort=updated", null, function(err, res) {
103+
// Github does not always honor the 1000 limit so we want to iterate over the data set.
104+
_requestAllPages("/user/repos?type=all&per_page=1000&sort=updated", function(err, res) {
77105
cb(err, res);
78106
});
79107
};
@@ -120,7 +148,8 @@
120148
// -------
121149

122150
this.userRepos = function(username, cb) {
123-
_request("GET", "/users/"+username+"/repos?type=all&per_page=1000&sort=updated", null, function(err, res) {
151+
// Github does not always honor the 1000 limit so we want to iterate over the data set.
152+
_requestAllPages("/users/"+username+"/repos?type=all&per_page=1000&sort=updated", function(err, res) {
124153
cb(err, res);
125154
});
126155
};
@@ -138,7 +167,8 @@
138167
// -------
139168

140169
this.orgRepos = function(orgname, cb) {
141-
_request("GET", "/orgs/"+orgname+"/repos?type=all&per_page=1000&sort=updated&direction=desc", null, function(err, res) {
170+
// Github does not always honor the 1000 limit so we want to iterate over the data set.
171+
_requestAllPages("/orgs/"+orgname+"/repos?type=all&&page_num=1000&sort=updated&direction=desc", function(err, res) {
142172
cb(err, res);
143173
});
144174
};

0 commit comments

Comments
 (0)