diff --git a/services/docker/docker-stars.service.js b/services/docker/docker-stars.service.js index 92cc8eb81e85b..f7e645aba19df 100644 --- a/services/docker/docker-stars.service.js +++ b/services/docker/docker-stars.service.js @@ -1,13 +1,18 @@ +import Joi from 'joi' import { metric } from '../text-formatters.js' import { nonNegativeInteger } from '../validators.js' -import { BaseService } from '../index.js' +import { BaseJsonService } from '../index.js' import { dockerBlue, buildDockerUrl, getDockerHubUser, } from './docker-helpers.js' -export default class DockerStars extends BaseService { +const schema = Joi.object({ + star_count: nonNegativeInteger.required(), +}).required() + +export default class DockerStars extends BaseJsonService { static category = 'rating' static route = buildDockerUrl('stars') static examples = [ @@ -31,18 +36,17 @@ export default class DockerStars extends BaseService { } async fetch({ user, repo }) { - const url = `https://hub.docker.com/v2/repositories/${getDockerHubUser( - user - )}/${repo}/stars/count/` - const { buffer } = await this._request({ - url, + return this._requestJson({ + schema, + url: `https://hub.docker.com/v2/repositories/${getDockerHubUser( + user + )}/${repo}/`, 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 }) + const { star_count } = await this.fetch({ user, repo }) + return this.constructor.render({ stars: star_count }) } }