Migrate crates badge to v2

pull/282/head
Amio 2019-05-31 14:26:16 +08:00
rodzic ad018077b7
commit a47f7671ac
2 zmienionych plików z 56 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,55 @@
import got from '../libs/got'
import { millify, version, versionColor } from '../libs/utils'
import {
badgenServe,
BadgenServeMeta as Meta,
BadgenServeHandlers as Handlers,
BadgenServeHandlerArgs as Args
} from '../libs/badgen-serve'
export const meta: Meta = {
title: 'Rust Crates',
examples: {
'/crates/v/regex': 'version',
'/crates/d/regex': 'downloads',
'/crates/dl/regex': 'downloads (latest version)',
}
}
export const handlers: Handlers = {
'/crates/:topic/:name': handler
}
export default badgenServe(handlers)
async function handler ({topic, pkg}: Args) {
const endpoint = `https://crates.io/api/v1/crates/${pkg}`
const { crate } = await got(endpoint).then(res => res.body)
switch (topic) {
case 'v':
return {
subject: 'crates.io',
status: version(crate.max_version),
color: versionColor(crate.max_version)
}
case 'd':
return {
subject: 'downloads',
status: millify(crate.downloads),
color: 'green'
}
case 'dl':
return {
subject: 'downloads',
status: millify(crate.recent_downloads) + ' latest version',
color: 'green'
}
default:
return {
subject: 'crates',
status: 'unknown topic',
color: 'grey'
}
}
}

Wyświetl plik

@ -12,6 +12,7 @@ const liveBadgeExampleList = [
'david',
'packagephobia',
'bundlephobia',
'crates',
'amo',
'apm',
'homebrew',