* allow serviceData to override cacheSeconds with a longer value * prevent [endpoint] json cacheSeconds property exceeding service default * allow ShieldsRuntimeError to specify a cacheSeconds property By default error responses use the cacheLength of the service class throwing the error. This allows error to tell the handling layer the maxAge that should be set on the error badge response. * add customExceptions param This 1. allows us to specify custom properties to pass to the exception constructor if we throw any of the standard got errors e.g: `ETIMEDOUT`, `ECONNRESET`, etc 2. uses a custom `cacheSeconds` property (if set on the exception) to set the response maxAge * customExceptions --> systemErrors * errorMessages --> httpErrors
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
import Joi from 'joi'
|
|
import { metric } from '../text-formatters.js'
|
|
import { nonNegativeInteger } from '../validators.js'
|
|
import { GithubAuthV3Service } from './github-auth-service.js'
|
|
import { documentation, httpErrorsFor } from './github-helpers.js'
|
|
|
|
const schema = Joi.object({
|
|
stargazers_count: nonNegativeInteger,
|
|
}).required()
|
|
|
|
export default class GithubStars extends GithubAuthV3Service {
|
|
static category = 'social'
|
|
|
|
static route = {
|
|
base: 'github/stars',
|
|
pattern: ':user/:repo',
|
|
}
|
|
|
|
static examples = [
|
|
{
|
|
title: 'GitHub Repo stars',
|
|
namedParams: {
|
|
user: 'badges',
|
|
repo: 'shields',
|
|
},
|
|
queryParams: { style: 'social' },
|
|
// TODO: This is currently a literal, as `staticPreview` doesn't
|
|
// support `link`.
|
|
staticPreview: {
|
|
label: 'Stars',
|
|
message: '7k',
|
|
style: 'social',
|
|
},
|
|
documentation,
|
|
},
|
|
]
|
|
|
|
static defaultBadgeData = {
|
|
label: 'stars',
|
|
namedLogo: 'github',
|
|
}
|
|
|
|
static render({ stars, user, repo }) {
|
|
const slug = `${encodeURIComponent(user)}/${encodeURIComponent(repo)}`
|
|
return {
|
|
message: metric(stars),
|
|
color: 'blue',
|
|
link: [
|
|
`https://github.com/${slug}`,
|
|
`https://github.com/${slug}/stargazers`,
|
|
],
|
|
}
|
|
}
|
|
|
|
async handle({ user, repo }) {
|
|
const { stargazers_count: stars } = await this._requestJson({
|
|
url: `/repos/${user}/${repo}`,
|
|
schema,
|
|
httpErrors: httpErrorsFor(),
|
|
})
|
|
return this.constructor.render({ user, repo, stars })
|
|
}
|
|
}
|