badgen.net/endpoints/amo.ts

70 wiersze
1.7 KiB
TypeScript
Czysty Zwykły widok Historia

2019-05-13 12:00:56 +00:00
import got from '../libs/got'
import { millify, stars, version, versionColor } from '../libs/utils'
2019-05-26 04:38:30 +00:00
import {
badgenServe,
BadgenServeMeta as Meta,
BadgenServeHandlers as Handlers,
BadgenServeHandlerArgs as Args
} from '../libs/badgen-serve'
2019-05-13 03:04:00 +00:00
2019-05-26 04:38:30 +00:00
export const meta: Meta = {
title: 'Mozilla Add-on',
examples: {
'/amo/v/markdown-viewer-chrome': 'version',
'/amo/users/markdown-viewer-chrome': 'users',
'/amo/rating/markdown-viewer-chrome': 'rating',
'/amo/stars/markdown-viewer-chrome': 'stars',
'/amo/reviews/markdown-viewer-chrome': 'reviews',
}
}
2019-05-13 03:04:00 +00:00
2019-05-26 04:38:30 +00:00
export const handlers: Handlers = {
2019-05-13 08:39:17 +00:00
'/amo/:topic/:name': handler
2019-05-13 03:04:00 +00:00
}
2019-05-26 04:38:30 +00:00
async function handler ({ topic, name }: Args) {
2019-05-13 03:04:00 +00:00
const endpoint = `https://addons.mozilla.org/api/v3/addons/addon/${name}/`
const addon = await got(endpoint).then(res => res.body)
switch (topic) {
case 'v':
return {
subject: 'mozilla add-on',
2019-05-13 12:00:56 +00:00
status: version(addon.current_version.version),
color: versionColor(addon.current_version.version)
2019-05-13 03:04:00 +00:00
}
case 'users':
return {
subject: 'users',
status: millify(parseInt(addon.average_daily_users)),
color: 'green'
}
case 'rating':
return {
subject: 'rating',
status: `${Number(addon.ratings.average).toFixed(2)}/5`,
color: 'green'
}
case 'stars':
return {
subject: 'stars',
status: stars(addon.ratings.average),
color: 'green'
}
case 'reviews':
return {
subject: 'reviews',
status: addon.ratings.count,
color: 'green'
}
default:
return {
subject: 'mozilla add-on',
status: 'unknown',
color: 'grey'
}
}
}
2019-05-14 01:15:16 +00:00
export default badgenServe(handlers)