-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (39 loc) · 910 Bytes
/
index.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
'use strict';
var got = require('got');
var registryUrl = require('registry-url');
function get(url, cb) {
got(url, {json: true}, function (err, data) {
if (err && err.code === 404) {
cb(new Error('Package or version doesn\'t exist'));
return;
}
if (err) {
cb(err);
return;
}
cb(null, data);
});
}
module.exports = function (name, version, cb) {
var url = registryUrl(name.split('/')[0]) + name + '/';
if (typeof version !== 'string') {
cb = version;
version = '';
}
get(url + version, cb);
};
module.exports.field = function (name, field, cb) {
var url = registryUrl(name.split('/')[0]) +
'-/by-field/?key=%22' + name + '%22&field=' + field;
get(url, function (err, res) {
if (err) {
cb(err);
return;
}
if (Object.keys(res).length === 0) {
cb(new Error('Field `' + field + '` doesn\'t exist'));
return;
}
cb(null, res[name][field]);
});
};