Skip to content

Commit 5c9f9f5

Browse files
authored
feat: add a badge type for package likes (#2478)
1 parent 51f4126 commit 5c9f9f5

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

docs/content/2.guide/6.badges.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ npmx.dev offers many different SVG badges with stats about any package via its A
2828
- **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"}
2929
- **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"}
3030
- **name**: Simple badge displaying the package name. :img{src="https://img.shields.io/badge/%2364748b-64748b" class="inline align-middle h-5 w-14"}
31+
- **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"}
3132

3233
## Examples
3334

docs/shared/utils/badges.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const BADGE_TYPES = Object.freeze([
1616
'maintainers',
1717
'deprecated',
1818
'name',
19+
'likes',
1920
] as const)
2021

2122
export type BadgeType = (typeof BADGE_TYPES)[number]

server/api/registry/badge/[type]/[...pkg].get.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,13 @@ const badgeStrategies = {
496496
color: isDeprecated ? COLORS.red : COLORS.green,
497497
}
498498
},
499+
500+
'likes': async (pkgData: globalThis.Packument) => {
501+
const likesUtil = new PackageLikesUtils()
502+
const { totalLikes } = await likesUtil.getLikes(pkgData.name)
503+
504+
return { label: 'likes', value: String(totalLikes ?? 0), color: COLORS.red }
505+
},
499506
}
500507

501508
const BadgeTypeSchema = v.picklist(Object.keys(badgeStrategies) as [string, ...string[]])

0 commit comments

Comments
 (0)