forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[...pkg].get.ts
More file actions
38 lines (35 loc) · 1.06 KB
/
[...pkg].get.ts
File metadata and controls
38 lines (35 loc) · 1.06 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
import * as v from 'valibot'
import { PackageNameSchema } from '#shared/schemas/package'
import { CACHE_MAX_AGE_ONE_HOUR, ERROR_JSR_FETCH_FAILED } from '#shared/utils/constants'
import type { JsrPackageInfo } from '#shared/types/jsr'
/**
* Check if an npm package exists on JSR.
*
* GET /api/jsr/:pkg
*
* @example GET /api/jsr/@std/fs → { exists: true, scope: "std", name: "fs", ... }
* @example GET /api/jsr/lodash → { exists: false }
*/
export default defineCachedEventHandler<Promise<JsrPackageInfo>>(
async event => {
const pkgPath = getRouterParam(event, 'pkg')
try {
const packageName = v.parse(PackageNameSchema, pkgPath)
return await fetchJsrPackageInfo(packageName)
} catch (error: unknown) {
handleApiError(error, {
statusCode: 502,
message: ERROR_JSR_FETCH_FAILED,
})
}
},
{
maxAge: CACHE_MAX_AGE_ONE_HOUR,
swr: true,
name: 'api-jsr-package',
getKey: event => {
const pkg = getRouterParam(event, 'pkg') ?? ''
return `jsr:v1:${pkg.replace(/\/+$/, '').trim()}`
},
},
)