11import type { MaybeRefOrGetter } from 'vue'
22import { toValue } from 'vue'
3-
4- export interface LicenseChange {
5- from : string
6- to : string
7- version : string
8- }
9-
10- export interface LicenseChangesResult {
11- changes : LicenseChange [ ]
12- }
13-
14- // Type definitions for npm registry response
15- interface NpmRegistryVersion {
16- version : string
17- license ?: string
18- }
19-
20- // for registry responses of $fetch function, the type includes the key versions as well as many others too.
21- interface NpmRegistryResponse {
22- time : Record < string , string >
23- versions : Record < string , NpmRegistryVersion >
3+ interface LicenseChangeResponse {
4+ change : { from : string ; to : string } | null
245}
256
267/**
@@ -30,59 +11,19 @@ export function useLicenseChanges(
3011 packageName : MaybeRefOrGetter < string | null | undefined > ,
3112 resolvedVersion : MaybeRefOrGetter < string | null | undefined > = ( ) => undefined ,
3213) {
33- return useAsyncData < LicenseChangesResult > (
34- ( ) => {
35- const name = toValue ( packageName )
36- const version = toValue ( resolvedVersion ) ?? 'latest'
37- return `license-changes:${ name } :${ version } `
38- } ,
39- async ( ) => {
40- const name = toValue ( packageName )
41- const resolvedVer = toValue ( resolvedVersion )
42- if ( ! name ) return { changes : [ ] }
43-
44- // Fetch full package metadata from npm registry
45- const url = `https://registry.npmjs.org/${ name } `
46- const data = await $fetch < NpmRegistryResponse > ( url )
47-
48- const changes : LicenseChange [ ] = [ ]
49-
50- // `data.versions` is an object with version keys
51- const versions = Object . values ( data . versions ) as NpmRegistryVersion [ ]
52-
53- // Sort versions ascending to compare chronologically
54- versions . sort ( ( a , b ) => {
55- const dateA = new Date ( data . time [ a . version ] as string ) . getTime ( )
56- const dateB = new Date ( data . time [ b . version ] as string ) . getTime ( )
57-
58- // Ascending order (oldest to newest)
59- return dateA - dateB
60- } )
14+ const name = computed ( ( ) => toValue ( packageName ) )
15+ if ( ! name ) return { data : null } // Don't fetch if no name
6116
62- // When resolvedVer is not provided, check changes across all versions
63- const targetVersion = resolvedVer ?? versions [ versions . length - 1 ] ?. version
17+ const version = computed ( ( ) => toValue ( resolvedVersion ) ?? 'latest' )
6418
65- if ( targetVersion ) {
66- const resolvedIndex = versions . findIndex ( v => v . version === targetVersion )
19+ const url = computed ( ( ) => {
20+ return name . value ? `/api/registry/license-change/${ encodeURIComponent ( name . value ) } ` : ''
21+ } )
6722
68- if ( resolvedIndex > 0 ) {
69- const currentLicense = ( versions [ resolvedIndex ] ?. license as string ) ?? 'UNKNOWN'
70- const previousLicense = ( versions [ resolvedIndex - 1 ] ?. license as string ) ?? 'UNKNOWN'
23+ const result = useFetch < LicenseChangeResponse > ( url , {
24+ query : computed ( ( ) => ( { version : version . value } ) ) ,
25+ watch : [ url , version ] ,
26+ } )
7127
72- if ( currentLicense !== previousLicense ) {
73- changes . push ( {
74- from : previousLicense ,
75- to : currentLicense ,
76- version : ( versions [ resolvedIndex ] ?. version as string ) || 'UNKNOWN' ,
77- } )
78- }
79- }
80- }
81- return { changes }
82- } ,
83- {
84- default : ( ) => ( { changes : [ ] } ) ,
85- watch : [ ( ) => toValue ( packageName ) , ( ) => toValue ( resolvedVersion ) ] ,
86- } ,
87- )
28+ return result
8829}
0 commit comments