From a37cceb2e53221b4567635eff91c8a8cebe2b3e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Vladovi=C4=87?= Date: Wed, 16 Dec 2020 13:22:02 +0100 Subject: [PATCH] badge: Add CTAN service (#455) --- api/ctan.ts | 41 +++++++++++++++++++++++++++++++++++++++++ libs/badge-list.ts | 1 + 2 files changed, 42 insertions(+) create mode 100644 api/ctan.ts diff --git a/api/ctan.ts b/api/ctan.ts new file mode 100644 index 0000000..2f6f06e --- /dev/null +++ b/api/ctan.ts @@ -0,0 +1,41 @@ +import got from '../libs/got' +import { version, versionColor } from '../libs/utils' +import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler' + +const CTAN_API_URL = 'https://ctan.org/json/2.0/' + +const client = got.extend({ prefixUrl: CTAN_API_URL, timeout: 3500 }) + +export default createBadgenHandler({ + title: 'CTAN', + examples: { + '/ctan/v/latexindent': 'version', + '/ctan/license/latexdiff': 'license' + }, + handlers: { + '/ctan/:topic/:pkg': handler, + } +}) + +async function handler ({ topic, pkg }: PathArgs) { + const { + license, + version: versionInfo, + } = await client.get(`pkg/${pkg}`).json() + const { number: ver } = versionInfo; + + switch (topic) { + case 'v': + return { + subject: 'ctan', + status: version(ver), + color: versionColor(ver) + } + case 'license': + return { + subject: 'license', + status: license || 'unknown', + color: 'green' + } + } +} diff --git a/libs/badge-list.ts b/libs/badge-list.ts index 08eebe6..f5613f6 100644 --- a/libs/badge-list.ts +++ b/libs/badge-list.ts @@ -29,6 +29,7 @@ export const liveBadgeList = [ 'cocoapods', 'haxelib', 'opam', + 'ctan', 'scoop', 'winget', 'f-droid',