diff --git a/docs/content/2.guide/6.badges.md b/docs/content/2.guide/6.badges.md index ab7cfb4da3..a5194a0f4a 100644 --- a/docs/content/2.guide/6.badges.md +++ b/docs/content/2.guide/6.badges.md @@ -28,6 +28,7 @@ npmx.dev offers many different SVG badges with stats about any package via its A - **maintainers**: Displays the total count of package maintainers. :img{src="https://img.shields.io/badge/%2306b6d4-06b6d4" class="inline align-middle h-5 w-14"} - **deprecated**: Shows if the package is active or deprecated. :img{src="https://img.shields.io/badge/%2322c55e-22c55e" class="inline align-middle h-5 w-14"} / :img{src="https://img.shields.io/badge/%23ef4444-ef4444" class="inline align-middle h-5 w-14"} - **name**: Simple badge displaying the package name. :img{src="https://img.shields.io/badge/%2364748b-64748b" class="inline align-middle h-5 w-14"} +- **likes**: Shows the total count of package likes. :img{src="https://img.shields.io/badge/%23ef4444-ef4444" class="inline align-middle h-5 w-14"} ## Examples diff --git a/docs/shared/utils/badges.ts b/docs/shared/utils/badges.ts index 2b7d425514..338d7923a9 100644 --- a/docs/shared/utils/badges.ts +++ b/docs/shared/utils/badges.ts @@ -16,6 +16,7 @@ export const BADGE_TYPES = Object.freeze([ 'maintainers', 'deprecated', 'name', + 'likes', ] as const) export type BadgeType = (typeof BADGE_TYPES)[number] diff --git a/server/api/registry/badge/[type]/[...pkg].get.ts b/server/api/registry/badge/[type]/[...pkg].get.ts index aa6b0c4b10..3ed6d2f079 100644 --- a/server/api/registry/badge/[type]/[...pkg].get.ts +++ b/server/api/registry/badge/[type]/[...pkg].get.ts @@ -496,6 +496,13 @@ const badgeStrategies = { color: isDeprecated ? COLORS.red : COLORS.green, } }, + + 'likes': async (pkgData: globalThis.Packument) => { + const likesUtil = new PackageLikesUtils() + const { totalLikes } = await likesUtil.getLikes(pkgData.name) + + return { label: 'likes', value: String(totalLikes ?? 0), color: COLORS.red } + }, } const BadgeTypeSchema = v.picklist(Object.keys(badgeStrategies) as [string, ...string[]])