-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[badge-json] new "badge-json" service #1525
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { jsonBadgeUrl } from '../lib/badge-url'; | ||
|
||
export default class JsonBadgeMaker extends React.Component { | ||
static propTypes = { | ||
baseUri: PropTypes.string, | ||
}; | ||
|
||
state = { | ||
url: '' | ||
}; | ||
|
||
handleSubmit (e) { | ||
e.preventDefault(); | ||
|
||
const { baseUri } = this.props; | ||
const { url } = this.state; | ||
const badgeUri = jsonBadgeUrl(baseUri || window.location.href, url); | ||
|
||
document.location = badgeUri; | ||
} | ||
|
||
render() { | ||
return ( | ||
<form onSubmit={e => this.handleSubmit(e)}> | ||
<input | ||
className="medium" | ||
value={this.state.url} | ||
onChange={event => this.setState({ url: event.target.value })} | ||
placeholder="url" /> {} | ||
<button>Make Badge</button> | ||
</form> | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7545,6 +7545,71 @@ camp.route(/^\/maven-metadata\/v\/(https?)\/(.+\.xml)\.(svg|png|gif|jpg|json)$/, | |
}); | ||
})); | ||
|
||
// badge-json url | ||
camp.route(/^\/json\/(https?.+)\.(svg|png|gif|jpg|json)$/, | ||
cache(function(query, match, sendBadge, request) { | ||
const urlEncoded = match[1]; | ||
const format = match[2]; | ||
const defaultLabel = 'badge json'; | ||
let badgeData = getBadgeData(defaultLabel, {}); | ||
try { | ||
const url = encodeURI(decodeURIComponent(urlEncoded)); | ||
request(url, function dealWithData(err, res, buffer) { | ||
if (checkErrorResponse(badgeData, err, res, 'url not found')) { | ||
sendBadge(format, badgeData); | ||
return; | ||
} | ||
try { | ||
let data = JSON.parse(buffer); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you move this transformation code into its own function in |
||
if (!data.label) { | ||
// label should be non-empty | ||
throw {name:'message', message:'invalid badge-json'}; | ||
} | ||
if (!data.isSocial && !data.value) { | ||
// non-empty value required when !isSocial | ||
throw {name:'message', message:'invalid badge-json'}; | ||
} | ||
if (data.isSocial) { | ||
data.style = "social"; | ||
} | ||
// defined query vals override json vals | ||
for (let prop in query) { | ||
if (query[prop] !== undefined) { | ||
data[prop] = query[prop]; | ||
} | ||
} | ||
badgeData = getBadgeData(defaultLabel, data); | ||
badgeData.text[1] = data.value; | ||
if (badgeData.colorB === undefined && data.valueClass) { | ||
switch (data.valueClass) { | ||
case "error" : | ||
badgeData.colorscheme = "red"; | ||
break; | ||
case "notice" : | ||
badgeData.colorscheme = "orange"; | ||
break; | ||
case "success" : | ||
badgeData.colorscheme = "brightgreen"; | ||
break; | ||
case "info" : | ||
badgeData.colorscheme = "blue"; | ||
break; | ||
} | ||
} | ||
sendBadge(format, badgeData); | ||
} catch(e) { | ||
badgeData.text[1] = e.name === 'message' | ||
? e.message | ||
: 'invalid'; | ||
sendBadge(format, badgeData); | ||
} | ||
}); // end request | ||
} catch(e) { | ||
badgeData.text[1] = 'invalid'; | ||
sendBadge(format, badgeData); | ||
} | ||
})); | ||
|
||
// User defined sources - JSON response | ||
camp.route(/^\/badge\/dynamic\/(json)\.(svg|png|gif|jpg|json)$/, | ||
cache({ | ||
|
@@ -7971,7 +8036,7 @@ camp.route(/^\/vaadin-directory\/(star|status|rating|rc|rating-count|v|version|r | |
|
||
})); | ||
|
||
// Any badge. | ||
// "static" badge. | ||
camp.route(/^\/(:|badge\/)(([^-]|--)*?)-(([^-]|--)*)-(([^-]|--)+)\.(svg|png|gif|jpg)$/, | ||
function(data, match, end, ask) { | ||
var subject = escapeFormat(match[2]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned in #1525 (comment) let's make two or maybe three ways of leveraging the JSON functionality.
I'd prefer we add support for three endpoints:
/badge/dynamic/json-badge-data/https/example.com/info.json.svg
(as an alternative to /json`)/badge/dynamic/json-badge-data.svg?url=https%2Fexample.com%2Finfo.json
/badge/static.svg?badgeData=...
These support the more efficient static route for servers capable of building a redirect. That route can also be used in a dynamic web page, which is really cool! Simultaneously they make the "static" and "dynamic" distinctions very clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
sidenote
I'm not much of a fan of the
https/example.com
endpoints,just feels strange without the
https://
:/badge/dynamic/json-badge-data/https://example.com/path/to.json.svg
/badge/dynamic/json-badge-data/https/example.com/path/to.json.svg
but we have a few that way already so for the sake of consistency it's probably best to continue that pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea… I find it a little weird too. Though, I think a colon in a path is supposed to be URL encoded. Maybe we could just make the scheme optional and default to
https
? That way it would disappear in most cases…