1- import type { VulnerabilityTreeResult } from '#shared/types/dependency-analysis'
2-
31/**
42 * Shared composable for dependency analysis data (vulnerabilities, deprecated packages).
53 * Fetches once and caches the result so multiple components can use it.
@@ -9,44 +7,8 @@ export function useDependencyAnalysis(
97 packageName : MaybeRefOrGetter < string > ,
108 version : MaybeRefOrGetter < string > ,
119) {
12- // Build a stable key from the current values
13- const name = toValue ( packageName )
14- const ver = toValue ( version )
15- const key = `dep-analysis:v1:${ name } @${ ver } `
16-
17- // Use useState for SSR-safe caching across components
18- const data = useState < VulnerabilityTreeResult | null > ( key , ( ) => null )
19- const status = useState < 'idle' | 'pending' | 'success' | 'error' > ( `${ key } :status` , ( ) => 'idle' )
20- const error = useState < Error | null > ( `${ key } :error` , ( ) => null )
21-
22- async function fetch ( ) {
23- const pkgName = toValue ( packageName )
24- const pkgVersion = toValue ( version )
25-
26- if ( ! pkgName || ! pkgVersion ) return
27-
28- // Already fetched or fetching
29- if ( status . value === 'success' || status . value === 'pending' ) return
30-
31- status . value = 'pending'
32- error . value = null
33-
34- try {
35- const result = await $fetch < VulnerabilityTreeResult > (
36- `/api/registry/vulnerabilities/${ encodePackageName ( pkgName ) } /v/${ pkgVersion } ` ,
37- )
38- data . value = result
39- status . value = 'success'
40- } catch ( e ) {
41- error . value = e instanceof Error ? e : new Error ( 'Failed to fetch dependency analysis' )
42- status . value = 'error'
43- }
44- }
45-
46- return {
47- data : readonly ( data ) ,
48- status : readonly ( status ) ,
49- error : readonly ( error ) ,
50- fetch,
51- }
10+ return useFetch (
11+ ( ) =>
12+ `/api/registry/vulnerabilities/${ encodePackageName ( toValue ( packageName ) ) } /v/${ toValue ( version ) } ` ,
13+ )
5214}
0 commit comments