kopia lustrzana https://github.com/badgen/badgen.net
badge(nuget): Add total downloads support (#536)
rodzic
8e23422a02
commit
0ba32760e5
32
api/nuget.ts
32
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<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'
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue