-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPackageNames.js
64 lines (50 loc) · 1.3 KB
/
getPackageNames.js
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
/**
* SEE
* https://api-docs.npms.io
*/
'use strict';
const MODULE_REQUIRE = 1
/* built-in */
/* NPM */
, htp = require('htp')
, co = require('jinang/co')
, modifyUrl = require('jinang/modifyUrl')
, noda = require('noda')
/* in-package */
, config = noda.inRequire('lib/config')
/* in-file */
, endPoint = 'https://api.npms.io/v2/search'
;
/**
* @param {Object} options
* @param {string} options.author
* @param {string} options.owner
*/
function getPackageNames(options) { return co.easy(function*() {
let Q = [];
options = options ? options : {};
if (options.author) {
Q.push([ 'author', options.author ]);
}
if (options.owner) {
Q.push([ 'maintainer', options.owner ]);
}
if (Q.length == 0) {
throw new Error('at lease one condition should be appointed');
}
let q = Q.map(kv => `${kv[0]}:${kv[1]}`).join('+');
// 250 is the max value of `size`.
let names = [], from = 0, size = 25;
do {
let urlname = modifyUrl.query(endPoint, { q, from: names.length, size }, 'w');
let body = (yield htp.get(urlname)).body;
// Extract package names.
body.results.forEach(ret => names.push(ret.package.name));
// When all packages returned.
if (names.length == body.total) {
break;
}
} while(true)
return names;
}) };
module.exports = getPackageNames;