|
| 1 | +interface LicenseChangeRecord { |
| 2 | + from: string |
| 3 | + to: string |
| 4 | +} |
| 5 | + |
| 6 | +export default defineCachedEventHandler( |
| 7 | + async event => { |
| 8 | + // 1. Extract the package name from the catch-all parameter |
| 9 | + const packageName = getRouterParam(event, 'pkg') |
| 10 | + if (!packageName) { |
| 11 | + throw createError({ |
| 12 | + statusCode: 400, |
| 13 | + statusMessage: 'Package name is required', |
| 14 | + }) |
| 15 | + } |
| 16 | + const query = getQuery(event) |
| 17 | + const version = query.version || 'latest' |
| 18 | + |
| 19 | + try { |
| 20 | + // 2. Fetch the "Packument" on the server |
| 21 | + // This stays on the server, so the client never downloads this massive JSON |
| 22 | + const data = await fetchNpmPackage(packageName) |
| 23 | + |
| 24 | + if (!data.versions || !data.time) { |
| 25 | + throw createError({ |
| 26 | + statusCode: 404, |
| 27 | + statusMessage: 'Package metadata not found', |
| 28 | + }) |
| 29 | + } |
| 30 | + // 3. Process the logic |
| 31 | + const versions = Object.values(data.versions) |
| 32 | + |
| 33 | + // Sort versions chronologically using the 'time' object |
| 34 | + versions.sort((a, b) => { |
| 35 | + const timeA = new Date(data.time[a.version] as string).getTime() |
| 36 | + const timeB = new Date(data.time[b.version] as string).getTime() |
| 37 | + return timeA - timeB |
| 38 | + }) |
| 39 | + let change: LicenseChangeRecord | null = null |
| 40 | + |
| 41 | + const currentVersionIndex = |
| 42 | + version === 'latest' ? versions.length - 1 : versions.findIndex(v => v.version === version) |
| 43 | + |
| 44 | + const previousVersionIndex = currentVersionIndex - 1 |
| 45 | + const currentLicense = String(versions[currentVersionIndex]?.license || 'UNKNOWN') |
| 46 | + const previousLicense = String(versions[previousVersionIndex]?.license || 'UNKNOWN') |
| 47 | + |
| 48 | + if (currentLicense !== previousLicense) { |
| 49 | + change = { |
| 50 | + from: previousLicense, |
| 51 | + to: currentLicense, |
| 52 | + } |
| 53 | + } |
| 54 | + return { change } |
| 55 | + } catch (error: any) { |
| 56 | + throw createError({ |
| 57 | + statusCode: error.statusCode || 500, |
| 58 | + statusMessage: `Failed to fetch license data: ${error.message}`, |
| 59 | + }) |
| 60 | + } |
| 61 | + }, |
| 62 | + { |
| 63 | + // 5. Cache Configuration |
| 64 | + maxAge: 60 * 60, // time in seconds |
| 65 | + swr: true, |
| 66 | + getKey: event => { |
| 67 | + const pkg = getRouterParam(event, 'pkg') ?? '' |
| 68 | + const query = getQuery(event) |
| 69 | + |
| 70 | + // 1. remove the /'s from the package name |
| 71 | + const cleanPkg = pkg.replace(/\/+$/, '').trim() |
| 72 | + |
| 73 | + // 2. Get the version (default to 'latest' if not provided) |
| 74 | + const version = query.version || 'latest' |
| 75 | + |
| 76 | + // 3. Create a unique string such that it takes into account the pckage name and version |
| 77 | + // sample result: "license-change:v1:faker:2.1.15" |
| 78 | + return `license-change:v2:${cleanPkg}:${version}` |
| 79 | + }, |
| 80 | + }, |
| 81 | +) |
0 commit comments