|
33 | 33 | // |
34 | 34 | // I'm not proud of this and neither should you be if you were responsible for the XMLHttpRequest spec. |
35 | 35 |
|
36 | | - function _request(method, path, data, cb, raw) { |
| 36 | + function _request(method, path, data, cb, raw, sync) { |
37 | 37 | function getURL() { |
38 | 38 | var url = API_URL + path; |
39 | 39 | return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime(); |
|
42 | 42 | var xhr = new XMLHttpRequest(); |
43 | 43 | if (!raw) {xhr.dataType = "json";} |
44 | 44 |
|
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 | + } |
52 | 54 | } |
53 | 55 | } |
54 | 56 | }; |
|
61 | 63 | ); |
62 | 64 | } |
63 | 65 | data ? xhr.send(JSON.stringify(data)) : xhr.send(); |
| 66 | + if (sync) return xhr.response; |
64 | 67 | } |
65 | 68 |
|
66 | 69 |
|
|
343 | 346 | // Get contents |
344 | 347 | // -------- |
345 | 348 |
|
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); |
348 | 351 | }; |
349 | 352 |
|
350 | 353 | // Fork repository |
|
0 commit comments