Skip to content

Instantly share code, notes, and snippets.

@felipenmoura
Forked from rafaell-lycan/GithubApi.js
Last active April 6, 2016 22:40
Show Gist options
  • Save felipenmoura/f0f5607f4371fa2a5f8b to your computer and use it in GitHub Desktop.
Save felipenmoura/f0f5607f4371fa2a5f8b to your computer and use it in GitHub Desktop.
Github API ES6
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