Files
shields/frontend/lib/badge-url.js
Paul Melnikow 3bb392dfae Remove some duplicated URL generation code (#2240)
I went down a rabbit hole while trying to untangle the bug in the dockbit and bitrise examples https://github.com/badges/shields/pull/2234#pullrequestreview-169997546.

The URL generation code is spaghetti-like, with functions, many of which I wrote, with opaque names, doing similar but not identical things, and making slightly incompatible assumptions about the way query strings are handled.

I got a bit lost and need to take a step back.

Meanwhile, this is a small piece of work I did that’s worth keeping. It doesn’t scratch the surface of the tangle, but it does remove a bit of duplication.

It also makes a minor stylistic ES6 change in the handling of default arguments.

Ref: #2027
2018-11-05 16:55:49 -05:00

53 lines
1.2 KiB
JavaScript

import resolveUrl from './resolve-url'
import { staticBadgeUrl as makeStaticBadgeUrl } from '../../lib/make-badge-url'
export default function resolveBadgeUrl(
url,
baseUrl,
{ longCache, style, queryParams: inQueryParams } = {}
) {
const outQueryParams = Object.assign({}, inQueryParams)
if (longCache) {
outQueryParams.maxAge = '2592000'
}
if (style) {
outQueryParams.style = style
}
return resolveUrl(url, baseUrl, outQueryParams)
}
export function staticBadgeUrl(baseUrl, label, message, color, options) {
const path = makeStaticBadgeUrl({ label, message, color })
return resolveUrl(path, baseUrl, options)
}
// Options can include: { prefix, suffix, color, longCache, style, queryParams }
export function dynamicBadgeUrl(
baseUrl,
datatype,
label,
dataUrl,
query,
{ prefix, suffix, color, queryParams = {}, ...rest } = {}
) {
Object.assign(queryParams, {
label,
url: dataUrl,
query,
})
if (color) {
queryParams.colorB = color
}
if (prefix) {
queryParams.prefix = prefix
}
if (suffix) {
queryParams.suffix = suffix
}
const outOptions = Object.assign({ queryParams }, rest)
return resolveBadgeUrl(`/badge/dynamic/${datatype}.svg`, baseUrl, outOptions)
}