Files
shields/services/docker/docker-pulls.service.js
Caleb Cartwright e4e7b09009 Add a render helper for downloads badges, run [amo ansible apm chromewebstore clojars conda crates docker dub eclipse gem githubdownloads] (#7163)
* refactor: add render helper for downloads badges

* refactor: use new helper in some download badge classes

* doc renderer function

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2021-10-26 23:19:20 +00:00

50 lines
1.2 KiB
JavaScript

import Joi from 'joi'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'
import {
dockerBlue,
buildDockerUrl,
getDockerHubUser,
} from './docker-helpers.js'
const pullsSchema = Joi.object({
pull_count: nonNegativeInteger,
}).required()
export default 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: downloads }) {
return renderDownloadsBadge({ downloads, colorOverride: 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 })
}
}