Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
simple basic-auth node github api
// 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); });
}
@prezine
Copy link

prezine commented Jul 18, 2018

git.js:6 Uncaught ReferenceError: require is not defined at... How to handle this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment