diff --git a/libs/examples-live.js b/libs/examples-live.js index c551304..7177998 100644 --- a/libs/examples-live.js +++ b/libs/examples-live.js @@ -212,9 +212,10 @@ module.exports = { ['arbitrary url', '/badgesize/normal/https://unpkg.com/snarkdown/dist/snarkdown.js'] ], jsdelivr: [ - ['hits (per month)', '/jsdelivr/stats/gh/jquery/jquery'], - ['hits (per month)', '/jsdelivr/stats/npm/lodash'], - ['rank', '/jsdelivr/rank/npm/lodash'] + ['hits (per month)', '/jsdelivr/hits/gh/jquery/jquery'], + ['hits (per month)', '/jsdelivr/hits/npm/lodash'], + ['rank', '/jsdelivr/rank/npm/lodash'], + ['version', '/jsdelivr/v/npm/lodash'] ], /* utilities */ 'opencollective': [ diff --git a/libs/index.md b/libs/index.md index dd2be98..0d7f1ef 100644 --- a/libs/index.md +++ b/libs/index.md @@ -277,9 +277,10 @@ Advanced usage (for badge makers): ['arbitrary url', '/badgesize/normal/https://unpkg.com/snarkdown/dist/snarkdown.js'] ], jsdelivr: [ - ['hits (per month)', '/jsdelivr/stats/gh/jquery/jquery'], - ['hits (per month)', '/jsdelivr/stats/npm/lodash'], + ['hits (per month)', '/jsdelivr/hits/gh/jquery/jquery'], + ['hits (per month)', '/jsdelivr/hits/npm/lodash'], ['rank', '/jsdelivr/rank/npm/lodash'], + ['version', '/jsdelivr/v/npm/lodash'], ], /* utilities */ 'opencollective': [ diff --git a/libs/live-fns/jsdelivr.js b/libs/live-fns/jsdelivr.js index 7b6aa73..dde3ffb 100644 --- a/libs/live-fns/jsdelivr.js +++ b/libs/live-fns/jsdelivr.js @@ -1,8 +1,9 @@ const got = require('../got.js') const millify = require('millify') +const semColor = require('../utils/sem-color.js') module.exports = async (topic, type, ...name) => { - if (!['stats', 'rank'].includes(topic)) { + if (!['hits', 'rank', 'v'].includes(topic)) { return { subject: 'jsDelivr', status: 'unknown topic', @@ -11,11 +12,23 @@ module.exports = async (topic, type, ...name) => { } const pkg = name.join('/') - const endpoint = `https://data.jsdelivr.com/v1/package/${type}/${pkg}/stats` - const { total, rank } = await got(endpoint).then(res => res.body) switch (topic) { - case 'stats': + case 'hits': + return stats('hits', type, pkg) + case 'rank': + return stats('rank', type, pkg) + case 'v': + return version(pkg) + } +} + +const stats = async (metric, type, name) => { + const endpoint = `https://data.jsdelivr.com/v1/package/${type}/${name}/stats` + const { total, rank } = await got(endpoint).then(res => res.body) + + switch (metric) { + case 'hits': return { subject: 'jsDelivr', status: `${millify(total)}/month`, @@ -24,8 +37,18 @@ module.exports = async (topic, type, ...name) => { case 'rank': return { subject: 'jsDelivr rank', - status: rank || 'none', + status: rank ? `#${rank}` : 'none', color: rank ? 'blue' : 'grey' } } } + +const version = async (name) => { + const endpoint = `https://cdn.jsdelivr.net/npm/${name}/package.json` + const { version } = await got(endpoint).then(res => res.body) + return { + subject: 'jsDelivr', + status: `v${version}`, + color: semColor(version) + } +}