Files
shields/services/clojars/clojars-version.service.js
Paul Melnikow fafb22efee Move "good" badge helpers from lib/ to services/ (#3101)
This moves a few helpers from `lib/` to `services/`:

build-status.js
build-status.spec.js
color-formatters.js
color-formatters.spec.js
contributor-count.js
licenses.js
licenses.spec.js
php-version.js
php-version.spec.js
text-formatters.js
text-formatters.spec.js
version.js
version.spec.js

And one from `lib/` to `core/`:

unhandled-rejection.spec.js

The diff is long, but the changes are straightforward.

Ref #2832
2019-02-27 20:47:46 -05:00

65 lines
1.4 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { version: versionColor } = require('../color-formatters')
const { BaseJsonService, NotFound } = require('..')
const clojarsSchema = Joi.object({
// optional due to non-standard 'not found' condition
version: Joi.string(),
}).required()
module.exports = class ClojarsVersion extends BaseJsonService {
async fetch({ clojar }) {
const url = `https://clojars.org/${clojar}/latest-version.json`
return this._requestJson({
url,
schema: clojarsSchema,
})
}
static render({ clojar, version }) {
return {
message: `[${clojar} "${version}"]`,
color: versionColor(version),
}
}
async handle({ clojar }) {
const json = await this.fetch({ clojar })
if (Object.keys(json).length === 0) {
/* Note the 'not found' response from clojars is:
status code = 200, body = {} */
throw new NotFound()
}
return this.constructor.render({ clojar, version: json.version })
}
// Metadata
static get defaultBadgeData() {
return { label: 'clojars' }
}
static get category() {
return 'version'
}
static get route() {
return {
base: 'clojars/v',
pattern: ':clojar+',
}
}
static get examples() {
return [
{
namedParams: { clojar: 'prismic' },
staticPreview: this.render({ clojar: 'clojar', version: '1.2' }),
},
]
}
}