-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy path[...pkg].get.ts
More file actions
39 lines (36 loc) · 1.24 KB
/
[...pkg].get.ts
File metadata and controls
39 lines (36 loc) · 1.24 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
import { CACHE_MAX_AGE_ONE_HOUR, ERROR_NPM_FETCH_FAILED } from '#shared/utils/constants'
import { resolvePackageReadmeSource } from '#server/utils/readme-loaders'
/**
* Returns rendered README HTML for a package.
*
* URL patterns:
* - /api/registry/readme/packageName - latest version
* - /api/registry/readme/packageName/v/1.2.3 - specific version
* - /api/registry/readme/@scope/packageName - scoped package, latest
* - /api/registry/readme/@scope/packageName/v/1.2.3 - scoped package, specific version
*/
export default defineCachedEventHandler(
async event => {
try {
const packagePath = getRouterParam(event, 'pkg') ?? ''
const { packageName, markdown, repoInfo } = await resolvePackageReadmeSource(packagePath)
if (!markdown) {
return { html: '', mdExists: false, playgroundLinks: [], toc: [] }
}
return await renderReadmeHtml(markdown, packageName, repoInfo)
} catch (error: unknown) {
handleApiError(error, {
statusCode: 502,
message: ERROR_NPM_FETCH_FAILED,
})
}
},
{
maxAge: CACHE_MAX_AGE_ONE_HOUR,
swr: true,
getKey: event => {
const pkg = getRouterParam(event, 'pkg') ?? ''
return `readme:v9:${pkg.replace(/\/+$/, '').trim()}`
},
},
)