Files
shields/services/bundlephobia/bundlephobia.service.js
chris48s 14892e3943 Implement a pattern for dealing with upstream APIs which are slow on the first hit; affects [endpoint] (#9233)
* allow serviceData to override cacheSeconds with a longer value

* prevent [endpoint] json cacheSeconds property exceeding service default

* allow ShieldsRuntimeError to specify a cacheSeconds property

By default error responses use the cacheLength of
the service class throwing the error.

This allows error to tell the handling layer the maxAge
that should be set on the error badge response.

* add customExceptions param

This

1. allows us to specify custom properties to pass to the exception
   constructor if we throw any of the standard got errors
   e.g: `ETIMEDOUT`, `ECONNRESET`, etc
2. uses a custom `cacheSeconds` property (if set on the exception)
   to set the response maxAge

* customExceptions --> systemErrors

* errorMessages --> httpErrors
2023-06-13 21:08:43 +01:00

99 lines
2.6 KiB
JavaScript

import Joi from 'joi'
import prettyBytes from 'pretty-bytes'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'
const schema = Joi.object({
size: nonNegativeInteger,
gzip: nonNegativeInteger,
}).required()
const keywords = ['node', 'bundlephobia']
export default class Bundlephobia extends BaseJsonService {
static category = 'size'
static route = {
base: 'bundlephobia',
pattern: ':format(min|minzip)/:scope(@[^/]+)?/:packageName/:version?',
}
static examples = [
{
title: 'npm bundle size',
pattern: ':format(min|minzip)/:packageName',
namedParams: {
format: 'min',
packageName: 'react',
},
staticPreview: this.render({ format: 'min', size: 6652 }),
keywords,
},
{
title: 'npm bundle size (scoped)',
pattern: ':format(min|minzip)/:scope/:packageName',
namedParams: {
format: 'min',
scope: '@cycle',
packageName: 'core',
},
staticPreview: this.render({ format: 'min', size: 3562 }),
keywords,
},
{
title: 'npm bundle size (version)',
pattern: ':format(min|minzip)/:packageName/:version',
namedParams: {
format: 'min',
packageName: 'react',
version: '15.0.0',
},
staticPreview: this.render({ format: 'min', size: 20535 }),
keywords,
},
{
title: 'npm bundle size (scoped version)',
pattern: ':format(min|minzip)/:scope/:packageName/:version',
namedParams: {
format: 'min',
scope: '@cycle',
packageName: 'core',
version: '7.0.0',
},
staticPreview: this.render({ format: 'min', size: 3562 }),
keywords,
},
]
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 })
}
}