fix: use path param with regex for npm package feed (#14626)

* fix: use path param with regex for npm package feed

1.  Don't include query params like `?code` in the package name.
2.  Handle special cases when the org name of the package ends with
    `package`, i.e. the package name is something like `@*package/*`.

* refactor: use map

---------
This commit is contained in:
Yufan You
2024-03-03 14:33:11 +08:00
committed by GitHub
parent 2c771aa0f6
commit cd6f2d982c
2 changed files with 7 additions and 8 deletions
+5 -7
View File
@@ -3,7 +3,7 @@ import { art } from '@/utils/render';
import * as path from 'node:path';
export default async (ctx) => {
const name = ctx.req.url.split('package/')[1];
const name = ctx.req.param('name');
const packageDownloadLastMonthAPI = `https://api.npmjs.org/downloads/point/last-month/${name}`; // 按月统计
const packageDownloadLastWeekAPI = `https://api.npmjs.org/downloads/point/last-week/${name}`; // 按周统计
const packageDownloadLastDayAPI = `https://api.npmjs.org/downloads/point/last-day/${name}`; // 按天统计
@@ -15,14 +15,12 @@ export default async (ctx) => {
const packageVersionRes = await got(packageVersionAPI).json();
const packageVersion = packageVersionRes.time;
const packageVersionList = [];
for (const key in packageVersion) {
packageVersionList.push({
const packageVersionList = Object.keys(packageVersion)
.map((key) => ({
version: key,
time: packageVersion[key],
});
}
packageVersionList.reverse();
}))
.toReversed();
ctx.set('data', {
title: `${name} - npm`,
+2 -1
View File
@@ -1,3 +1,4 @@
export default (router) => {
router.get('package/*', './package');
// https://github.com/dword-design/package-name-regex/blob/master/src/index.js
router.get('package/:name{(@[a-z0-9-~][a-z0-9-._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*}', './package');
};