diff --git a/index.js b/index.js index 055ee05..bceeb8f 100644 --- a/index.js +++ b/index.js @@ -34,11 +34,15 @@ function convertSign(sign) { // adds parameters that describe how the results are displayed to the end of the url function appendResultParams(params, url) { -// add the page number to the end of the query, if a page is specified + // add the page number to the end of the query, if a page is specified if (typeof params['page'] !== 'undefined') { url += '&page=' + params['page']; } + if (typeof params['per_page'] !== 'undefined') { + url += '&per_page=' + params['per_page']; + } + // add sort if exists if (typeof params['sort'] !== 'undefined') { url += '&sort=' + params['sort']; @@ -86,6 +90,7 @@ function GithubSearcher(options) { url: '', headers: { 'User-Agent': this.auth.username, + 'Accept': 'application/vnd.github.v3.text-match+json', // add authorization to header so that you can send authenticated headers 'Authorization': 'Basic ' + new Buffer(this.auth.username + ':' + this.auth.password).toString('base64') } @@ -101,7 +106,7 @@ function GithubSearcher(options) { } // method to search for users -GithubSearcher.prototype.searchUsers = function(params, callback) { +GithubSearcher.prototype.searchUsers = function (params, callback) { // throw error if the callback is not a function if (typeof callback !== 'function') { @@ -121,7 +126,7 @@ GithubSearcher.prototype.searchUsers = function(params, callback) { if (typeof params === 'string') { url += params; - // if an object, then create the url string by iterating through the object + // if an object, then create the url string by iterating through the object } else if (typeof params === 'object') { // add the term part first, since it is added differently @@ -134,8 +139,8 @@ GithubSearcher.prototype.searchUsers = function(params, callback) { if (k === 'repos' || k === 'followers') { // repos and followers allows user to pass in comparison operators >, <, = url += addFirstPlus(url) + k + ':' + convertSign(params[k][0]) + params[k].substring(1, params[k].length); - } else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') { - // term and page are handled separately/ differently + } else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order' && k !== 'per_page') { + // term and page are handled separately/ differently url += addFirstPlus(url) + k + ':' + params[k]; } } @@ -151,7 +156,7 @@ GithubSearcher.prototype.searchUsers = function(params, callback) { this.options.url = url; // Use the request module to ping the url - request.get(this.options, function(error, response, body) { + request.get(this.options, function (error, response, body) { if (!error && response.statusCode === 200) { // pass our result to the callback callback(JSON.parse(body)); @@ -163,7 +168,7 @@ GithubSearcher.prototype.searchUsers = function(params, callback) { }; // method to query repositories -GithubSearcher.prototype.searchRepos = function(params, callback) { +GithubSearcher.prototype.searchRepos = function (params, callback) { // throw error if the callback is not a function if (typeof callback !== 'function') { throw new Error('Callback is not a function!'); @@ -191,9 +196,9 @@ GithubSearcher.prototype.searchRepos = function(params, callback) { // forks, stars and size allows user to pass in comparison operators >, <, = // convert first element in string, and then concatenate the remainder of the string url += addFirstPlus(url) + k + ':' + convertSign(params[k][0]) + params[k].substring(1, params[k].length); - } else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') { - // term, page, sort and order are handled separately/ differently - url += addFirstPlus(url) + k + ':' + params[k]; + } else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order' && k !== 'per_page') { + // term, page, sort and order are handled separately/ differently + url += addFirstPlus(url) + k + ':' + params[k]; } } @@ -208,7 +213,7 @@ GithubSearcher.prototype.searchRepos = function(params, callback) { this.options.url = url; // use request module to ping url - request.get(this.options, function(error, response, body) { + request.get(this.options, function (error, response, body) { if (!error && response.statusCode === 200) { // pass our result to the callback callback(JSON.parse(body)); @@ -219,7 +224,7 @@ GithubSearcher.prototype.searchRepos = function(params, callback) { return this; }; -GithubSearcher.prototype.searchCode = function(params, callback) { +GithubSearcher.prototype.searchCode = function (params, callback) { // throw error if the callback is not a function if (typeof callback !== 'function') { @@ -248,9 +253,9 @@ GithubSearcher.prototype.searchCode = function(params, callback) { // size allows user to pass in comparison operators >, <, = // convert first element in string, and then concatenate the remainder of the string url += addFirstPlus(url) + k + ':' + convertSign(params[k][0]) + params[k].substring(1, params[k].length); - } else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') { - // term, page, sort and order are handled separately/ differently - url += addFirstPlus(url) + k + ':' + params[k]; + } else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order' && k !== 'per_page') { + // term, page, sort and order are handled separately/ differently + url += addFirstPlus(url) + k + ':' + params[k]; } } @@ -266,7 +271,7 @@ GithubSearcher.prototype.searchCode = function(params, callback) { this.options.url = url; // use request module to ping url - request.get(this.options, function(error, response, body) { + request.get(this.options, function (error, response, body) { if (!error && response.statusCode === 200) { // pass our result to the callback callback(JSON.parse(body)); @@ -278,7 +283,7 @@ GithubSearcher.prototype.searchCode = function(params, callback) { }; -GithubSearcher.prototype.searchIssues = function(params, callback) { +GithubSearcher.prototype.searchIssues = function (params, callback) { // throw error if the callback is not a function if (typeof callback !== 'function') { @@ -305,7 +310,7 @@ GithubSearcher.prototype.searchIssues = function(params, callback) { for (var k in params) { // just add everything to the query // there are no cases where <, >, = are used - if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') { + if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order' && k !== 'per_page') { url += addFirstPlus(url) + k + ':' + params[k]; } } @@ -322,7 +327,7 @@ GithubSearcher.prototype.searchIssues = function(params, callback) { this.options.url = url; // use request module to ping url - request.get(this.options, function(error, response, body) { + request.get(this.options, function (error, response, body) { if (!error && response.statusCode === 200) { // pass our result to the callback callback(JSON.parse(body)); @@ -333,4 +338,4 @@ GithubSearcher.prototype.searchIssues = function(params, callback) { return this; }; -module.exports = GithubSearcher; \ No newline at end of file +module.exports = GithubSearcher;