-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy path[...pkg].get.ts
More file actions
94 lines (80 loc) · 2.86 KB
/
[...pkg].get.ts
File metadata and controls
94 lines (80 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
interface LicenseChangeRecord {
from: string
to: string
}
interface NpmRegistryVersion {
version: string
license?: string
}
interface NpmRegistryResponse {
time: Record<string, string>
versions: Record<string, NpmRegistryVersion>
}
export default defineCachedEventHandler(
async event => {
// 1. Extract the package name from the catch-all parameter
const rawPkg = getRouterParam(event, 'pkg')
if (!rawPkg) {
throw createError({
statusCode: 400,
statusMessage: 'Package name is required',
})
}
const query = getQuery(event)
const version = query.version || 'latest'
const packageName = decodeURIComponent(rawPkg).replace(/\/+$/, '').trim()
try {
// 2. Fetch the "Packument" on the server
// This stays on the server, so the client never downloads this massive JSON
const data = await $fetch<NpmRegistryResponse>(`https://registry.npmjs.org/${packageName}`)
if (!data.versions || !data.time) {
throw createError({
statusCode: 404,
statusMessage: 'Package metadata not found',
})
}
// 3. Process the logic (moved from your composable)
const versions = Object.values(data.versions)
// Sort versions chronologically using the 'time' object
versions.sort((a, b) => {
const timeA = new Date(data.time[a.version] as string).getTime()
const timeB = new Date(data.time[b.version] as string).getTime()
return timeA - timeB
})
let change: LicenseChangeRecord | null = null
const currentVersionIndex =
version === 'latest' ? versions.length - 1 : versions.findIndex(v => v.version === version)
const previousVersionIndex = currentVersionIndex - 1
const currentLicense = String(versions[currentVersionIndex]?.license || 'UNKNOWN')
const previousLicense = String(versions[previousVersionIndex]?.license || 'UNKNOWN')
if (currentLicense !== previousLicense) {
change = {
from: previousLicense as string,
to: currentLicense as string,
}
}
return { change }
} catch (error: any) {
throw createError({
statusCode: error.statusCode || 500,
statusMessage: `Failed to fetch license data: ${error.message}`,
})
}
},
{
// 5. Cache Configuration
maxAge: 60 * 60, // time in seconds
swr: true,
getKey: event => {
const pkg = getRouterParam(event, 'pkg') ?? ''
const query = getQuery(event)
// 1. remove the /'s from the package name
const cleanPkg = pkg.replace(/\/+$/, '').trim()
// 2. Get the version (default to 'latest' if not provided)
const version = query.version || 'latest'
// 3. Create a unique string including the version
// Result: "license-change:v1:lodash:4.17.21"
return `license-change:v2:${cleanPkg}:${version}`
},
},
)