|
1 | | -import type { MaybeRefOrGetter } from "vue"; |
2 | | -import { toValue } from "vue"; |
| 1 | +import type { MaybeRefOrGetter } from 'vue' |
| 2 | +import { toValue } from 'vue' |
3 | 3 |
|
4 | 4 | export interface LicenseChange { |
5 | | - from: string; |
6 | | - to: string; |
7 | | - version: string; |
| 5 | + from: string |
| 6 | + to: string |
| 7 | + version: string |
8 | 8 | } |
9 | 9 |
|
10 | 10 | export interface LicenseChangesResult { |
11 | | - changes: LicenseChange[]; |
| 11 | + changes: LicenseChange[] |
12 | 12 | } |
13 | 13 |
|
14 | 14 | // Type definitions for npm registry response |
15 | 15 | interface NpmRegistryVersion { |
16 | | - version: string; |
17 | | - license?: string; |
| 16 | + version: string |
| 17 | + license?: string |
18 | 18 | } |
19 | 19 |
|
20 | 20 | // for registry responses of $fetch function, the type includes the key versions as well as many others too. |
21 | 21 | interface NpmRegistryResponse { |
22 | | - time: Record<string, string>; |
23 | | - versions: Record<string, NpmRegistryVersion>; |
| 22 | + time: Record<string, string> |
| 23 | + versions: Record<string, NpmRegistryVersion> |
24 | 24 | } |
25 | 25 |
|
26 | 26 | /** |
27 | 27 | * Composable to detect license changes across all versions of a package |
28 | 28 | */ |
29 | | -export function useLicenseChanges( |
30 | | - packageName: MaybeRefOrGetter<string | null | undefined>, |
31 | | -) { |
| 29 | +export function useLicenseChanges(packageName: MaybeRefOrGetter<string | null | undefined>) { |
32 | 30 | return useAsyncData<LicenseChangesResult>( |
33 | 31 | () => `license-changes:${toValue(packageName)}`, |
34 | 32 | async () => { |
35 | | - const name = toValue(packageName); |
36 | | - if (!name) return { changes: [] }; |
| 33 | + const name = toValue(packageName) |
| 34 | + if (!name) return { changes: [] } |
37 | 35 |
|
38 | 36 | // Fetch full package metadata from npm registry |
39 | | - const url = `https://registry.npmjs.org/${name}`; |
40 | | - const data = await $fetch<NpmRegistryResponse>(url); |
| 37 | + const url = `https://registry.npmjs.org/${name}` |
| 38 | + const data = await $fetch<NpmRegistryResponse>(url) |
41 | 39 |
|
42 | | - const changes: LicenseChange[] = []; |
43 | | - let prevLicense: string | undefined = undefined; |
| 40 | + const changes: LicenseChange[] = [] |
| 41 | + let prevLicense: string | undefined = undefined |
44 | 42 |
|
45 | 43 | // `data.versions` is an object with version keys |
46 | | - const versions = Object.values(data.versions) as NpmRegistryVersion[]; |
| 44 | + const versions = Object.values(data.versions) as NpmRegistryVersion[] |
47 | 45 |
|
48 | 46 | // Sort versions ascending to compare chronologically |
49 | 47 | versions.sort((a, b) => { |
50 | | - const dateA = new Date(data.time[a.version] as string).getTime(); |
51 | | - const dateB = new Date(data.time[b.version] as string).getTime(); |
| 48 | + const dateA = new Date(data.time[a.version] as string).getTime() |
| 49 | + const dateB = new Date(data.time[b.version] as string).getTime() |
52 | 50 |
|
53 | 51 | // Ascending order (oldest to newest) |
54 | | - return dateA - dateB; |
55 | | - }); |
| 52 | + return dateA - dateB |
| 53 | + }) |
56 | 54 |
|
57 | 55 | // Detect license changes |
58 | 56 | for (const version of versions) { |
59 | | - const license = (version.license as string) ?? "UNKNOWN"; |
| 57 | + const license = (version.license as string) ?? 'UNKNOWN' |
60 | 58 | if (prevLicense && license !== prevLicense) { |
61 | 59 | changes.push({ |
62 | 60 | from: prevLicense, |
63 | 61 | to: license, |
64 | 62 | version: version.version as string, |
65 | | - }); |
| 63 | + }) |
66 | 64 | } |
67 | | - prevLicense = license; |
| 65 | + prevLicense = license |
68 | 66 | } |
69 | | - return { changes }; |
| 67 | + return { changes } |
70 | 68 | }, |
71 | 69 | { |
72 | 70 | default: () => ({ changes: [] }), |
73 | 71 | watch: [() => toValue(packageName)], |
74 | 72 | }, |
75 | | - ); |
| 73 | + ) |
76 | 74 | } |
0 commit comments