Skip to content

Instantly share code, notes, and snippets.

@rafaell-lycan
Last active October 20, 2015 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rafaell-lycan/b8cc7156b6eaf00dd037 to your computer and use it in GitHub Desktop.
Save rafaell-lycan/b8cc7156b6eaf00dd037 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 path = args.join('/');
return this.request('GET', path);
}
request(method, path) {
return new Promise( (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