Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/providers/hover/npmx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Extractor } from '#types/extractor'
import type { HoverProvider, Position, TextDocument } from 'vscode'
import { SPACER } from '#constants'
import { getPackageInfo } from '#utils/api/package'
import { npmPacakgeUrl, npmxDocsUrl, npmxPackageUrl } from '#utils/links'
import { jsrPackageUrl, npmPackageUrl, npmxDocsUrl, npmxPackageUrl } from '#utils/links'
import { isSupportedProtocol, parseVersion } from '#utils/package'
import { Hover, MarkdownString } from 'vscode'

Expand All @@ -24,24 +24,45 @@ export class NpmxHoverProvider<T extends Extractor> implements HoverProvider {
return

const parsed = parseVersion(dep.version)
if (!parsed || !isSupportedProtocol(parsed.protocol))
if (!parsed)
return

const { name } = dep
const { protocol, semver } = parsed

const pkg = await getPackageInfo(name)
if (!pkg)
if (protocol === 'jsr') {
const jsrMd = new MarkdownString('', true)
const jsrUrl = jsrPackageUrl(name, semver)

jsrMd.isTrusted = true

const jsrPackageLink = `[$(package)${SPACER}View on jsr.io](${jsrUrl})`
const npmxWarning = '$(warning) Not on npmx'
jsrMd.appendMarkdown(`${jsrPackageLink} | ${npmxWarning}`)

return new Hover(jsrMd)
}

if (!isSupportedProtocol(protocol))
return

const pkg = await getPackageInfo(name)
if (!pkg) {
const errorMd = new MarkdownString('', true)

errorMd.isTrusted = true
errorMd.appendMarkdown('$(warning) Unable to fetch package information')

return new Hover(errorMd)
}
Comment on lines +49 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Good error handling for fetch failures, but consider wrapping in try-catch.

This correctly addresses the PR objective of showing feedback when package lookup fails. However, looking at getPackageInfo in the relevant snippets, it returns null for 404 errors but throws for other error types. If a non-404 error occurs (e.g., network timeout, 500), this could result in an unhandled promise rejection.

🛡️ Proposed fix to handle all error cases
-    const pkg = await getPackageInfo(name)
-    if (!pkg) {
+    let pkg: Awaited<ReturnType<typeof getPackageInfo>> = null
+    try {
+      pkg = await getPackageInfo(name)
+    }
+    catch {
+      // Fall through to show error message
+    }
+
+    if (!pkg) {
       const errorMd = new MarkdownString('', true)
 
       errorMd.isTrusted = true
       errorMd.appendMarkdown('$(warning) Unable to fetch package information')
 
       return new Hover(errorMd)
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const pkg = await getPackageInfo(name)
if (!pkg) {
const errorMd = new MarkdownString('', true)
errorMd.isTrusted = true
errorMd.appendMarkdown('$(warning) Unable to fetch package information')
return new Hover(errorMd)
}
let pkg: Awaited<ReturnType<typeof getPackageInfo>> = null
try {
pkg = await getPackageInfo(name)
}
catch {
// Fall through to show error message
}
if (!pkg) {
const errorMd = new MarkdownString('', true)
errorMd.isTrusted = true
errorMd.appendMarkdown('$(warning) Unable to fetch package information')
return new Hover(errorMd)
}


const md = new MarkdownString('', true)
md.isTrusted = true

const { semver } = parsed

const currentVersion = pkg.versionsMeta[semver]
if (currentVersion) {
if (currentVersion.provenance)
md.appendMarkdown(`[$(verified)${SPACER}Verified provenance](${npmPacakgeUrl(name, semver)}#provenance)\n\n`)
md.appendMarkdown(`[$(verified)${SPACER}Verified provenance](${npmPackageUrl(name, semver)}#provenance)\n\n`)
}

const packageLink = `[$(package)${SPACER}View on npmx](${npmxPackageUrl(name)})`
Expand Down
6 changes: 5 additions & 1 deletion src/utils/links.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NPMJS_COM, NPMX_DEV } from '#constants'

export function npmPacakgeUrl(name: string, version?: string): string {
export function npmPackageUrl(name: string, version?: string): string {
return version
? `${NPMJS_COM}/package/${name}/v/${version}`
: `${NPMJS_COM}/package/${name}`
Expand All @@ -15,3 +15,7 @@ export function npmxPackageUrl(name: string, version?: string): string {
export function npmxDocsUrl(name: string, version: string): string {
return `${NPMX_DEV}/docs/${name}/v/${version}`
}

export function jsrPackageUrl(name: string, version: string): string {
return `https://jsr.io/${name}@${version}`
}