From 0ba32760e57129042db85a6999f143f9d7c8e7b6 Mon Sep 17 00:00:00 2001 From: Tatsuro Shibamura Date: Wed, 10 Nov 2021 17:28:37 +0900 Subject: [PATCH] badge(nuget): Add total downloads support (#536) --- api/nuget.ts | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/api/nuget.ts b/api/nuget.ts index ef6c0b8..903b5b6 100644 --- a/api/nuget.ts +++ b/api/nuget.ts @@ -1,16 +1,18 @@ import got from '../libs/got' -import { version, versionColor } from '../libs/utils' +import { millify, version, versionColor } from '../libs/utils' import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler' export default createBadgenHandler({ - title: 'Nuget', + title: 'NuGet', examples: { - '/nuget/v/newtonsoft.json': 'version (stable channel)', - '/nuget/v/newtonsoft.json/pre': 'version (pre channel)', - '/nuget/v/newtonsoft.json/latest': 'version (latest channel)', + '/nuget/v/Newtonsoft.Json': 'version (stable channel)', + '/nuget/v/Newtonsoft.Json/pre': 'version (pre channel)', + '/nuget/v/Newtonsoft.Json/latest': 'version (latest channel)', + '/nuget/dt/Newtonsoft.Json': 'total downloads', }, handlers: { - '/nuget/v/:project/:channel?': handler + '/nuget/v/:project/:channel?': handler, + '/nuget/dt/:project': downloads } }) @@ -19,7 +21,7 @@ const stable = versions => versions.filter(v => !v.includes('-')) const latest = versions => versions.length > 0 && versions.slice(-1)[0] async function handler ({ project, channel }: PathArgs) { - const endpoint = `https://api.nuget.org/v3-flatcontainer/${project}/index.json` + const endpoint = `https://api.nuget.org/v3-flatcontainer/${project.toLowerCase()}/index.json` const { versions } = await got(endpoint).json() let ver = '' @@ -45,3 +47,19 @@ async function handler ({ project, channel }: PathArgs) { color: versionColor(ver) } } + +async function downloads ({ project }: PathArgs) { + const endpoint = `https://azuresearch-usnc.nuget.org/query` + const searchParams = { + q: `packageid:${project.toLowerCase()}`, + prerelease: true, + semVerLevel: 2 + } + const { data } = await got.get(endpoint, { searchParams }).json() + + return { + subject: 'downloads', + status: millify(data[0].totalDownloads), + color: 'green' + } +}