* add renderSizeBadge helper, use it everywhere - switch from pretty-bytes to byte-size - add renderSizeBadge() helper function - match upstream conventions for metric/IEC units - add new test helpers and use them in service tests * unrelated: fix npm unpacked size query param schema not strictly related to this PR but I noticed it was broken * chromewebstore: reformat size string, test against isIecFileSize
51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
import Joi from 'joi'
|
|
import { pathParams } from '../index.js'
|
|
import { renderSizeBadge } from '../size.js'
|
|
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 openApi = {
|
|
'/github/repo-size/{user}/{repo}': {
|
|
get: {
|
|
summary: 'GitHub repo size',
|
|
description: documentation,
|
|
parameters: pathParams(
|
|
{
|
|
name: 'user',
|
|
example: 'atom',
|
|
},
|
|
{
|
|
name: 'repo',
|
|
example: 'atom',
|
|
},
|
|
),
|
|
},
|
|
},
|
|
}
|
|
|
|
static defaultBadgeData = { label: 'repo size' }
|
|
|
|
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 })
|
|
// note the GH API returns size in KiB
|
|
// so we multiply by 1024 to get a size in bytes and then format that in IEC bytes
|
|
return renderSizeBadge(size * 1024, 'iec', 'repo size')
|
|
}
|
|
}
|