npm downloads in jQuery

Note: This post was originally published in August 2015

After my Github repository showcase plugin I wanted to build something else just as simple. I wondered if I could display the amount of downloads for npm packages.

Luckily I found this interesting piece of documentation, it shows how the npmjs.org download count API works. That could easily be used in a jQuery plugin, simply by adapting my Github showcase plugin.

The result is jquery-npm-downloadcount. It works similarly to my other plugin. You can see a demo here.

It's available on Bower:

bower install jquery-npm-downloadcount  

You can then use it like so:

$(document).ready(function() {
    $('#my-downloadcount').npm_downloadcount({
        'package': 'request-ms',
        'templateUrl': 'templates/count.tpl',
        'renderer': Mustache.render,
        'store': sessionStorage
    });
});

Options

  • package: The package name to show the count for. If this is not set, defaults to all
  • template: The template to use for rendering (this or templateUrl is required)
  • templateUrl: The URL of the template to use for rendering (this or template is required)
  • cacheTime: The cache time in hours. Defaults to 24h. Disable caching by setting cacheTime to 0
  • point: The count point. Can be last-day, last-week or last-month (default).
  • renderer: The render method. Should take (template, scope). If no renderer is set, an error will be generated
  • store: The cache store to use. Should have setItem(key, value) and getItem(key). If no store is set, an error will be generated

If both template and templateUrl are set, templateUrl takes precedence.

point can also be a date range in the format YYYY-mm-dd:YYYY-mm-dd.

See you later!

comments powered by Disqus