* 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
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
import Joi from 'joi'
|
|
import prettyBytes from 'pretty-bytes'
|
|
import { nonNegativeInteger } from '../validators.js'
|
|
import { GithubAuthV3Service } from './github-auth-service.js'
|
|
import { documentation, httpErrorsFor } from './github-helpers.js'
|
|
|
|
const schema = Joi.object({
|
|
size: nonNegativeInteger,
|
|
}).required()
|
|
|
|
export default class GithubRepoSize extends GithubAuthV3Service {
|
|
static category = 'size'
|
|
static route = { base: 'github/repo-size', pattern: ':user/:repo' }
|
|
static examples = [
|
|
{
|
|
title: 'GitHub repo size',
|
|
namedParams: {
|
|
user: 'atom',
|
|
repo: 'atom',
|
|
},
|
|
staticPreview: this.render({ size: 319488 }),
|
|
documentation,
|
|
},
|
|
]
|
|
|
|
static defaultBadgeData = { label: 'repo size' }
|
|
|
|
static render({ size }) {
|
|
return {
|
|
// note the GH API returns size in Kb
|
|
message: prettyBytes(size * 1024),
|
|
color: 'blue',
|
|
}
|
|
}
|
|
|
|
async fetch({ user, repo }) {
|
|
return this._requestJson({
|
|
url: `/repos/${user}/${repo}`,
|
|
schema,
|
|
httpErrors: httpErrorsFor(),
|
|
})
|
|
}
|
|
|
|
async handle({ user, repo }) {
|
|
const { size } = await this.fetch({ user, repo })
|
|
return this.constructor.render({ size })
|
|
}
|
|
}
|