Files
shields/services/cdnjs/cdnjs.service.js
Paul Melnikow db4bffb300 Split BaseService and BaseJsonService into separate modules (#1889)
There’s a lot of behavior here, and going to be even more, so I think it makes sense to split these up as I’ve done with the tests.
2018-08-11 10:43:05 -04:00

60 lines
1.3 KiB
JavaScript

'use strict'
const Joi = require('joi')
const BaseJsonService = require('../base-json')
const { NotFound } = require('../errors')
const { addv: versionText } = require('../../lib/text-formatters')
const { version: versionColor } = require('../../lib/color-formatters')
const cdnjsSchema = Joi.object({
// optional due to non-standard 'not found' condition
version: Joi.string(),
}).required()
module.exports = class Cdnjs extends BaseJsonService {
async handle({ library }) {
const url = `https://api.cdnjs.com/libraries/${library}?fields=version`
const json = await this._requestJson({
url,
schema: cdnjsSchema,
})
if (Object.keys(json).length === 0) {
/* Note the 'not found' response from cdnjs is:
status code = 200, body = {} */
throw new NotFound()
}
return {
message: versionText(json.version),
color: versionColor(json.version),
}
}
// Metadata
static get defaultBadgeData() {
return { label: 'cdnjs' }
}
static get category() {
return 'version'
}
static get url() {
return {
base: 'cdnjs/v',
format: '(.+)',
capture: ['library'],
}
}
static get examples() {
return [
{
previewUrl: 'jquery',
keywords: ['cdn', 'cdnjs'],
},
]
}
}