Skip to content

Commit 6af0739

Browse files
committed
feat: update
1 parent dacfdc1 commit 6af0739

3 files changed

Lines changed: 31 additions & 14 deletions

File tree

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,26 @@ 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-
##### `colorA`
161+
##### `labelColor`
162162

163163
Overrides the default label color. You can pass a standard hex code (with or without the `#` prefix).
164164

165165
- **Default**: `#0a0a0a`
166-
- **Usage**: `?colorA=HEX_CODE`
166+
- **Usage**: `?labelColor=HEX_CODE`
167167

168-
##### `colorB`
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+
175+
##### `color`
169176

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

172179
- **Default**: Depends on the badge type (e.g., version is blue, downloads are orange).
173-
- **Usage**: `?colorB=HEX_CODE`
180+
- **Usage**: `?color=HEX_CODE`
174181

175182
| Example | URL |
176183
| :------------- | :------------------------------------- |

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ const BUNDLEPHOBIA_API = 'https://bundlephobia.com/api/size'
1212
const NPMS_API = 'https://api.npms.io/v2/package'
1313

1414
const QUERY_SCHEMA = v.object({
15-
colorA: v.optional(v.string()),
15+
color: v.optional(v.string()),
1616
name: v.optional(v.string()),
17-
colorB: v.optional(v.string()),
17+
labelColor: v.optional(v.string()),
18+
label: v.optional(v.string()),
1819
})
1920

2021
const COLORS = {
@@ -263,9 +264,10 @@ export default defineCachedEventHandler(
263264
})
264265

265266
const queryParams = v.safeParse(QUERY_SCHEMA, query)
266-
const userColor = queryParams.success ? queryParams.output.colorB : undefined
267-
const userColorLeft = queryParams.success ? queryParams.output.colorA : undefined
267+
const userColor = queryParams.success ? queryParams.output.color : undefined
268+
const labelColor = queryParams.success ? queryParams.output.labelColor : undefined
268269
const showName = queryParams.success && queryParams.output.name === 'true'
270+
const userLabel = queryParams.success ? queryParams.output.label : undefined
269271

270272
const badgeTypeResult = v.safeParse(BadgeTypeSchema, typeParam)
271273
const strategyKey = badgeTypeResult.success ? badgeTypeResult.output : 'version'
@@ -276,13 +278,13 @@ export default defineCachedEventHandler(
276278
const pkgData = await fetchNpmPackage(packageName)
277279
const strategyResult = await strategy(pkgData, requestedVersion)
278280

279-
const finalLabel = showName ? packageName : strategyResult.label
281+
const finalLabel = userLabel ? userLabel : showName ? packageName : strategyResult.label
280282
const finalValue = strategyResult.value
281283

282284
const rawColor = userColor ?? strategyResult.color
283285
const finalColor = rawColor?.startsWith('#') ? rawColor : `#${rawColor}`
284286

285-
const rawLeftColor = userColorLeft ?? '#0a0a0a'
287+
const rawLeftColor = labelColor ?? '#0a0a0a'
286288
const finalLeftColor = rawLeftColor?.startsWith('#') ? rawLeftColor : `#${rawLeftColor}`
287289

288290
const leftWidth = measureTextWidth(finalLabel)

test/e2e/badge.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,30 @@ test.describe('badge API', () => {
102102
})
103103
})
104104

105-
test('custom colorA parameter is applied to SVG', async ({ page, baseURL }) => {
105+
test('custom labelColor parameter is applied to SVG', async ({ page, baseURL }) => {
106106
const customColor = '00ff00'
107-
const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?colorA=${customColor}`)
107+
const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?labelColor=${customColor}`)
108108
const { body } = await fetchBadge(page, url)
109109

110110
expect(body).toContain(`fill="#${customColor}"`)
111111
})
112112

113-
test('custom colorB parameter is applied to SVG', async ({ page, baseURL }) => {
113+
test('custom color parameter is applied to SVG', async ({ page, baseURL }) => {
114114
const customColor = 'ff69b4'
115-
const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?colorB=${customColor}`)
115+
const url = toLocalUrl(baseURL, `/api/registry/badge/version/nuxt?color=${customColor}`)
116116
const { body } = await fetchBadge(page, url)
117117

118118
expect(body).toContain(`fill="#${customColor}"`)
119119
})
120120

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+
121129
test('invalid badge type defaults to version strategy', async ({ page, baseURL }) => {
122130
const url = toLocalUrl(baseURL, '/api/registry/badge/invalid-type/nuxt')
123131
const { body } = await fetchBadge(page, url)

0 commit comments

Comments
 (0)