Files
shields/services/github/github-repo-size.service.js
chris48s 504015c0ba migrate hapi/joi to joi (#5624)
* update joi + joi-extension-semver

* @hapi/joi --> joi

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2020-09-30 17:51:02 +00:00

51 lines
1.2 KiB
JavaScript

'use strict'
const Joi = require('joi')
const prettyBytes = require('pretty-bytes')
const { nonNegativeInteger } = require('../validators')
const { GithubAuthV3Service } = require('./github-auth-service')
const { documentation, errorMessagesFor } = require('./github-helpers')
const schema = Joi.object({
size: nonNegativeInteger,
}).required()
module.exports = 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,
errorMessages: errorMessagesFor(),
})
}
async handle({ user, repo }) {
const { size } = await this.fetch({ user, repo })
return this.constructor.render({ size })
}
}