npm-count
Version:
a npm/download-counts wrapper
100 lines (76 loc) • 2.91 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: fetchBulkQuery.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: fetchBulkQuery.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>// dependencies
import request from 'request';
import Promise from 'bluebird';
import immutable from 'immutable';
// private
const requestAsync = Promise.promisify(request);
const api = 'https://api.npmjs.org/downloads';
/**
* fetch the stats via https://github.com/npm/download-counts
*
* @module fetchBulkQuery
* @param {(string|array<string>)} names - package names
* @param {object} [options]
* @param {string} [options.type='point'] - api type
* @param {string} [options.period='last-day'] - stats period
* @param {string} [options.requestOptions={json:true,gzip:true}] - pass to `request`
* @returns {promise<object>} always key-value stats (e.g: {name: stats, ...})
* @see https://github.com/npm/download-counts#readme
*/
export default (names, options = {}) => {
const opts = immutable.fromJS({
type: 'point',
period: 'last-day',
requestOptions: {
json: true,
gzip: true,
},
}).mergeDeep(options).toJS();
const params = typeof names === 'string' ? [names] : names;
const url = `${api}/${opts.type}/${opts.period}/${encodeURIComponent(params.join(','))}`;
return requestAsync(url, opts.requestOptions)
.then(({ body }) => {
if (body.error) {
return {};// e.g. {"error":"reason description"}
}
// transform key-value stats
// パッケージ名をカンマで区切るとjsonの書式が変わるので、カンマ区切りの時の書式に合わせる
if (params.length === 1) {
return { [body.package]: body };
}
return body;
});
};
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-fetchBulkQuery.html">fetchBulkQuery</a></li><li><a href="module-fetchLastDay.html">fetchLastDay</a></li><li><a href="module-fetchPublications.html">fetchPublications</a></li><li><a href="module-fetchTrending.html">fetchTrending</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Mar 10 2016 02:45:20 GMT+0000 (UTC)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>