-
-
Save felipenmoura/f0f5607f4371fa2a5f8b to your computer and use it in GitHub Desktop.
Github API ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class GithubApi{ | |
constructor(){ | |
this.host = 'https://api.github.com'; | |
} | |
get(...params) { | |
let args = [].slice.call(params); | |
let callback = args.pop(); | |
let path = args.join('/'); | |
request('GET', path, callback); | |
} | |
request(method, path, callback) { | |
return new Promise(function(resolve, reject){ | |
let request = new XMLHttpRequest(); | |
request.onload = function () { | |
let result = request.responseText; | |
if (~request.getResponseHeader('content-type').indexOf('json')) { | |
result = JSON.parse(result); | |
} | |
resolve(result); | |
}; | |
request.onerror = err => reject(err); | |
request.open(method, this.host + '/' + path, true); | |
request.send(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment