Skip to content

Commit 50ed1cd

Browse files
fix: use consistent package parsing on social/likes (#1180)
1 parent d3800dc commit 50ed1cd

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

app/composables/usePackageComparison.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ export function usePackageComparison(packageNames: MaybeRefOrGetter<string[]>) {
142142
$fetch<VulnerabilityTreeResult>(
143143
`/api/registry/vulnerabilities/${encodePackageName(name)}`,
144144
).catch(() => null),
145-
$fetch<PackageLikes>(`/api/social/likes/${name}`).catch(() => null),
145+
$fetch<PackageLikes>(`/api/social/likes/${encodePackageName(name)}`).catch(
146+
() => null,
147+
),
146148
])
147149
const versionData = pkgData.versions[latestVersion]
148150
const packageSize = versionData?.dist?.unpackedSize
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
1+
import * as v from 'valibot'
2+
import { PackageRouteParamsSchema } from '#shared/schemas/package'
3+
4+
/**
5+
* GET /api/social/likes/:name
6+
*
7+
* Gets the likes for a npm package on npmx
8+
*/
19
export default eventHandlerWithOAuthSession(async (event, oAuthSession, _) => {
2-
const packageName = getRouterParam(event, 'pkg')
3-
if (!packageName) {
10+
const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? []
11+
const { rawPackageName } = parsePackageParams(pkgParamSegments)
12+
13+
if (!rawPackageName) {
414
throw createError({
515
status: 400,
616
message: 'package name not provided',
717
})
818
}
919

10-
const likesUtil = new PackageLikesUtils()
11-
return await likesUtil.getLikes(packageName, oAuthSession?.did.toString())
20+
try {
21+
const { packageName } = v.parse(PackageRouteParamsSchema, {
22+
packageName: decodeURIComponent(rawPackageName),
23+
})
24+
25+
const likesUtil = new PackageLikesUtils()
26+
return await likesUtil.getLikes(packageName, oAuthSession?.did.toString())
27+
} catch (error: unknown) {
28+
handleApiError(error, {
29+
statusCode: 502,
30+
message: 'Failed to get likes',
31+
})
32+
}
1233
})

0 commit comments

Comments
 (0)