|
| 1 | +import * as v from 'valibot' |
| 2 | +import { createError, getRouterParam, setHeader } from 'h3' |
| 3 | +import { PackageRouteParamsSchema } from '#shared/schemas/package' |
| 4 | +import { CACHE_MAX_AGE_ONE_HOUR } from '#shared/utils/constants' |
| 5 | +import { fetchNpmPackage } from '#server/utils/npm' |
| 6 | +import { assertValidPackageName } from '#shared/utils/npm' |
| 7 | +import { handleApiError } from '#server/utils/error-handler' |
| 8 | + |
| 9 | +function measureTextWidth(text: string, charWidth = 6.2, paddingX = 6): number { |
| 10 | + return Math.max(40, Math.round(text.length * charWidth) + paddingX * 2) |
| 11 | +} |
| 12 | + |
| 13 | +export default defineCachedEventHandler( |
| 14 | + async event => { |
| 15 | + const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? [] |
| 16 | + if (pkgParamSegments.length === 0) { |
| 17 | + throw createError({ statusCode: 400, message: 'Package name is required.' }) |
| 18 | + } |
| 19 | + |
| 20 | + const { rawPackageName, rawVersion } = parsePackageParams(pkgParamSegments) |
| 21 | + |
| 22 | + try { |
| 23 | + const { packageName, version: requestedVersion } = v.parse(PackageRouteParamsSchema, { |
| 24 | + packageName: rawPackageName, |
| 25 | + version: rawVersion, |
| 26 | + }) |
| 27 | + |
| 28 | + assertValidPackageName(packageName) |
| 29 | + |
| 30 | + const label = `./ ${packageName}` |
| 31 | + |
| 32 | + const packument = await fetchNpmPackage(packageName) |
| 33 | + const value = requestedVersion ?? packument['dist-tags']?.latest ?? 'unknown' |
| 34 | + |
| 35 | + const leftWidth = measureTextWidth(label) |
| 36 | + const rightWidth = measureTextWidth(value) |
| 37 | + const totalWidth = leftWidth + rightWidth |
| 38 | + const height = 20 |
| 39 | + |
| 40 | + const svg = ` |
| 41 | + <svg xmlns="http://www.w3.org/2000/svg" width="${totalWidth}" height="${height}" role="img" aria-label="${label}: ${value}"> |
| 42 | + <clipPath id="r"> |
| 43 | + <rect width="${totalWidth}" height="${height}" rx="3" fill="#fff"/> |
| 44 | + </clipPath> |
| 45 | + <g clip-path="url(#r)"> |
| 46 | + <rect width="${leftWidth}" height="${height}" fill="#0a0a0a"/> |
| 47 | + <rect x="${leftWidth}" width="${rightWidth}" height="${height}" fill="#ffffff"/> |
| 48 | + </g> |
| 49 | + <g text-anchor="middle" font-family="'Geist', system-ui, -apple-system, sans-serif" font-size="11"> |
| 50 | + <text x="${leftWidth / 2}" y="14" fill="#ffffff">${label}</text> |
| 51 | + <text x="${leftWidth + rightWidth / 2}" y="14" fill="#000000">${value}</text> |
| 52 | + </g> |
| 53 | + </svg> |
| 54 | + `.trim() |
| 55 | + |
| 56 | + setHeader(event, 'Content-Type', 'image/svg+xml') |
| 57 | + |
| 58 | + return svg |
| 59 | + } catch (error: unknown) { |
| 60 | + handleApiError(error, { |
| 61 | + statusCode: 502, |
| 62 | + message: 'Failed to generate npm badge.', |
| 63 | + }) |
| 64 | + } |
| 65 | + }, |
| 66 | + { |
| 67 | + maxAge: CACHE_MAX_AGE_ONE_HOUR, |
| 68 | + swr: true, |
| 69 | + getKey: event => { |
| 70 | + const pkg = getRouterParam(event, 'pkg') ?? '' |
| 71 | + return `badge:version:${pkg}` |
| 72 | + }, |
| 73 | + }, |
| 74 | +) |
0 commit comments