Files
shields/services/github/github-size.service.js
chris48s 5fb78c50ef migrate examples to openApi part 38; affects [github] (#9889)
* migrate some services from examples to openApi

* Update services/github/github-size.service.js

Co-authored-by: Pierre-Yves Bigourdan <10694593+PyvesB@users.noreply.github.com>

---------

Co-authored-by: Pierre-Yves Bigourdan <10694593+PyvesB@users.noreply.github.com>
2024-01-13 18:31:59 +00:00

79 lines
2.1 KiB
JavaScript

import Joi from 'joi'
import prettyBytes from 'pretty-bytes'
import { nonNegativeInteger } from '../validators.js'
import { NotFound, pathParam, queryParam } from '../index.js'
import { GithubAuthV3Service } from './github-auth-service.js'
import { documentation, httpErrorsFor } from './github-helpers.js'
const queryParamSchema = Joi.object({
branch: Joi.string(),
}).required()
const schema = Joi.alternatives(
Joi.object({
size: nonNegativeInteger,
}).required(),
Joi.array().required(),
)
export default class GithubSize extends GithubAuthV3Service {
static category = 'size'
static route = {
base: 'github/size',
pattern: ':user/:repo/:path+',
queryParamSchema,
}
static openApi = {
'/github/size/{user}/{repo}/{path}': {
get: {
summary: 'GitHub file size in bytes',
description: documentation,
parameters: [
pathParam({ name: 'user', example: 'webcaetano' }),
pathParam({ name: 'repo', example: 'craft' }),
pathParam({ name: 'path', example: 'build/phaser-craft.min.js' }),
queryParam({
name: 'branch',
example: 'master',
description: 'Can be a branch, a tag or a commit hash.',
}),
],
},
},
}
static render({ size }) {
return {
message: prettyBytes(size),
color: 'blue',
}
}
async fetch({ user, repo, path, branch }) {
if (branch) {
return this._requestJson({
url: `/repos/${user}/${repo}/contents/${path}?ref=${branch}`,
schema,
httpErrors: httpErrorsFor('repo, branch or file not found'),
})
} else {
return this._requestJson({
url: `/repos/${user}/${repo}/contents/${path}`,
schema,
httpErrors: httpErrorsFor('repo or file not found'),
})
}
}
async handle({ user, repo, path }, queryParams) {
const branch = queryParams.branch
const body = await this.fetch({ user, repo, path, branch })
if (Array.isArray(body)) {
throw new NotFound({ prettyMessage: 'not a regular file' })
}
return this.constructor.render({ size: body.size })
}
}