Skip to content

Commit 52b700a

Browse files
committed
Merge pull request github-tools#9 from mattpass/master
Raw XHR to replace jQuery AJAX
2 parents 88cc388 + e528978 commit 52b700a

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

github.js

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Github.js 0.6.1
1+
// Github.js 0.6.2
22
// (c) 2012 Michael Aufreiter, Development Seed
33
// Github.js is freely distributable under the MIT license.
44
// For all details and documentation:
@@ -13,38 +13,26 @@
1313
// Util
1414
// =======
1515

16-
function headers() {
17-
var headers = {}
18-
if (options.auth === 'oauth' && !options.token) return { Accept: 'application/vnd.github.raw' };
19-
if (options.auth === 'basic' && (!options.username || !options.password)) return { Accept: 'application/vnd.github.raw' };
20-
return options.auth == 'oauth'
21-
? { Authorization: 'token '+ options.token, Accept: 'application/vnd.github.raw' }
22-
: { Authorization : 'Basic ' + Base64.encode(options.username + ':' + options.password), Accept: 'application/vnd.github.raw' }
23-
}
24-
25-
function _request(method, path, data, cb) {
26-
$.ajax({
27-
type: method,
28-
url: API_URL + path,
29-
data: JSON.stringify(data),
30-
dataType: 'json',
31-
contentType: 'application/x-www-form-urlencoded',
32-
success: function(res) { cb(null, res); },
33-
error: function(err) { cb(err); },
34-
headers : headers()
35-
});
36-
}
37-
38-
function _raw_request(method, path, data, cb) {
39-
$.ajax({
40-
type: method,
41-
url: API_URL + path,
42-
data: JSON.stringify(data),
43-
contentType: 'application/x-www-form-urlencoded',
44-
success: function(res) { cb(null, res); },
45-
error: function(err) { cb(err); },
46-
headers : headers()
47-
});
16+
function _request(method, path, data, cb, raw) {
17+
var xhr = new XMLHttpRequest();
18+
if (!raw) {xhr.dataType = "json"}
19+
xhr.open(method, API_URL + path);
20+
xhr.onreadystatechange = function () {
21+
if (this.readyState == 4) {
22+
this.status == 200 ? cb(null, JSON.parse(this.responseText)) : cb(this.status);
23+
}
24+
}
25+
xhr.setRequestHeader('Accept','application/vnd.github.raw');
26+
if (
27+
(options.auth == 'oauth' && options.token) ||
28+
(options.auth == 'basic' && options.username && options.password)
29+
) {
30+
xhr.setRequestHeader('Authorization',options.auth == 'oauth'
31+
? 'token '+ options.token
32+
: 'Basic ' + Base64.encode(options.username + ':' + options.password)
33+
);
34+
}
35+
data ? xhr.send(JSON.stringify(data)) : xhr.send();
4836
}
4937

5038
// User API
@@ -156,7 +144,7 @@
156144
// -------
157145

158146
this.getBlob = function(sha, cb) {
159-
_raw_request("GET", repoPath + "/git/blobs/" + sha, null, cb);
147+
_request("GET", repoPath + "/git/blobs/" + sha, null, cb, 'raw');
160148
};
161149

162150
// For a given file path, get the corresponding sha (blob for files, tree for dirs)

0 commit comments

Comments
 (0)