Skip to content

Commit 577e018

Browse files
committed
refactor: reorganize api-related utils
1 parent ce7690c commit 577e018

File tree

12 files changed

+52
-41
lines changed

12 files changed

+52
-41
lines changed
File renamed without changes.

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ export const PNPM_WORKSPACE_PATTERN = `**/${PNPM_WORKSPACE_BASENAME}`
77
export const VERSION_TRIGGER_CHARACTERS = ['.', '^', '~', ...Array.from({ length: 10 }).map((_, i) => `${i}`)]
88

99
export const CACHE_TTL_ONE_DAY = 1000 * 60 * 60 * 24
10+
11+
export const NPM_REGISTRY = 'https://registry.npmjs.org'
12+
export const NPMX_DEV_API = 'https://npmx.dev/api'

src/providers/completion-item/version.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Extractor } from '#types/extractor'
22
import type { CompletionItemProvider, Position, TextDocument } from 'vscode'
33
import { config } from '#state'
4-
import { getPackageInfo } from '#utils/npm'
5-
import { extractVersionPrefix } from '#utils/version'
4+
import { getPackageInfo } from '#utils/api/package'
5+
import { extractVersionPrefix } from '#utils/package'
66
import { CompletionItem, CompletionItemKind } from 'vscode'
77

88
export class VersionCompletionItemProvider<T extends Extractor> implements CompletionItemProvider {

src/providers/diagnostics/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { DependencyInfo, Extractor, ValidNode } from '#types/extractor'
2-
import type { ResolvedPackument } from '#utils/npm'
2+
import type { ResolvedPackument } from '#utils/api/package'
33
import type { Awaitable } from 'reactive-vscode'
44
import type { Diagnostic, TextDocument } from 'vscode'
55
import { basename } from 'node:path'
66
import { config, logger } from '#state'
7-
import { getPackageInfo } from '#utils/npm'
7+
import { getPackageInfo } from '#utils/api/package'
88
import { useActiveTextEditor, useDocumentText, watch } from 'reactive-vscode'
99
import { languages } from 'vscode'
1010
import { displayName } from '../../generated-meta'

src/providers/diagnostics/rules/deprecation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { DiagnosticRule } from '..'
2-
import { extractVersion } from '#utils/version'
2+
import { extractVersion } from '#utils/package'
33
import { DiagnosticSeverity } from 'vscode'
44

55
export const checkDeprecation: DiagnosticRule = (dep, pkg) => {

src/providers/diagnostics/rules/replacement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { ModuleReplacement } from 'module-replacements'
1+
import type { ModuleReplacement } from '#utils/api/replacement'
22
import type { DiagnosticRule } from '..'
3-
import { getReplacement } from '#utils/npmx'
3+
import { getReplacement } from '#utils/api/replacement'
44
import { DiagnosticSeverity } from 'vscode'
55

66
// https://github.com/npmx-dev/npmx.dev/blob/main/app/components/PackageReplacement.vue#L8-L30

src/providers/diagnostics/rules/vulnerability.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { OsvSeverityLevel } from '#utils/npmx'
1+
import type { OsvSeverityLevel } from '#utils/api/vulnerability'
22
import type { DiagnosticRule } from '..'
3-
import { getVulnerability, SEVERITY_LEVELS } from '#utils/npmx'
4-
import { extractVersion } from '#utils/version'
3+
import { getVulnerability, SEVERITY_LEVELS } from '#utils/api/vulnerability'
4+
import { extractVersion } from '#utils/package'
55
import { DiagnosticSeverity } from 'vscode'
66

77
const DIAGNOSTIC_MAPPING: Record<Exclude<OsvSeverityLevel, 'unknown'>, DiagnosticSeverity> = {

src/providers/hover/npmx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Extractor } from '#types/extractor'
22
import type { HoverProvider, Position, TextDocument } from 'vscode'
3-
import { getPackageInfo } from '#utils/npm'
4-
import { extractVersion } from '#utils/version'
3+
import { getPackageInfo } from '#utils/api/package'
4+
import { extractVersion } from '#utils/package'
55
import { Hover, MarkdownString } from 'vscode'
66

77
export class NpmxHoverProvider<T extends Extractor> implements HoverProvider {
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Packument, PackumentVersion } from '@npm/types'
2+
import { NPM_REGISTRY } from '#constants'
23
import { logger } from '#state'
4+
import { encodePackageName } from '#utils/package'
35
import { ofetch } from 'ofetch'
4-
import { memoize } from './memoize'
5-
6-
const NPM_REGISTRY = 'https://registry.npmjs.org'
6+
import { memoize } from '../memoize'
77

88
interface ResolvedPackumentVersion extends Pick<PackumentVersion, 'version'> {
99
tag?: string
@@ -15,17 +15,6 @@ export interface ResolvedPackument {
1515
versions: Record<string, ResolvedPackumentVersion>
1616
}
1717

18-
/**
19-
* Encode a package name for use in npm registry URLs.
20-
* Handles scoped packages (e.g., @scope/name -> @scope%2Fname).
21-
*/
22-
export function encodePackageName(name: string): string {
23-
if (name.startsWith('@')) {
24-
return `@${encodeURIComponent(name.slice(1))}`
25-
}
26-
return encodeURIComponent(name)
27-
}
28-
2918
export const getPackageInfo = memoize<string, Promise<ResolvedPackument>>(async (name) => {
3019
logger.info(`Fetching package info for ${name}`)
3120
const encodedName = encodePackageName(name)

src/utils/api/replacement.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { ModuleReplacement } from 'module-replacements'
2+
import { NPMX_DEV_API } from '#constants'
3+
import { logger } from '#state'
4+
import { ofetch } from 'ofetch'
5+
import { memoize } from '../memoize'
6+
import { encodePackageName } from '../package'
7+
8+
export type {
9+
ModuleReplacement,
10+
}
11+
12+
export const getReplacement = memoize<string, Promise<ModuleReplacement>>(async (name) => {
13+
logger.info(`Fetching replacements for ${name}`)
14+
const encodedName = encodePackageName(name)
15+
16+
const result = await ofetch<ModuleReplacement>(`${NPMX_DEV_API}/replacements/${encodedName}`)
17+
logger.info(`Fetched replacements for ${name}`)
18+
19+
return result
20+
})

0 commit comments

Comments
 (0)