badge: add haxelib service (#381)

* Add haxelib service

Fetch library information from https://lib.haxe.org using
`haxe-rpc-client`.

* Add latest version downloads badge

* Handle nonexistent projects gracefully
pull/383/head
Dario Vladović 2020-05-08 17:30:53 +02:00 zatwierdzone przez GitHub
rodzic 57ad8a53af
commit 55a89ffcea
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 85 dodań i 0 usunięć

78
api/haxelib.ts 100644
Wyświetl plik

@ -0,0 +1,78 @@
import RpcClient from 'haxe-rpc-client'
import { millify, version, versionColor } from '../libs/utils'
import { createBadgenHandler, PathArgs, BadgenError } from '../libs/create-badgen-handler'
const HAXELIB_RPC_URL = 'https://lib.haxe.org/api/3.0/index.n'
class HaxelibClient extends RpcClient {
constructor() {
super(HAXELIB_RPC_URL)
}
async info(project) {
try {
return await this.call<any>('api.infos', [project])
} catch (err) {
if (err.message.startsWith('No such Project')) {
throw new BadgenError({ status: 404 })
}
throw err
}
}
}
export default createBadgenHandler({
title: 'haxelib',
examples: {
'/haxelib/v/tink_http': 'version',
'/haxelib/v/nme': 'version',
'/haxelib/d/hxnodejs': 'downloads',
'/haxelib/dl/hxnodejs': 'downloads (latest version)',
'/haxelib/license/openfl': 'license'
},
handlers: {
'/haxelib/:topic/:project': handler
}
})
async function handler ({ topic, project }: PathArgs) {
const client = new HaxelibClient()
const {
downloads,
license,
curversion: ver,
versions
} = await client.info(project)
switch (topic) {
case 'v': {
return {
subject: 'haxelib',
status: version(ver),
color: versionColor(ver)
}
}
case 'license': {
return {
subject: 'license',
status: license || 'unknown',
color: 'blue'
}
}
case 'd': {
return {
subject: 'downloads',
status: millify(downloads),
color: 'green'
}
}
case 'dl': {
const latestVersion = versions.find(it => it.name === ver)
return {
subject: 'downloads',
status: millify(latestVersion.downloads) + ' latest version',
color: 'green'
}
}
}
}

Wyświetl plik

@ -26,6 +26,7 @@ export const liveBadgeList = [
'vs-marketplace',
'maven',
'cocoapods',
'haxelib',
// CI
'travis',
'circleci',

5
package-lock.json wygenerowano
Wyświetl plik

@ -5402,6 +5402,11 @@
"minimalistic-assert": "^1.0.1"
}
},
"haxe-rpc-client": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/haxe-rpc-client/-/haxe-rpc-client-1.0.0.tgz",
"integrity": "sha512-AffqK18IUrPB1NL9GK6N1NvOFoeTTJxwXSscO/DydzeOyJ2qbZC17mZNTLlWHEEmfnbRAEjvHT48OiXGO1GAAA=="
},
"hex-color-regex": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/hex-color-regex/-/hex-color-regex-1.1.0.tgz",

Wyświetl plik

@ -31,6 +31,7 @@
"chrome-webstore": "^1.3.1",
"date-fns": "^2.12.0",
"got": "^10.7.0",
"haxe-rpc-client": "^1.0.0",
"lodash.debounce": "^4.0.8",
"measurement-protocol": "^0.1.1",
"micro": "^9.3.4",