-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadge.js
51 lines (44 loc) · 1.11 KB
/
badge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const https = require('https');
DEFAULT_COLOR = "green"
function create_url_badge(key, value, query){
let url = `https://img.shields.io/badge/${key.replaceAll('-', '--')}-${value}-${DEFAULT_COLOR}`
if(Object.keys(query).length !== 0) {
const params = Object.entries(query)
.map(([k, v]) => `${k}=${v}`)
.join('&');
url += `?${params}`
}
return url
}
async function get_badge(key, value, query) {
const shields_url = create_url_badge(key, value, query)
console.log(`GET request to ${shields_url}`)
let data = ''
await new Promise((resolve) => {
https.get(shields_url, (resp) => {
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
resolve(data)
});
}).on("error", (err) => {
console.log("Shield.io request failed:")
console.error(err)
})
})
return data
}
function create_json_badge(key, value){
return {
"schemaVersion": 1,
"label": key,
"color": DEFAULT_COLOR,
"message": value.toString()
}
}
module.exports = {
get_badge, create_json_badge
}