diff --git a/README.md b/README.md index 02e026f2..0bf5032e 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,10 @@ repo.show(function(err, repo) {}); Get contents at a particular path. ```js -repo.contents("path/to/dir", function(err, contents) {}); +repo.contents(branch, "path/to/file", function(err, contents) {}); +``` +```js +var contents = repo.contents(branch, "path/to/file"); ``` Fork repository. This operation runs asynchronously. You may want to poll for `repo.contents` until the forked repo is ready. diff --git a/github.js b/github.js index f9a1d726..5d047209 100644 --- a/github.js +++ b/github.js @@ -15,7 +15,7 @@ // // I'm not proud of this and neither should you be if you were responsible for the XMLHttpRequest spec. - function _request(method, path, data, cb, raw) { + function _request(method, path, data, cb, raw, sync) { function getURL() { var url = API_URL + path; return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime(); @@ -24,16 +24,18 @@ var xhr = new XMLHttpRequest(); if (!raw) {xhr.dataType = "json";} - xhr.open(method, getURL()); - xhr.onreadystatechange = function () { - if (this.readyState == 4) { - if (this.status >= 200 && this.status < 300 || this.status === 304) { - cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true); - } else { - cb({request: this, error: this.status}); + xhr.open(method, getURL(), !sync); + if (!sync) { + xhr.onreadystatechange = function () { + if (this.readyState == 4) { + if (this.status >= 200 && this.status < 300 || this.status === 304) { + cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true); + } else { + cb({request: this, error: this.status}); + } } - } - }; + }; + } xhr.setRequestHeader('Accept','application/vnd.github.raw'); xhr.setRequestHeader('Content-Type','application/json'); if ( @@ -46,6 +48,7 @@ ); } data ? xhr.send(JSON.stringify(data)) : xhr.send(); + if (sync) return xhr.response; } // User API @@ -329,6 +332,10 @@ _request("GET", repoPath + "/contents?ref=" + branch, { path: path }, cb); }; + this.contentsSync = function(branch, path) { + return _request("GET", repoPath + "/contents/" + path + "?ref=" + branch, null, null, 'raw', true); + }; + // Fork repository // -------