badge(nuget): Add total downloads support (#536)

pull/542/head
Tatsuro Shibamura 2021-11-10 17:28:37 +09:00 zatwierdzone przez GitHub
rodzic 8e23422a02
commit 0ba32760e5
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 25 dodań i 7 usunięć

Wyświetl plik

@ -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<any>()
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<any>()
return {
subject: 'downloads',
status: millify(data[0].totalDownloads),
color: 'green'
}
}