From 90f474b0a976aaa4d5b00db3d7f927e1b75606db Mon Sep 17 00:00:00 2001 From: Amio Date: Fri, 31 May 2019 14:35:44 +0800 Subject: [PATCH] Migrate docker badge to v2 --- endpoints/docker.ts | 53 +++++++++++++++++++++++++++++++++++++++++++++ libs/examples.ts | 3 ++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 endpoints/docker.ts diff --git a/endpoints/docker.ts b/endpoints/docker.ts new file mode 100644 index 0000000..c329e7e --- /dev/null +++ b/endpoints/docker.ts @@ -0,0 +1,53 @@ +import millify from 'millify' +import got from '../libs/got' +import { + badgenServe, + BadgenServeMeta as Meta, + BadgenServeHandlers as Handlers, + BadgenServeHandlerArgs as Args +} from '../libs/badgen-serve' + +export const meta: Meta = { + 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)', + } +} + +export const handlers: Handlers = { + '/docker/:topic/:scope/:name': handler +} + +export default badgenServe(handlers) + +async function handler ({ topic, scope, name }: Args) { + 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' + } + } +} diff --git a/libs/examples.ts b/libs/examples.ts index a723ff4..55c8aee 100644 --- a/libs/examples.ts +++ b/libs/examples.ts @@ -13,9 +13,10 @@ const liveBadgeExampleList = [ 'packagephobia', 'bundlephobia', 'crates', + 'docker', + 'homebrew', 'amo', 'apm', - 'homebrew', // CI 'appveyor', // quality & metrics