-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
crates.ts
47 lines (44 loc) · 1.19 KB
/
crates.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import got from '../../libs/got'
import { millify, version, versionColor } from '../../libs/utils'
import { createBadgenHandler, PathArgs } from '../../libs/create-badgen-handler-next'
export default createBadgenHandler({
title: 'Rust Crates',
examples: {
'/crates/v/regex': 'version',
'/crates/d/regex': 'downloads',
'/crates/dl/regex': 'downloads (latest version)',
},
handlers: {
'/crates/:topic<v|d|dl>/:pkg': handler
}
})
async function handler ({topic, pkg}: PathArgs) {
const endpoint = `https://crates.io/api/v1/crates/${pkg}`
const { crate } = await got(endpoint).json<any>()
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.io',
status: 'unknown topic',
color: 'grey'
}
}
}