diff --git a/libs/index.md b/libs/index.md index c1f121e..9673e22 100644 --- a/libs/index.md +++ b/libs/index.md @@ -139,6 +139,17 @@ Available query params: ['version (pre)', '/nuget/v/newtonsoft.json/pre'], ['version (latest)', '/nuget/v/newtonsoft.json/latest'] ], + packagist: [ + ['version (stable)', '/packagist/v/monolog/monolog'], + ['version (pre)', '/packagist/v/monolog/monolog/pre'], + ['version (latest)', '/packagist/v/monolog/monolog/latest'], + ['total downloads', '/packagist/dt/monolog/monolog'], + ['daily downloads', '/packagist/dd/monolog/monolog'], + ['monthly downloads', '/packagist/dm/monolog/monolog'], + ['favers', '/packagist/favers/monolog/monolog'], + ['dependents', '/packagist/dependents/monolog/monolog'], + ['suggesters', '/packagist/suggesters/monolog/monolog'], + ], /* CIs */ travis: [ ['build', '/travis/babel/babel'], diff --git a/libs/live-fns/_index.js b/libs/live-fns/_index.js index 58f33b7..6531c7f 100644 --- a/libs/live-fns/_index.js +++ b/libs/live-fns/_index.js @@ -16,6 +16,7 @@ module.exports = { 'nuget': require('./nuget.js'), 'opencollective': require('./opencollective.js'), 'packagephobia': require('./packagephobia.js'), + 'packagist': require('./packagist.js'), 'pypi': require('./pypi.js'), 'runkit': require('./runkit.js'), 'travis': require('./travis.js'), diff --git a/libs/live-fns/packagist.js b/libs/live-fns/packagist.js new file mode 100644 index 0000000..2f07fcd --- /dev/null +++ b/libs/live-fns/packagist.js @@ -0,0 +1,81 @@ +const millify = require('millify') + +const axios = require('../axios.js') +const semColor = require('../utils/sem-color.js') + +const pre = versions => versions.filter(v => v.includes('-')) +const stable = versions => versions.filter(v => !v.includes('-')) +const latest = versions => versions.length > 0 && versions.slice(-1)[0] + +module.exports = async function (topic, vendor, pkg, channel = 'stable') { + const endpoint = `https://packagist.org/packages/${vendor}/${pkg}.json` + const response = await axios.get(endpoint).then(res => res.data) + + switch (topic) { + case 'v': + const versions = Object.keys(response.package.versions) + + let version = '' + + switch (channel) { + case 'latest': + version = latest(versions) + break + case 'pre': + version = latest(pre(versions)) + break + default: + version = latest(stable(versions).reverse()) + } + + version = version || latest(versions) + + return { + subject: 'packagist', + status: version || 'unknown', + color: semColor(version) + } + case 'dt': + return { + subject: 'downloads', + status: millify(response.package.downloads.total), + color: 'green' + } + case 'dd': + return { + subject: 'downloads', + status: millify(response.package.downloads.daily), + color: 'green' + } + case 'dm': + return { + subject: 'downloads', + status: millify(response.package.downloads.monthly), + color: 'green' + } + case 'favers': + return { + subject: 'favers', + status: millify(response.package.favers), + color: 'green' + } + case 'dependents': + return { + subject: 'dependents', + status: response.package.dependents, + color: 'green' + } + case 'suggesters': + return { + subject: 'suggesters', + status: response.package.suggesters, + color: 'green' + } + default: + return { + subject: 'packagist', + status: 'unknown', + color: 'grey' + } + } +}