@@ -71,6 +71,36 @@ function staticBadgeUrl({
|
||||
return `${baseUrl}/badge/${path}.${format}${suffix}`
|
||||
}
|
||||
|
||||
function queryStringStaticBadgeUrl({
|
||||
baseUrl = '',
|
||||
label,
|
||||
message,
|
||||
color,
|
||||
labelColor,
|
||||
style,
|
||||
namedLogo,
|
||||
logoColor,
|
||||
logoWidth,
|
||||
logoPosition,
|
||||
format = 'svg',
|
||||
}) {
|
||||
// schemaVersion could be a parameter if we iterate on it,
|
||||
// for now it's hardcoded to the only supported version.
|
||||
const schemaVersion = '1'
|
||||
const suffix = `?${queryString.stringify({
|
||||
label,
|
||||
message,
|
||||
color,
|
||||
labelColor,
|
||||
style,
|
||||
logo: namedLogo,
|
||||
logoColor,
|
||||
logoWidth,
|
||||
logoPosition,
|
||||
})}`
|
||||
return `${baseUrl}/static/v${schemaVersion}.${format}${suffix}`
|
||||
}
|
||||
|
||||
function dynamicBadgeUrl({
|
||||
baseUrl,
|
||||
datatype,
|
||||
@@ -109,5 +139,6 @@ module.exports = {
|
||||
badgeUrlFromPattern,
|
||||
encodeField,
|
||||
staticBadgeUrl,
|
||||
queryStringStaticBadgeUrl,
|
||||
dynamicBadgeUrl,
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ const {
|
||||
badgeUrlFromPattern,
|
||||
encodeField,
|
||||
staticBadgeUrl,
|
||||
queryStringStaticBadgeUrl,
|
||||
dynamicBadgeUrl,
|
||||
} = require('./make-badge-url')
|
||||
|
||||
@@ -81,6 +82,35 @@ describe('Badge URL generation functions', function() {
|
||||
}).expect('/badge/-blue-blue.svg')
|
||||
})
|
||||
|
||||
test(queryStringStaticBadgeUrl, () => {
|
||||
// the query-string library sorts parameters by name
|
||||
given({
|
||||
label: 'foo',
|
||||
message: 'bar',
|
||||
color: 'blue',
|
||||
style: 'flat-square',
|
||||
}).expect(
|
||||
'/static/v1.svg?color=blue&label=foo&message=bar&style=flat-square'
|
||||
)
|
||||
given({
|
||||
label: 'foo Bar',
|
||||
message: 'bar Baz',
|
||||
color: 'blue',
|
||||
style: 'flat-square',
|
||||
format: 'png',
|
||||
namedLogo: 'github',
|
||||
}).expect(
|
||||
'/static/v1.png?color=blue&label=foo%20Bar&logo=github&message=bar%20Baz&style=flat-square'
|
||||
)
|
||||
given({
|
||||
label: 'Hello World',
|
||||
message: 'Привет Мир',
|
||||
color: '#aabbcc',
|
||||
}).expect(
|
||||
'/static/v1.svg?color=%23aabbcc&label=Hello%20World&message=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%9C%D0%B8%D1%80'
|
||||
)
|
||||
})
|
||||
|
||||
test(dynamicBadgeUrl, () => {
|
||||
const dataUrl = 'http://example.com/foo.json'
|
||||
const query = '$.bar'
|
||||
|
||||
@@ -62,7 +62,7 @@ module.exports = function coalesceBadge(
|
||||
labelColor: overrideLabelColor,
|
||||
} = overrides
|
||||
|
||||
// Only use the legacy properties of the new ones are not provided
|
||||
// Only use the legacy properties if the new ones are not provided
|
||||
if (typeof overrideColor === 'undefined') {
|
||||
overrideColor = legacyOverrideColor
|
||||
}
|
||||
|
||||
@@ -179,12 +179,17 @@ export default class Usage extends React.PureComponent {
|
||||
|
||||
<VerticalSpace />
|
||||
|
||||
<p>Using dash "-" separator</p>
|
||||
<p>
|
||||
<Snippet
|
||||
snippet={`${baseUrl}/badge/<SUBJECT>-<STATUS>-<COLOR>.svg`}
|
||||
/>
|
||||
<Snippet snippet={`${baseUrl}/badge/<LABEL>-<MESSAGE>-<COLOR>.svg`} />
|
||||
</p>
|
||||
{this.constructor.renderStaticBadgeEscapingRules()}
|
||||
<p>Using query string parameters</p>
|
||||
<p>
|
||||
<Snippet
|
||||
snippet={`${baseUrl}/static/v1.svg?label=<LABEL>&message=<MESSAGE>&color=<COLOR>`}
|
||||
/>
|
||||
</p>
|
||||
|
||||
<H3 id="colors">Colors</H3>
|
||||
<p>
|
||||
|
||||
32
services/static-badge/query-string-static.service.js
Normal file
32
services/static-badge/query-string-static.service.js
Normal file
@@ -0,0 +1,32 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('joi')
|
||||
const { BaseStaticService, InvalidParameter } = require('..')
|
||||
|
||||
const queryParamSchema = Joi.object({
|
||||
message: Joi.string().required(),
|
||||
}).required()
|
||||
|
||||
module.exports = class QueryStringStaticBadge extends BaseStaticService {
|
||||
static get category() {
|
||||
return 'other'
|
||||
}
|
||||
|
||||
static get route() {
|
||||
return {
|
||||
format: 'static/v([0-9])',
|
||||
capture: ['schemaVersion'],
|
||||
// All but one of the parameters are parsed via coalesceBadge. This
|
||||
// reuses what is the override behaviour for other badges.
|
||||
queryParamSchema,
|
||||
}
|
||||
}
|
||||
|
||||
handle(namedParams, queryParams) {
|
||||
if (namedParams.schemaVersion !== '1') {
|
||||
throw new InvalidParameter({ prettyMessage: 'Invalid schemaVersion' })
|
||||
}
|
||||
|
||||
return { message: queryParams.message }
|
||||
}
|
||||
}
|
||||
39
services/static-badge/query-string-static.tester.js
Normal file
39
services/static-badge/query-string-static.tester.js
Normal file
@@ -0,0 +1,39 @@
|
||||
'use strict'
|
||||
|
||||
const t = (module.exports = require('../tester').createServiceTester())
|
||||
|
||||
t.create('Missing message')
|
||||
.get('/static/v1.json?label=label&message=&color=blue&style=_shields_test')
|
||||
.expectJSON({
|
||||
name: 'label',
|
||||
value: 'invalid query parameter: message',
|
||||
color: 'red',
|
||||
})
|
||||
|
||||
t.create('Missing label')
|
||||
.get('/static/v1.json?label=&message=message&color=blue&style=_shields_test')
|
||||
.expectJSON({ name: '', value: 'message', color: 'blue' })
|
||||
|
||||
t.create('Case is preserved')
|
||||
.get(
|
||||
'/static/v1.json?label=LiCeNsE&message=mIt&color=blue&style=_shields_test'
|
||||
)
|
||||
.expectJSON({ name: 'LiCeNsE', value: 'mIt', color: 'blue' })
|
||||
|
||||
t.create('Set color')
|
||||
.get(
|
||||
'/static/v1.json?label=label&message=message&color=yellow&style=_shields_test'
|
||||
)
|
||||
.expectJSON({ name: 'label', value: 'message', color: 'yellow' })
|
||||
|
||||
t.create('Set color with a number')
|
||||
.get(
|
||||
'/static/v1.json?label=label&message=message&color=123&style=_shields_test'
|
||||
)
|
||||
.expectJSON({ name: 'label', value: 'message', color: '#123' })
|
||||
|
||||
t.create('Set label')
|
||||
.get(
|
||||
'/static/v1.json?label=mylabel&message=message&color=blue&style=_shields_test'
|
||||
)
|
||||
.expectJSON({ name: 'mylabel', value: 'message', color: 'blue' })
|
||||
Reference in New Issue
Block a user