Browse Source

Migrate bundlephobia badge to v2

docker-size
Amio 5 years ago
parent
commit
7d1a59c455
  1. 53
      endpoints/bundlephobia.ts
  2. 1
      libs/examples.ts

53
endpoints/bundlephobia.ts

@ -0,0 +1,53 @@
import byteSize from 'byte-size'
import got from '../libs/got'
import {
badgenServe,
BadgenServeMeta as Meta,
BadgenServeHandlers as Handlers,
BadgenServeHandlerArgs as Args
} from '../libs/badgen-serve'
export const meta: Meta = {
title: 'Bundlephobia',
examples: {
'/bundlephobia/min/react': 'minified',
'/bundlephobia/minzip/react': 'minified + gzip',
'/bundlephobia/minzip/@material-ui/core': '(scoped pkg) minified + gzip',
}
}
export const handlers: Handlers = {
'/bundlephobia/:topic/:scope<@.*>/:name': handler,
'/bundlephobia/:topic/:name': handler,
}
export default badgenServe(handlers)
// https://github.com/pastelsky/bundlephobia/issues/4
async function handler ({ topic, scope, name }: Args) {
const pkg = scope ? `${scope}/${name}` : name
const endpoint = `https://bundlephobia.com/api/size?package=${pkg}`
const { size, gzip } = await got(endpoint).then(res => res.body)
switch (topic) {
case 'min':
return {
subject: 'minified size',
status: byteSize(size, { units: 'iec' }).toString().replace(/iB\b/, 'B'),
color: 'blue'
}
case 'minzip':
return {
subject: 'minzipped size',
status: byteSize(gzip, { units: 'iec' }).toString().replace(/iB\b/, 'B'),
color: 'blue'
}
default:
return {
subject: 'bundlephobia',
status: 'unknown topic',
color: 'grey'
}
}
}

1
libs/examples.ts

@ -11,6 +11,7 @@ const liveBadgeExampleList = [
'npm',
'david',
'packagephobia',
'bundlephobia',
'amo',
'apm',
'homebrew',

Loading…
Cancel
Save