simple basic-auth node github api
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
// Simple Use: | |
// ---------- | |
// github('/user/repos', callback); | |
// github('/repos/:user/:repo/issues', { user: user, repo: repo }, callback); | |
var request = require('request'); | |
var querystring = require('querystring'); | |
var github = function (path, options, callback) { | |
var username = 'username'; | |
var password = '********'; | |
var tokens = path.match(/:[^\/]+/g); | |
var method = options.method || 'get'; | |
if (typeof options == 'function') { | |
callback = options; | |
options = {}; | |
} | |
delete options.method; | |
tokens && tokens.forEach(function (token) { | |
var key = token.substr(1); | |
path = path.replace(token, options[key]); | |
delete options[key]; | |
}); | |
if (Object.keys(options).length) path += '?' + querystring.stringify(options); | |
request({ | |
url: 'https://api.github.com' + path, | |
json:true, | |
headers: { | |
'Host': 'api.github.com', | |
'Authorization': 'Basic ' + new Buffer(username + ':' + password).toString('base64') | |
} | |
}, function (err, response, body) { callback(err, body); }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
git.js:6 Uncaught ReferenceError: require is not defined at...
How to handle this?