Files
shields/services/docker/docker-pulls.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

55 lines
1.2 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { metric } = require('../text-formatters')
const { nonNegativeInteger } = require('../validators')
const { BaseJsonService } = require('..')
const {
dockerBlue,
buildDockerUrl,
getDockerHubUser,
} = require('./docker-helpers')
const pullsSchema = Joi.object({
pull_count: nonNegativeInteger,
}).required()
module.exports = class DockerPulls extends BaseJsonService {
static category = 'downloads'
static route = buildDockerUrl('pulls')
static examples = [
{
title: 'Docker Pulls',
namedParams: {
user: '_',
repo: 'ubuntu',
},
staticPreview: this.render({ count: 765400000 }),
},
]
static defaultBadgeData = { label: 'docker pulls' }
static render({ count }) {
return {
message: metric(count),
color: dockerBlue,
}
}
async fetch({ user, repo }) {
return this._requestJson({
schema: pullsSchema,
url: `https://hub.docker.com/v2/repositories/${getDockerHubUser(
user
)}/${repo}`,
errorMessages: { 404: 'repo not found' },
})
}
async handle({ user, repo }) {
const data = await this.fetch({ user, repo })
return this.constructor.render({ count: data.pull_count })
}
}