-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathregistry.js
65 lines (56 loc) · 1.29 KB
/
registry.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
65
'use strict';
const fetch = require('node-fetch');
const utils = require('./utils.js');
const config = require('./config.js');
/**
* Registry constructor. Create an instance of a registry for querying registry.npmjs.org directly.
*
* ```js
* const registry = new Registry();
* ```
*
* @returns {Object} instance of `Registry`
* @name Registry
* @api public
*/
class Registry {
constructor() {
this.config = utils.clone(config);
}
/**
* Get the package.json for the specified repository.
*
* ```js
* let results = await registry.get('micromatch')
* ```
* @param {String} `name` Repository name to get.
* @return {Promise} Results of the query when promise is resolved.
* @name .get
* @api public
*/
async get(name) {
const response = await fetch(this.url(name));
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
}
/**
* Build a formatted url
*
* @param {String} `name` Repo name.
* @return {String} formatted url string
* @name .url
* @api public
*/
url(name) {
if (name[0] === '@' && name.indexOf('/') !== -1) {
name = '@' + encodeURIComponent(name.slice(1));
}
return this.config.registry + name;
}
}
/**
* Exposes `Registry`
*/
module.exports = Registry;