Skip to content

Commit 5a8526c

Browse files
committed
Can now get contents via sync method
Uses most of pull request from @dhcole and closes #46 To avoid code duplication though, sync has been added as an extra param, instead of new function
1 parent c875027 commit 5a8526c

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ Show repository information
4949
repo.show(function(err, repo) {});
5050
```
5151

52-
Get contents at a particular path in a particular branch.
52+
Get contents at a particular path in a particular branch. Set sync to true to get contents via sync method.
5353

5454
```js
55-
repo.contents("master", "path/to/dir", function(err, contents) {});
55+
repo.contents(branch, "path/to/dir", function(err, contents) {}, sync);
5656
```
5757

5858
Fork repository. This operation runs asynchronously. You may want to poll for `repo.contents` until the forked repo is ready.

github.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//
3434
// I'm not proud of this and neither should you be if you were responsible for the XMLHttpRequest spec.
3535

36-
function _request(method, path, data, cb, raw) {
36+
function _request(method, path, data, cb, raw, sync) {
3737
function getURL() {
3838
var url = API_URL + path;
3939
return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime();
@@ -42,13 +42,15 @@
4242
var xhr = new XMLHttpRequest();
4343
if (!raw) {xhr.dataType = "json";}
4444

45-
xhr.open(method, getURL());
46-
xhr.onreadystatechange = function () {
47-
if (this.readyState == 4) {
48-
if (this.status >= 200 && this.status < 300 || this.status === 304) {
49-
cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true);
50-
} else {
51-
cb({request: this, error: this.status});
45+
xhr.open(method, getURL(), !sync);
46+
if (!sync) {
47+
xhr.onreadystatechange = function () {
48+
if (this.readyState == 4) {
49+
if (this.status >= 200 && this.status < 300 || this.status === 304) {
50+
cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true);
51+
} else {
52+
cb({request: this, error: this.status});
53+
}
5254
}
5355
}
5456
};
@@ -61,6 +63,7 @@
6163
);
6264
}
6365
data ? xhr.send(JSON.stringify(data)) : xhr.send();
66+
if (sync) return xhr.response;
6467
}
6568

6669

@@ -343,8 +346,8 @@
343346
// Get contents
344347
// --------
345348

346-
this.contents = function(branch, path, cb) {
347-
_request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, 'raw');
349+
this.contents = function(branch, path, cb, sync) {
350+
return _request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, 'raw', sync);
348351
};
349352

350353
// Fork repository

0 commit comments

Comments
 (0)