From df8049a7659b8cba84070f09e8f5cd3d70f75a0c Mon Sep 17 00:00:00 2001 From: chris48s Date: Sat, 23 Mar 2024 19:38:05 +0000 Subject: [PATCH] redirect [npm] /dt to /d18m (#10033) * redirect [npm] /dt to /d18m * fix unit test --- .../npm/npm-downloads-redirect.service.js | 11 ++++++++ services/npm/npm-downloads.service.js | 7 ++--- services/npm/npm-downloads.spec.js | 4 +-- services/npm/npm-downloads.tester.js | 26 +++++++++++++------ 4 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 services/npm/npm-downloads-redirect.service.js diff --git a/services/npm/npm-downloads-redirect.service.js b/services/npm/npm-downloads-redirect.service.js new file mode 100644 index 0000000000..9227d8a6dd --- /dev/null +++ b/services/npm/npm-downloads-redirect.service.js @@ -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'), +}) diff --git a/services/npm/npm-downloads.service.js b/services/npm/npm-downloads.service.js index 56e0fc09ac..8132a7aa6f 100644 --- a/services/npm/npm-downloads.service.js +++ b/services/npm/npm-downloads.service.js @@ -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') }, }, { diff --git a/services/npm/npm-downloads.spec.js b/services/npm/npm-downloads.spec.js index ef811e9451..6529dd57ef 100644 --- a/services/npm/npm-downloads.spec.js +++ b/services/npm/npm-downloads.spec.js @@ -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', diff --git a/services/npm/npm-downloads.tester.js b/services/npm/npm-downloads.tester.js index 4ff67bad94..7c02c940e9 100644 --- a/services/npm/npm-downloads.tester.js +++ b/services/npm/npm-downloads.tester.js @@ -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')