Files
shields/services/docker/docker-stars.service.js
Caleb Cartwright 1d649fa702 convert some service classes to static props, run [depfu discourse docker] (#5540)
* refactor(depfu): convert to static props

* refactor(discourse): convert to static props

* refactor(docker): convert to static props
2020-09-14 21:44:55 +00:00

51 lines
1.2 KiB
JavaScript

'use strict'
const { metric } = require('../text-formatters')
const { nonNegativeInteger } = require('../validators')
const { BaseService } = require('..')
const {
dockerBlue,
buildDockerUrl,
getDockerHubUser,
} = require('./docker-helpers')
module.exports = class DockerStars extends BaseService {
static category = 'rating'
static route = buildDockerUrl('stars')
static examples = [
{
title: 'Docker Stars',
namedParams: {
user: '_',
repo: 'ubuntu',
},
staticPreview: this.render({ stars: 9000 }),
},
]
static defaultBadgeData = { label: 'docker stars' }
static render({ stars }) {
return {
message: metric(stars),
color: dockerBlue,
}
}
async fetch({ user, repo }) {
const url = `https://hub.docker.com/v2/repositories/${getDockerHubUser(
user
)}/${repo}/stars/count/`
const { buffer } = await this._request({
url,
errorMessages: { 404: 'repo not found' },
})
return this.constructor._validate(buffer, nonNegativeInteger)
}
async handle({ user, repo }) {
const stars = await this.fetch({ user, repo })
return this.constructor.render({ stars })
}
}