2018-06-29 16:52:17 +00:00
|
|
|
const badgen = require('badgen')
|
2018-07-27 03:31:21 +00:00
|
|
|
const { send } = require('micro')
|
2018-07-28 09:02:43 +00:00
|
|
|
const { builtin } = require('./icons.js')
|
2018-06-29 16:52:17 +00:00
|
|
|
|
2018-07-22 13:00:51 +00:00
|
|
|
module.exports = function serveBadge (req, res, options = {}) {
|
|
|
|
const { code = 200, maxAge = '86400' } = options
|
2018-07-17 07:03:38 +00:00
|
|
|
|
2018-07-22 13:00:51 +00:00
|
|
|
const hostStyle = req.headers.host === 'flat.badgen.net' ? 'flat' : undefined
|
|
|
|
const { subject, status, color } = req.params
|
2018-07-25 06:58:32 +00:00
|
|
|
const { style, label, emoji, list, icon } = req.query
|
2018-07-22 13:00:51 +00:00
|
|
|
|
|
|
|
const badgenParams = {
|
2018-07-28 15:02:40 +00:00
|
|
|
subject: typeof label !== 'undefined' ? label : subject,
|
2018-07-25 05:34:22 +00:00
|
|
|
status: String(list ? status.replace(/,/g, ' | ') : status),
|
2018-07-22 13:00:51 +00:00
|
|
|
color: color,
|
|
|
|
style: style || hostStyle,
|
2018-07-25 06:58:32 +00:00
|
|
|
emoji: Boolean(emoji),
|
2018-07-28 09:02:43 +00:00
|
|
|
icon: builtin[icon]
|
2018-07-22 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 05:34:22 +00:00
|
|
|
res.setHeader('Content-Type', 'image/svg+xml;charset=utf-8')
|
|
|
|
res.setHeader('Cache-Control', `public, max-age=60, s-maxage=${maxAge}`)
|
|
|
|
send(res, code, badgen(badgenParams))
|
2018-06-29 16:52:17 +00:00
|
|
|
}
|