redirect [npm] /dt to /d18m (#10033)
* redirect [npm] /dt to /d18m * fix unit test
This commit is contained in:
11
services/npm/npm-downloads-redirect.service.js
Normal file
11
services/npm/npm-downloads-redirect.service.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { redirector } from '../index.js'
|
||||
|
||||
export default redirector({
|
||||
category: 'downloads',
|
||||
route: {
|
||||
base: 'npm/dt',
|
||||
pattern: ':packageName+',
|
||||
},
|
||||
transformPath: ({ packageName }) => `/npm/d18m/${packageName}`,
|
||||
dateAdded: new Date('2024-03-19'),
|
||||
})
|
||||
@@ -28,7 +28,7 @@ const intervalMap = {
|
||||
transform: json => json.downloads,
|
||||
interval: 'year',
|
||||
},
|
||||
dt: {
|
||||
d18m: {
|
||||
query: 'range/1000-01-01:3000-01-01',
|
||||
// https://github.com/npm/registry/blob/master/docs/download-counts.md#output-1
|
||||
schema: Joi.object({
|
||||
@@ -48,7 +48,7 @@ export default class NpmDownloads extends BaseJsonService {
|
||||
|
||||
static route = {
|
||||
base: 'npm',
|
||||
pattern: ':interval(dw|dm|dy|dt)/:scope(@.+)?/:packageName',
|
||||
pattern: ':interval(dw|dm|dy|d18m)/:scope(@.+)?/:packageName',
|
||||
}
|
||||
|
||||
static openApi = {
|
||||
@@ -59,7 +59,8 @@ export default class NpmDownloads extends BaseJsonService {
|
||||
{
|
||||
name: 'interval',
|
||||
example: 'dw',
|
||||
description: 'Weekly, Monthly, Yearly, or Total downloads',
|
||||
description:
|
||||
'Downloads in the last Week, Month, Year, or 18 Months',
|
||||
schema: { type: 'string', enum: this.getEnum('interval') },
|
||||
},
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@ import { test, given } from 'sazerac'
|
||||
import NpmDownloads from './npm-downloads.service.js'
|
||||
|
||||
describe('NpmDownloads', function () {
|
||||
test(NpmDownloads._intervalMap.dt.transform, () => {
|
||||
test(NpmDownloads._intervalMap.d18m.transform, () => {
|
||||
given({
|
||||
downloads: [
|
||||
{ downloads: 2, day: '2018-01-01' },
|
||||
@@ -13,7 +13,7 @@ describe('NpmDownloads', function () {
|
||||
|
||||
test(NpmDownloads.render, () => {
|
||||
given({
|
||||
interval: 'dt',
|
||||
interval: 'd18m',
|
||||
downloadCount: 0,
|
||||
}).expect({
|
||||
color: 'red',
|
||||
|
||||
@@ -12,20 +12,30 @@ t.create('weekly downloads of @cycle/core')
|
||||
.get('/dw/@cycle/core.json')
|
||||
.expectBadge({ label: 'downloads', message: isMetricOverTimePeriod })
|
||||
|
||||
t.create('total downloads of left-pad').get('/dt/left-pad.json').expectBadge({
|
||||
label: 'downloads',
|
||||
message: isMetric,
|
||||
color: 'brightgreen',
|
||||
})
|
||||
t.create('downloads in last 18 months of left-pad')
|
||||
.get('/d18m/left-pad.json')
|
||||
.expectBadge({
|
||||
label: 'downloads',
|
||||
message: isMetric,
|
||||
color: 'brightgreen',
|
||||
})
|
||||
|
||||
t.create('total downloads of @cycle/core')
|
||||
.get('/dt/@cycle/core.json')
|
||||
t.create('downloads in last 18 months of @cycle/core')
|
||||
.get('/d18m/@cycle/core.json')
|
||||
.expectBadge({ label: 'downloads', message: isMetric })
|
||||
|
||||
t.create('downloads of unknown package')
|
||||
.get('/dt/npm-api-does-not-have-this-package.json')
|
||||
.get('/dy/npm-api-does-not-have-this-package.json')
|
||||
.expectBadge({
|
||||
label: 'downloads',
|
||||
message: 'package not found or too new',
|
||||
color: 'red',
|
||||
})
|
||||
|
||||
t.create('Total downloads redirect: unscoped package')
|
||||
.get('/dt/left-pad.svg')
|
||||
.expectRedirect('/npm/d18m/left-pad.svg')
|
||||
|
||||
t.create('Total downloads redirect: scoped package')
|
||||
.get('/dt/@cycle/core.svg')
|
||||
.expectRedirect('/npm/d18m/@cycle/core.svg')
|
||||
|
||||
Reference in New Issue
Block a user