-
-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy path[...pkg].get.ts
More file actions
42 lines (37 loc) · 1.4 KB
/
[...pkg].get.ts
File metadata and controls
42 lines (37 loc) · 1.4 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
import type { ExtendedPackageJson } from '#shared/utils/package-analysis'
import { PackageRouteParamsSchema } from '#shared/schemas/package'
import { ERROR_PACKAGE_DETECT_CHANGELOG, NPM_REGISTRY } from '#shared/utils/constants'
import * as v from 'valibot'
import { detectChangelog } from '~~/server/utils/changelog/detectChangelog'
// CACHE_MAX_AGE_ONE_DAY,
export default defineCachedEventHandler(
async event => {
const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? []
const { rawPackageName, rawVersion } = parsePackageParams(pkgParamSegments)
try {
const { packageName, version } = v.parse(PackageRouteParamsSchema, {
packageName: rawPackageName,
version: rawVersion,
})
const encodedName = encodePackageName(packageName)
const versionSuffix = version ? `/${version}` : '/latest'
const pkg = await $fetch<ExtendedPackageJson>(
`${NPM_REGISTRY}/${encodedName}${versionSuffix}`,
)
return await detectChangelog(pkg)
} catch (error) {
handleApiError(error, {
statusCode: 502,
message: ERROR_PACKAGE_DETECT_CHANGELOG,
})
}
},
// {
// maxAge: CACHE_MAX_AGE_ONE_DAY, // 24 hours - analysis rarely changes
// swr: true,
// getKey: event => {
// const pkg = getRouterParam(event, 'pkg') ?? ''
// return `changelog:v1:${pkg.replace(/\/+$/, '').trim()}`
// },
// },
)