* init npm-unpacked-size service * add sample badge * fetch unpacked size from latest version * format unpacked size * parametrize unpacked size by package name * add optional version parameter * fix typo on test * rename test to tester * test against json endpoint instead of svg * test and impl version with undefined unpacked size * test version with defined unpacked size * change color to lightgray when unpackedSize is undefined * add openapi docs * extend NpmBase instead of BaseJsonService * use isFileSize validator * add schema to validate npm registry response body * add tests for scoped packages * impl scoped package route * change the type of schema.dist.unpackedSize to optionalNonNegativeInteger * add registry_uri query param * add default label to badge * unpack tag instead of version * revert back to versions instead of tags
88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
import Joi from 'joi'
|
|
import prettyBytes from 'pretty-bytes'
|
|
import { pathParam, queryParam } from '../index.js'
|
|
import { optionalNonNegativeInteger } from '../validators.js'
|
|
import NpmBase, { packageNameDescription } from './npm-base.js'
|
|
|
|
const schema = Joi.object({
|
|
dist: Joi.object({
|
|
unpackedSize: optionalNonNegativeInteger,
|
|
}).required(),
|
|
}).required()
|
|
|
|
export default class NpmUnpackedSize extends NpmBase {
|
|
static category = 'size'
|
|
|
|
static route = {
|
|
base: 'npm/unpacked-size',
|
|
pattern: ':scope(@[^/]+)?/:packageName/:version*',
|
|
}
|
|
|
|
static openApi = {
|
|
'/npm/unpacked-size/{packageName}': {
|
|
get: {
|
|
summary: 'NPM Unpacked Size',
|
|
parameters: [
|
|
pathParam({
|
|
name: 'packageName',
|
|
example: 'npm',
|
|
description: packageNameDescription,
|
|
}),
|
|
queryParam({
|
|
name: 'registry_uri',
|
|
example: 'https://registry.npmjs.com',
|
|
}),
|
|
],
|
|
},
|
|
},
|
|
'/npm/unpacked-size/{packageName}/{version}': {
|
|
get: {
|
|
summary: 'NPM Unpacked Size (with version)',
|
|
parameters: [
|
|
pathParam({
|
|
name: 'packageName',
|
|
example: 'npm',
|
|
description: packageNameDescription,
|
|
}),
|
|
pathParam({
|
|
name: 'version',
|
|
example: '4.18.2',
|
|
}),
|
|
queryParam({
|
|
name: 'registry_uri',
|
|
example: 'https://registry.npmjs.com',
|
|
}),
|
|
],
|
|
},
|
|
},
|
|
}
|
|
|
|
static defaultBadgeData = { label: 'unpacked size' }
|
|
|
|
async fetch({ registryUrl, packageName, version }) {
|
|
return this._requestJson({
|
|
schema,
|
|
url: `${registryUrl}/${packageName}/${version}`,
|
|
})
|
|
}
|
|
|
|
async handle(
|
|
{ scope, packageName, version },
|
|
{ registry_uri: registryUrl = 'https://registry.npmjs.org' },
|
|
) {
|
|
const packageNameWithScope = scope ? `${scope}/${packageName}` : packageName
|
|
const { dist } = await this.fetch({
|
|
registryUrl,
|
|
packageName: packageNameWithScope,
|
|
version: version ?? 'latest',
|
|
})
|
|
const { unpackedSize } = dist
|
|
|
|
return {
|
|
label: 'unpacked size',
|
|
message: unpackedSize ? prettyBytes(unpackedSize) : 'unknown',
|
|
color: unpackedSize ? 'blue' : 'lightgray',
|
|
}
|
|
}
|
|
}
|