Skip to content

Commit bf8fa75

Browse files
bteadanielroe
andauthored
feat: badge support label/labelColor (#752)
Co-authored-by: Daniel Roe <daniel@roe.dev>
1 parent d4584fe commit bf8fa75

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

docs/content/2.guide/1.features.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,32 @@ Make sure to replace `TYPE` with one of the options listed below and `YOUR_PACKA
158158

159159
You can further customize your badges by appending query parameters to the badge URL.
160160

161+
##### `labelColor`
162+
163+
Overrides the default label color. You can pass a standard hex code (with or without the `#` prefix).
164+
165+
- **Default**: `#0a0a0a`
166+
- **Usage**: `?labelColor=HEX_CODE`
167+
168+
##### `label`
169+
170+
Overrides the default label text. You can pass any string to customize the label displayed on the badge.
171+
172+
- **Default**: Depends on the badge type (e.g., "version", "downloads/mo").
173+
- **Usage**: `?label=YOUR_LABEL`
174+
161175
##### `color`
162176

163177
Overrides the default strategy color. You can pass a standard hex code (with or without the `#` prefix).
164178

165179
- **Default**: Depends on the badge type (e.g., version is blue, downloads are orange).
166180
- **Usage**: `?color=HEX_CODE`
167181

168-
| Example | URL |
169-
| :------------- | :------------------------------------ |
170-
| **Hot Pink** | `.../badge/version/nuxt?color=ff69b4` |
171-
| **Pure Black** | `.../badge/version/nuxt?color=000000` |
172-
| **Brand Blue** | `.../badge/version/nuxt?color=3b82f6` |
182+
| Example | URL |
183+
| :------------- | :------------------------------------- |
184+
| **Hot Pink** | `.../badge/version/nuxt?colorB=ff69b4` |
185+
| **Pure Black** | `.../badge/version/nuxt?colorB=000000` |
186+
| **Brand Blue** | `.../badge/version/nuxt?colorB=3b82f6` |
173187

174188
##### `name`
175189

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const NPMS_API = 'https://api.npms.io/v2/package'
1414
const QUERY_SCHEMA = v.object({
1515
color: v.optional(v.string()),
1616
name: v.optional(v.string()),
17+
labelColor: v.optional(v.string()),
18+
label: v.optional(v.string()),
1719
})
1820

1921
const COLORS = {
@@ -263,7 +265,9 @@ export default defineCachedEventHandler(
263265

264266
const queryParams = v.safeParse(QUERY_SCHEMA, query)
265267
const userColor = queryParams.success ? queryParams.output.color : undefined
268+
const labelColor = queryParams.success ? queryParams.output.labelColor : undefined
266269
const showName = queryParams.success && queryParams.output.name === 'true'
270+
const userLabel = queryParams.success ? queryParams.output.label : undefined
267271

268272
const badgeTypeResult = v.safeParse(BadgeTypeSchema, typeParam)
269273
const strategyKey = badgeTypeResult.success ? badgeTypeResult.output : 'version'
@@ -274,12 +278,15 @@ export default defineCachedEventHandler(
274278
const pkgData = await fetchNpmPackage(packageName)
275279
const strategyResult = await strategy(pkgData, requestedVersion)
276280

277-
const finalLabel = showName ? packageName : strategyResult.label
281+
const finalLabel = userLabel ? userLabel : showName ? packageName : strategyResult.label
278282
const finalValue = strategyResult.value
279283

280284
const rawColor = userColor ?? strategyResult.color
281285
const finalColor = rawColor?.startsWith('#') ? rawColor : `#${rawColor}`
282286

287+
const rawLabelColor = labelColor ?? '#0a0a0a'
288+
const finalLabelColor = rawLabelColor?.startsWith('#') ? rawLabelColor : `#${rawLabelColor}`
289+
283290
const leftWidth = measureTextWidth(finalLabel)
284291
const rightWidth = measureTextWidth(finalValue)
285292
const totalWidth = leftWidth + rightWidth
@@ -291,7 +298,7 @@ export default defineCachedEventHandler(
291298
<rect width="${totalWidth}" height="${height}" rx="3" fill="#fff"/>
292299
</clipPath>
293300
<g clip-path="url(#r)">
294-
<rect width="${leftWidth}" height="${height}" fill="#0a0a0a"/>
301+
<rect width="${leftWidth}" height="${height}" fill="${finalLabelColor}"/>
295302
<rect x="${leftWidth}" width="${rightWidth}" height="${height}" fill="${finalColor}"/>
296303
</g>
297304
<g text-anchor="middle" font-family="'Geist', system-ui, -apple-system, sans-serif" font-size="11">

test/e2e/badge.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ test.describe('badge API', () => {
102102
})
103103
})
104104

105+
test('custom labelColor parameter is applied to SVG', async ({ page, baseURL }) => {
106+
const customColor = '00ff00'
107+
const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?labelColor=${customColor}`)
108+
const { body } = await fetchBadge(page, url)
109+
110+
expect(body).toContain(`fill="#${customColor}"`)
111+
})
112+
105113
test('custom color parameter is applied to SVG', async ({ page, baseURL }) => {
106114
const customColor = 'ff69b4'
107115
const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?color=${customColor}`)
@@ -110,6 +118,14 @@ test.describe('badge API', () => {
110118
expect(body).toContain(`fill="#${customColor}"`)
111119
})
112120

121+
test('custom label parameter is applied to SVG', async ({ page, baseURL }) => {
122+
const customLabel = 'my-label'
123+
const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?label=${customLabel}`)
124+
const { body } = await fetchBadge(page, url)
125+
126+
expect(body).toContain(customLabel)
127+
})
128+
113129
test('invalid badge type defaults to version strategy', async ({ page, baseURL }) => {
114130
const url = toLocalUrl(baseURL, '/api/registry/badge/invalid-type/nuxt')
115131
const { body } = await fetchBadge(page, url)

0 commit comments

Comments
 (0)