Last active
October 20, 2015 03:31
-
-
Save rafaell-lycan/b8cc7156b6eaf00dd037 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 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