Skip to content

Commit 3c6b335

Browse files
committed
fix: apply third-round copilot review suggestions
- URL-encode file path segments in the registry client - Drop unused getVersion and DownloadCard version prop (downloads are package-wide) - Keep canonical paths consistent with the real tab/search URLs
1 parent f5d6761 commit 3c6b335

5 files changed

Lines changed: 24 additions & 17 deletions

File tree

‎apps/web/app/components/cnpm/DownloadCard.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ const chartConfig = {
1919

2020
export function DownloadCard({
2121
pkgName,
22-
version,
2322
range = 7,
2423
}: {
2524
pkgName: string;
26-
version: string;
2725
range?: number;
2826
}) {
2927
const { data, loading } = useRegistryQuery(
3028
() => getDownloads(pkgName, range),
31-
[pkgName, version, range],
29+
[pkgName, range],
3230
);
3331

3432
if (loading) {

‎apps/web/app/components/cnpm/PkgSidebar.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function PkgSidebar({ manifest, version }: { manifest: RegistryManifest;
4141

4242
return (
4343
<div className="flex flex-col gap-5">
44-
<DownloadCard pkgName={manifest.name} version={version} />
44+
<DownloadCard pkgName={manifest.name} />
4545
{manifest.maintainers && manifest.maintainers.length > 0 && (
4646
<MaintainersCard maintainers={manifest.maintainers} />
4747
)}

‎apps/web/app/lib/registry/client.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ function ensureLeadingSlash(path: string) {
3838
return path.startsWith("/") ? path : `/${path}`;
3939
}
4040

41-
export function getManifest(pkg: string) {
42-
return registryJson<RegistryManifest>(`/${pkgPath(pkg)}`);
41+
function encodeFilePath(path: string) {
42+
return path
43+
.split("/")
44+
.map((segment) => encodeURIComponent(segment))
45+
.join("/");
4346
}
4447

45-
export function getVersion(pkg: string, version: string) {
46-
return registryJson<RegistryManifest>(`/${pkgPath(pkg)}/${encodeURIComponent(version)}`);
48+
export function getManifest(pkg: string) {
49+
return registryJson<RegistryManifest>(`/${pkgPath(pkg)}`);
4750
}
4851

4952
export function searchPackages(text: string, from = 0, size = 20) {
@@ -69,15 +72,15 @@ export function getDownloads(pkg: string, range = 7) {
6972
}
7073

7174
export function getDir(pkg: string, spec: string, path: string) {
72-
const dirPath = path && path !== "/" ? `${ensureLeadingSlash(path)}/` : "";
75+
const dirPath = path && path !== "/" ? `${encodeFilePath(ensureLeadingSlash(path))}/` : "";
7376
return registryJson<RegistryFilesResponse>(
7477
`/${pkgPath(pkg)}/${encodeURIComponent(spec)}/files${dirPath}?meta`,
7578
);
7679
}
7780

7881
export async function getFileContent(pkg: string, spec: string, path: string) {
7982
const res = await fetch(
80-
`${REGISTRY}/${pkgPath(pkg)}/${encodeURIComponent(spec)}/files${ensureLeadingSlash(path)}`,
83+
`${REGISTRY}/${pkgPath(pkg)}/${encodeURIComponent(spec)}/files${encodeFilePath(ensureLeadingSlash(path))}`,
8184
);
8285
if (res.status === 404) {
8386
throw new RegistryError(`Not Found: ${path}`, 404);

‎apps/web/app/routes/cnpm.pkg.tsx‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@ import { useRecentVisited } from "~/lib/registry/use-recent";
2020
import { seoMeta } from "~/lib/seo";
2121

2222
export function meta({ params }: { params: { "*"?: string } }) {
23-
const { name } = parsePkgPath(params["*"]);
23+
const { name, tab } = parsePkgPath(params["*"]);
24+
const path = name
25+
? tab === "home"
26+
? `/cnpm/pkg/${name}`
27+
: `/cnpm/pkg/${name}/${tab}`
28+
: "/cnpm";
2429
return seoMeta({
2530
title: name ? `${name} · CNPM 镜像` : "CNPM 包浏览器",
2631
description: name ? `查看 npm 包 ${name} 的 README、版本、依赖与文件。` : undefined,
27-
path: name ? `/cnpm/pkg/${name}` : "/cnpm",
32+
path,
2833
});
2934
}
3035

@@ -136,7 +141,7 @@ function CnpmPkgInner({ rest }: { rest?: string }) {
136141

137142
{tab === "trends" && (
138143
<div className="flex flex-col gap-4">
139-
<DownloadCard pkgName={name} version={version} range={30} />
144+
<DownloadCard pkgName={name} range={30} />
140145
<Empty>
141146
<EmptyTitle>趋势对比即将到来</EmptyTitle>
142147
<EmptyDescription>多包下载对比与时间范围切换将在此提供</EmptyDescription>

‎apps/web/app/routes/cnpm.search.tsx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ import { cn } from "~/lib/utils";
1818
const PAGE_SIZE = 20;
1919

2020
export function meta({ location }: { location: { search: string } }) {
21-
const q = new URLSearchParams(location.search).get("q") || "npm 包";
21+
const q = new URLSearchParams(location.search).get("q") || "";
22+
const label = q || "npm 包";
2223
return seoMeta({
23-
title: `搜索 ${q} · CNPM 镜像`,
24-
description: `在 npmmirror 镜像搜索 npm 包「${q}」。`,
25-
path: `/cnpm/search?q=${encodeURIComponent(q)}`,
24+
title: `搜索 ${label} · CNPM 镜像`,
25+
description: `在 npmmirror 镜像搜索 npm 包「${label}」。`,
26+
path: q ? `/cnpm/search?q=${encodeURIComponent(q)}` : "/cnpm/search",
2627
});
2728
}
2829

0 commit comments

Comments
 (0)