-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.mjs
103 lines (94 loc) · 3.75 KB
/
Search.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @file
* @copyright 2013 Michael Aufreiter (Development Seed) and 2016 Yahoo Inc.
* @license Licensed under {@link https://spdx.org/licenses/BSD-3-Clause-Clear.html BSD-3-Clause-Clear}.
* Github.js is freely distributable.
*/
import Requestable from './Requestable.mjs';
// simplest `console.debug` substitute of "debug" package dependency
import debug from './dependencies/debug.mjs';
const log = debug('github:search');
/**
* Wrap the Search API
*/
class Search extends Requestable {
/**
* Create a Search
* @param {Object} defaults - defaults for the search
* @param {Requestable.auth} [auth] - information required to authenticate to Github
* @param {string} [apiBase=https://api.github.com] - the base Github API URL
*/
constructor(defaults, auth, apiBase) {
super(auth, apiBase);
this.__defaults = this._getOptionsWithDefaults(defaults);
}
/**
* Available search options
* @see https://developer.github.com/v3/search/#parameters
* @typedef {Object} Search.Params
* @param {string} q - the query to make
* @param {string} sort - the sort field, one of `stars`, `forks`, or `updated`.
* Default is [best match](https://developer.github.com/v3/search/#ranking-search-results)
* @param {string} order - the ordering, either `asc` or `desc`
*/
/**
* Perform a search on the GitHub API
* @private
* @param {string} path - the scope of the search
* @param {Search.Params} [withOptions] - additional parameters for the search
* @param {Requestable.callback} [cb] - will receive the results of the search
* @return {Promise} - the promise for the http request
*/
_search(path, withOptions = {}, cb = undefined) {
let requestOptions = {};
Object.keys(this.__defaults).forEach((prop) => {
requestOptions[prop] = this.__defaults[prop];
});
Object.keys(withOptions).forEach((prop) => {
requestOptions[prop] = withOptions[prop];
});
log(`searching ${path} with options:`, requestOptions);
return this._requestAllPages(`/search/${path}`, requestOptions, cb);
}
/**
* Search for repositories
* @see https://developer.github.com/v3/search/#search-repositories
* @param {Search.Params} [options] - additional parameters for the search
* @param {Requestable.callback} [cb] - will receive the results of the search
* @return {Promise} - the promise for the http request
*/
forRepositories(options, cb) {
return this._search('repositories', options, cb);
}
/**
* Search for code
* @see https://developer.github.com/v3/search/#search-code
* @param {Search.Params} [options] - additional parameters for the search
* @param {Requestable.callback} [cb] - will receive the results of the search
* @return {Promise} - the promise for the http request
*/
forCode(options, cb) {
return this._search('code', options, cb);
}
/**
* Search for issues
* @see https://developer.github.com/v3/search/#search-issues
* @param {Search.Params} [options] - additional parameters for the search
* @param {Requestable.callback} [cb] - will receive the results of the search
* @return {Promise} - the promise for the http request
*/
forIssues(options, cb) {
return this._search('issues', options, cb);
}
/**
* Search for users
* @see https://developer.github.com/v3/search/#search-users
* @param {Search.Params} [options] - additional parameters for the search
* @param {Requestable.callback} [cb] - will receive the results of the search
* @return {Promise} - the promise for the http request
*/
forUsers(options, cb) {
return this._search('users', options, cb);
}
}
export default Search;