Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added single page search.
  • Loading branch information
j-rewerts committed Oct 22, 2019
commit 8b54caf1964f2510a2e4ad3fa4c088960b318f15
38 changes: 38 additions & 0 deletions lib/Requestable.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,44 @@ class Requestable {
return response;
}).catch(callbackErrorOrThrow(cb, path));
}

/**
* Make a request and fetch a specific page. Use this to override the paging provided
* by {@link Requestable#requestAllPages}.
* @param {string} path - the path to request
* @param {string} perPage - the maximum number of results to return
* @param {string} page - the page number
* @param {Object} options - the query parameters to include. It doesn't matter if this includes 'per_page' or
* 'page'.
* @param {Requestable.callback} [cb] - the function to receive the data. The returned data will always be an array.
* @return {Promise} - a promise which will resolve the page has been fetched
*/
_requestPage(path, perPage, page, options, cb) {
options['page'] = page;
options['per_page'] = perPage;

let results = [];
return this._request('GET', path, options)
.then((response) => {
let thisGroup;
if (response.data instanceof Array) {
thisGroup = response.data;
} else if (response.data.items instanceof Array) {
thisGroup = response.data.items;
} else {
let message = `cannot figure out how to append ${response.data} to the result set`;
throw new ResponseError(message, path, response);
}
results.push(...thisGroup);

if (cb) {
cb(null, results, response);
}

response.data = results;
return response;
}).catch(callbackErrorOrThrow(cb, path));
}
}

module.exports = Requestable;
Expand Down
6 changes: 5 additions & 1 deletion lib/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ class Search extends Requestable {
});

log(`searching ${path} with options:`, requestOptions);
return this._requestAllPages(`/search/${path}`, requestOptions, cb);
if (requestOptions.page && requestOptions.per_page) {
return this._requestPage(`/search/${path}`, requestOptions.per_page, requestOptions.page, requestOptions, cb);
} else {
return this._requestAllPages(`/search/${path}`, requestOptions, cb);
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions test/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ describe('Search', function() {
});
});

it('should search repositories for specific page', function() {
let options;
let search = github.search({
q: 'tetris language:assembly',
sort: 'stars',
order: 'desc',
type: 'all',
per_page: '100',
page: '2',
});

return search.forRepositories(options)
.then(function({data}) {
expect(data).to.be.an.array();
expect(data.length).to.be.above(0);
expect(data.length).to.be.equal(100);
expect(data[0].name).to.be.equal('nand2tetris');
});
});

it('should search code', function() {
let options;
let search = github.search({
Expand Down