badgen.net/api/docker.ts

46 wiersze
1.2 KiB
TypeScript
Czysty Zwykły widok Historia

2019-05-31 06:35:44 +00:00
import millify from 'millify'
import got from '../libs/got'
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
2019-05-31 06:35:44 +00:00
export default createBadgenHandler({
2019-05-31 06:35:44 +00:00
title: 'Docker',
examples: {
'/docker/pulls/library/ubuntu': 'pulls (library)',
'/docker/stars/library/ubuntu': 'stars (library)',
'/docker/pulls/amio/node-chrome': 'pulls (scoped)',
'/docker/stars/library/mongo?icon=docker&label=stars': 'stars (icon & label)',
},
handlers: {
'/docker/:topic<stars|pulls>/:scope/:name': handler
2019-05-31 06:35:44 +00:00
}
})
2019-05-31 06:35:44 +00:00
async function handler ({ topic, scope, name }: PathArgs) {
2019-05-31 06:35:44 +00:00
if (!['stars', 'pulls'].includes(topic)) {
return {
subject: 'docker',
status: 'unknown topic',
color: 'grey'
}
}
/* eslint-disable camelcase */
const endpoint = `https://hub.docker.com/v2/repositories/${scope}/${name}`
const { pull_count, star_count } = await got(endpoint).then(res => res.body)
switch (topic) {
case 'stars':
return {
subject: 'docker stars',
status: millify(star_count),
color: 'blue'
}
case 'pulls':
return {
subject: 'docker pulls',
status: millify(pull_count),
color: 'blue'
}
}
}