Better icon resolver (fix badgen/badgen#54)

pull/287/head
Amio 2019-06-12 09:42:07 +08:00
rodzic fabab708fc
commit 74705c5669
1 zmienionych plików z 24 dodań i 7 usunięć

Wyświetl plik

@ -15,19 +15,15 @@ export default function (req, res, options: ServeBadgeOptions) {
const { subject, status, color } = params
const { label, list, icon, iconWidth } = query
const _icon = icons[icon === '' ? subject : icon] || {
base64: icon,
width: iconWidth
}
const _icon = resolveIcon(icon === '' ? subject : icon, iconWidth)
const badge = badgen({
subject: typeof label !== 'undefined' ? label : subject,
status: list ? String(status).replace(/,/g, ' | ') : String(status),
color: query.color || color,
style: query.style || process.env.BADGE_STYLE,
icon: _icon.base64,
iconWidth: _icon.width
icon: _icon.src,
iconWidth: iconWidth || _icon.width
})
const staleControl = `stale-while-revalidate=604800, stale-if-error=604800`
@ -37,3 +33,24 @@ export default function (req, res, options: ServeBadgeOptions) {
res.statusCode = code
res.end(badge)
}
type ResolvedIcon = {
src?: string
width?: string
}
function resolveIcon (icon: string, width: string): ResolvedIcon {
const builtinIcon = icons[icon]
if (builtinIcon) {
return {
src: builtinIcon.base64,
width: width || builtinIcon.width
}
}
if (icon.startsWith('https://')) {
return { src: icon, width }
}
return {}
}