Files
shields/services/bundlephobia/bundlephobia.service.js
chris48s b71f812b68 migrate examples to openApi part 17; affects [buildkite cii codeship crates jsdelivr npms] and bundlephobia (#9584)
* update crates service test

* migrate some services from examples to openApi

* migrate crates from examples to openApi, improve titles

* explain what hd,hw,hm,hy actually mean

* improve descriptions
2023-12-22 13:25:38 +00:00

142 lines
3.6 KiB
JavaScript

import Joi from 'joi'
import prettyBytes from 'pretty-bytes'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService, pathParams } from '../index.js'
const schema = Joi.object({
size: nonNegativeInteger,
gzip: nonNegativeInteger,
}).required()
const description =
'[Bundlephobia](https://bundlephobia.com) lets you understand the size of a javascript package from NPM before it becomes a part of your bundle.'
export default class Bundlephobia extends BaseJsonService {
static category = 'size'
static route = {
base: 'bundlephobia',
pattern: ':format(min|minzip)/:scope(@[^/]+)?/:packageName/:version?',
}
static openApi = {
'/bundlephobia/{format}/{packageName}': {
get: {
summary: 'npm bundle size',
description,
parameters: pathParams(
{
name: 'format',
schema: { type: 'string', enum: this.getEnum('format') },
example: 'min',
},
{
name: 'packageName',
example: 'react',
},
),
},
},
'/bundlephobia/{format}/{scope}/{packageName}': {
get: {
summary: 'npm bundle size (scoped)',
description,
parameters: pathParams(
{
name: 'format',
schema: { type: 'string', enum: this.getEnum('format') },
example: 'min',
},
{
name: 'scope',
example: '@cycle',
},
{
name: 'packageName',
example: 'core',
},
),
},
},
'/bundlephobia/{format}/{packageName}/{version}': {
get: {
summary: 'npm bundle size (version)',
description,
parameters: pathParams(
{
name: 'format',
schema: { type: 'string', enum: this.getEnum('format') },
example: 'min',
},
{
name: 'packageName',
example: 'react',
},
{
name: 'version',
example: '15.0.0',
},
),
},
},
'/bundlephobia/{format}/{scope}/{packageName}/{version}': {
get: {
summary: 'npm bundle size (scoped version)',
description,
parameters: pathParams(
{
name: 'format',
schema: { type: 'string', enum: this.getEnum('format') },
example: 'min',
},
{
name: 'scope',
example: '@cycle',
},
{
name: 'packageName',
example: 'core',
},
{
name: 'version',
example: '7.0.0',
},
),
},
},
}
static _cacheLength = 900
static defaultBadgeData = { label: 'bundlephobia', color: 'informational' }
static render({ format, size }) {
const label = format === 'min' ? 'minified size' : 'minzipped size'
return {
label,
message: prettyBytes(size),
}
}
async fetch({ scope, packageName, version }) {
const packageQuery = `${scope ? `${scope}/` : ''}${packageName}${
version ? `@${version}` : ''
}`
const options = { searchParams: { package: packageQuery } }
return this._requestJson({
schema,
url: 'https://bundlephobia.com/api/size',
options,
httpErrors: {
404: 'package or version not found',
},
})
}
async handle({ format, scope, packageName, version }) {
const json = await this.fetch({ scope, packageName, version })
const size = format === 'min' ? json.size : json.gzip
return this.constructor.render({ format, size })
}
}