* Build(deps-dev): bump eslint-plugin-import from 2.17.3 to 2.18.0 Bumps [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import) from 2.17.3 to 2.18.0. - [Release notes](https://github.com/benmosher/eslint-plugin-import/releases) - [Changelog](https://github.com/benmosher/eslint-plugin-import/blob/master/CHANGELOG.md) - [Commits](https://github.com/benmosher/eslint-plugin-import/compare/v2.17.3...v2.18.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * Autofixes
102 lines
2.6 KiB
JavaScript
102 lines
2.6 KiB
JavaScript
'use strict'
|
|
|
|
const Joi = require('@hapi/joi')
|
|
const { metric } = require('../text-formatters')
|
|
const { nonNegativeInteger } = require('../validators')
|
|
const { BaseJsonService } = require('..')
|
|
|
|
// https://github.com/npm/registry/blob/master/docs/download-counts.md#output
|
|
const pointResponseSchema = Joi.object({
|
|
downloads: nonNegativeInteger,
|
|
}).required()
|
|
|
|
const intervalMap = {
|
|
dw: {
|
|
query: 'point/last-week',
|
|
schema: pointResponseSchema,
|
|
transform: json => json.downloads,
|
|
messageSuffix: '/week',
|
|
},
|
|
dm: {
|
|
query: 'point/last-month',
|
|
schema: pointResponseSchema,
|
|
transform: json => json.downloads,
|
|
messageSuffix: '/month',
|
|
},
|
|
dy: {
|
|
query: 'point/last-year',
|
|
schema: pointResponseSchema,
|
|
transform: json => json.downloads,
|
|
messageSuffix: '/year',
|
|
},
|
|
dt: {
|
|
query: 'range/1000-01-01:3000-01-01',
|
|
// https://github.com/npm/registry/blob/master/docs/download-counts.md#output-1
|
|
schema: Joi.object({
|
|
downloads: Joi.array()
|
|
.items(pointResponseSchema)
|
|
.required(),
|
|
}).required(),
|
|
transform: json =>
|
|
json.downloads
|
|
.map(item => item.downloads)
|
|
.reduce((accum, current) => accum + current),
|
|
messageSuffix: '',
|
|
},
|
|
}
|
|
|
|
// This hits an entirely different API from the rest of the NPM services, so
|
|
// it does not use NpmBase.
|
|
module.exports = class NpmDownloads extends BaseJsonService {
|
|
static get category() {
|
|
return 'downloads'
|
|
}
|
|
|
|
static get route() {
|
|
return {
|
|
base: 'npm',
|
|
pattern: ':interval(dw|dm|dy|dt)/:scope(@.+)?/:packageName',
|
|
}
|
|
}
|
|
|
|
static get examples() {
|
|
return [
|
|
{
|
|
title: 'npm',
|
|
namedParams: { interval: 'dw', packageName: 'localeval' },
|
|
staticPreview: this.render({ interval: 'dw', downloadCount: 30000 }),
|
|
keywords: ['node'],
|
|
},
|
|
]
|
|
}
|
|
|
|
// For testing.
|
|
static get _intervalMap() {
|
|
return intervalMap
|
|
}
|
|
|
|
static render({ interval, downloadCount }) {
|
|
const { messageSuffix } = intervalMap[interval]
|
|
|
|
return {
|
|
message: `${metric(downloadCount)}${messageSuffix}`,
|
|
color: downloadCount > 0 ? 'brightgreen' : 'red',
|
|
}
|
|
}
|
|
|
|
async handle({ interval, scope, packageName }) {
|
|
const { query, schema, transform } = intervalMap[interval]
|
|
|
|
const slug = scope ? `${scope}/${packageName}` : packageName
|
|
const json = await this._requestJson({
|
|
schema,
|
|
url: `https://api.npmjs.org/downloads/${query}/${slug}`,
|
|
errorMessages: { 404: 'package not found or too new' },
|
|
})
|
|
|
|
const downloadCount = transform(json)
|
|
|
|
return this.constructor.render({ interval, downloadCount })
|
|
}
|
|
}
|