Skip to content

Instantly share code, notes, and snippets.

@fat
Created July 2, 2012 03:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fat/3030931 to your computer and use it in GitHub Desktop.
Save fat/3030931 to your computer and use it in GitHub Desktop.
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