badgen.net/libs/fetch-icon.ts

13 wiersze
363 B
TypeScript

2019-06-20 03:33:53 +00:00
import got from 'got'
export default async function (iconUrl: string) {
2019-06-20 03:33:53 +00:00
return got(iconUrl).then(res => {
const type = res.headers['content-type']
if (!type!.startsWith('image')) { return }
const base64 = Buffer.from(res.body).toString('base64')
const encoded = `data:${type};base64,${base64}`
return encoded
}).catch(() => undefined)
2019-06-20 03:33:53 +00:00
}