Files
shields/services/github/github-watchers.service.js
2018-11-15 15:57:56 -05:00

70 lines
1.9 KiB
JavaScript

'use strict'
const LegacyService = require('../legacy-service')
const {
makeBadgeData: getBadgeData,
makeLogo: getLogo,
} = require('../../lib/badge-data')
const {
documentation,
checkErrorResponse: githubCheckErrorResponse,
} = require('./github-helpers')
module.exports = class GithubWatchers extends LegacyService {
static get category() {
return 'social'
}
static get route() {
return {
base: 'github/watchers',
}
}
static get examples() {
return [
{
title: 'GitHub watchers',
previewUrl: 'badges/shields',
query: { style: 'social', label: 'Watch' },
documentation,
},
]
}
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
camp.route(
/^\/github\/watchers\/([^/]+)\/([^/]+)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const user = match[1] // eg, qubyte/rubidium
const repo = match[2]
const format = match[3]
const apiUrl = `/repos/${user}/${repo}`
const badgeData = getBadgeData('watchers', data)
if (badgeData.template === 'social') {
badgeData.logo = getLogo('github', data)
badgeData.links = [
`https://github.com/${user}/${repo}`,
`https://github.com/${user}/${repo}/watchers`,
]
}
githubApiProvider.request(request, apiUrl, {}, (err, res, buffer) => {
if (githubCheckErrorResponse(badgeData, err, res)) {
sendBadge(format, badgeData)
return
}
try {
badgeData.text[1] = JSON.parse(buffer).subscribers_count
badgeData.colorscheme = null
badgeData.colorB = '#4183C4'
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
}
}