Files
shields/services/github/github-repo-size.service.js
chris48s 75ee413178 Add BaseGraphqlService, support [github] V4 API (#3763)
* add base class for Graphql APIs
* add GithubAuthV4Service + updates to GH token pool
* update github forks to use GithubAuthV4Service
* rename GithubAuthService to GithubAuthV3Service
2019-07-29 21:42:03 +01:00

66 lines
1.4 KiB
JavaScript

'use strict'
const Joi = require('@hapi/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 get category() {
return 'size'
}
static get route() {
return {
base: 'github/repo-size',
pattern: ':user/:repo',
}
}
static get examples() {
return [
{
title: 'GitHub repo size',
namedParams: {
user: 'atom',
repo: 'atom',
},
staticPreview: this.render({ size: 319488 }),
documentation,
},
]
}
static get defaultBadgeData() {
return {
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 })
}
}