badgen.net/libs/serve-badge.js

25 wiersze
825 B
JavaScript
Czysty Zwykły widok Historia

2018-06-29 16:52:17 +00:00
const badgen = require('badgen')
2018-07-27 03:31:21 +00:00
const { send } = require('micro')
const { builtin } = require('./icons.js')
2018-06-29 16:52:17 +00:00
module.exports = function serveBadge (req, res, options = {}) {
const { code = 200, maxAge = '86400' } = options
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
const badgenParams = {
2018-07-25 05:34:22 +00:00
subject: String(label || subject),
status: String(list ? status.replace(/,/g, ' | ') : status),
color: color,
style: style || hostStyle,
2018-07-25 06:58:32 +00:00
emoji: Boolean(emoji),
icon: builtin[icon]
}
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
}