* add dynamic xml badge support * add tests * dynamic badge add tests for query param * remove try catch * use `checkErrorResponse()` * update tests * [dynamic json] add test for multiple items * Rebase, [dynamic xml] Add support for multiple items * 404 response -> resource not found * multiple results test less greedy regex still alot of room for improvement to this test * update dynamic badge gen * add dynamic xml gen tests * update tests id & path * datalist -> select * uri -> url kept support for uri, incase of any already existing badges. * split dynamic gen uri * update query placeholder
48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
import resolveUrl from './resolve-url';
|
|
|
|
export default function resolveBadgeUrl(url, baseUrl, options) {
|
|
const { longCache, style, queryParams: inQueryParams } = options || {};
|
|
const outQueryParams = Object.assign({}, inQueryParams);
|
|
if (longCache) {
|
|
outQueryParams.maxAge = '2592000';
|
|
}
|
|
if (style) {
|
|
outQueryParams.style = style;
|
|
}
|
|
return resolveUrl(url, baseUrl, outQueryParams);
|
|
}
|
|
|
|
export function encodeField(s) {
|
|
return encodeURIComponent(s.replace(/-/g, '--').replace(/_/g, '__'));
|
|
}
|
|
|
|
export function staticBadgeUrl(baseUrl, subject, status, color, options) {
|
|
const path = [subject, status, color].map(encodeField).join('-');
|
|
return resolveUrl(`/badge/${path}.svg`, baseUrl, options);
|
|
}
|
|
|
|
// Options can include: { prefix, suffix, color, longCache, style, queryParams }
|
|
export function dynamicBadgeUrl(baseUrl, datatype, label, dataUrl, query, options = {}) {
|
|
const { prefix, suffix, color, queryParams = {}, ...rest } = options;
|
|
|
|
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);
|
|
}
|