UNPKG

1.92 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Download download stats from the npm server.
5 *
6 * @constructor
7 * @param {Mana} api The actual API instance.
8 * @api private
9 */
10function Downloads(api) {
11 this.send = api.send.bind(api);
12 this.api = api;
13}
14
15/**
16 * Get the total amount of downloads for a given period. If no package name has
17 * been supplied the total of all packages will be returned.
18 *
19 * @param {String} period The period you want to select.
20 * @param {String} pkg Optional name of the package.
21 * @param {Function} fn Completion callback.
22 * @returns {Assign}
23 * @api public
24 */
25Downloads.prototype.totals = function point(period, pkg, fn) {
26 if ('function' === typeof pkg) {
27 fn = pkg;
28 pkg = null;
29 }
30
31 return this.send(['downloads', 'point', period, pkg], {
32 api: this.api.statservice
33 }, fn).map(function map(data) {
34 //
35 // There is this annoying edge-case in the npm downloads API where they do
36 // not return the correct information for newly published packages. These
37 // newly published packages are missing the `package` and `downloads` field.
38 // We've normalized to their sensible defaults so we can just ignore the
39 // fact that they are unable to correctly estimate the download count for
40 // a new module.
41 //
42 data.package = data.package || pkg;
43 data.downloads = data.downloads || 0;
44
45 return data;
46 });
47};
48
49/**
50 * Get the download statics for range of days.
51 *
52 * @param {String} period The period you want to select.
53 * @param {String} pkg Optional name of the package.
54 * @param {Function} fn Completion callback.
55 * @returns {Assign}
56 * @api public
57 */
58Downloads.prototype.range = function ranged(period, pkg, fn) {
59 if ('function' === typeof pkg) {
60 fn = pkg;
61 pkg = null;
62 }
63
64 return this.send(['downloads', 'range', period, pkg], {
65 api: this.api.statservice
66 }, fn);
67};
68
69//
70// Exposes the Downloads API.
71//
72module.exports = Downloads;