Skip to content

Commit

Permalink
move query params to be local to dynamic badge
Browse files Browse the repository at this point in the history
  • Loading branch information
RedSparr0w committed Oct 31, 2017
1 parent c497f95 commit d841039
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
4 changes: 0 additions & 4 deletions lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ const globalQueryParams = new Set([
'link',
'colorA',
'colorB',
'prefix',
'suffix',
'uri',
'query'
]);

function flattenQueryParams(queryParams) {
Expand Down
65 changes: 34 additions & 31 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7112,43 +7112,46 @@ camp.route(/^\/maven-metadata\/v\/(https?)\/(.+\.xml)\.(svg|png|gif|jpg|json)$/,

// User defined sources - JSON response
camp.route(/^\/badge\/dynamic\/(json)\.(svg|png|gif|jpg|json)$/,
cache(function(query, match, sendBadge, request) {
var type = match[1];
var format = match[2];
var prefix = query.prefix || '';
var suffix = query.suffix || '';
var pathExpression = query.query;
cache({
queryParams: ['uri', 'query', 'prefix', 'suffix'],
handler: function(query, match, sendBadge, request) {
var type = match[1];
var format = match[2];
var prefix = query.prefix || '';
var suffix = query.suffix || '';
var pathExpression = query.query;

var badgeData = getBadgeData('custom badge', query);
var badgeData = getBadgeData('custom badge', query);

if (!query.uri){
badgeData.text[1] = 'no uri specified';
sendBadge(format, badgeData);
return;
}
var uri = encodeURI(decodeURIComponent(query.uri));
if (!query.uri){
badgeData.text[1] = 'no uri specified';
sendBadge(format, badgeData);
return;
}
var uri = encodeURI(decodeURIComponent(query.uri));

request(uri, (err, res, data) => {
try {
if (res && res.statusCode === 404)
throw 'invalid resource';
request(uri, (err, res, data) => {
try {
if (res && res.statusCode === 404)
throw 'invalid resource';

if (err != null || !res || res.statusCode !== 200)
throw 'inaccessible';
if (err != null || !res || res.statusCode !== 200)
throw 'inaccessible';

switch (type){
case 'json':
data = (typeof data == 'object' ? data : JSON.parse(data));
badgeData.text[1] = (prefix || '') + jp.query(data, pathExpression).join(', ') + (suffix || '');
break;
switch (type){
case 'json':
data = (typeof data == 'object' ? data : JSON.parse(data));
badgeData.text[1] = (prefix || '') + jp.query(data, pathExpression).join(', ') + (suffix || '');
break;
}
} catch(e) {
badgeData.colorB = 'lightgrey';
badgeData.text[1] = e;
} finally {
sendBadge(format, badgeData);
}
} catch(e) {
badgeData.colorB = 'lightgrey';
badgeData.text[1] = e;
} finally {
sendBadge(format, badgeData);
}
});
});
}
}));

// nsp for npm packages
Expand Down

0 comments on commit d841039

Please sign in to comment.