Files
shields/services/cdnjs/cdnjs.service.js
Caleb Cartwright a88302eafb convert some service classes to static props, run [cdnjs chromewebstore] (#5509)
* refactor(cdnjs): convert to static props

* refactor(chromewebstore): convert to static props

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2020-09-12 16:04:20 +00:00

49 lines
1.2 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { renderVersionBadge } = require('../version')
const { BaseJsonService, NotFound } = require('..')
const cdnjsSchema = Joi.object({
// optional due to non-standard 'not found' condition
version: Joi.string(),
}).required()
module.exports = class Cdnjs extends BaseJsonService {
static category = 'version'
static route = { base: 'cdnjs/v', pattern: ':library' }
static examples = [
{
namedParams: { library: 'jquery' },
staticPreview: this.render({ version: '1.5.2' }),
},
]
static defaultBadgeData = { label: 'cdnjs' }
static render({ version }) {
return renderVersionBadge({ version })
}
async fetch({ library }) {
const url = `https://api.cdnjs.com/libraries/${library}?fields=version`
return this._requestJson({
url,
schema: cdnjsSchema,
})
}
async handle({ library }) {
const json = await this.fetch({ library })
if (Object.keys(json).length === 0) {
/* Note the 'not found' response from cdnjs is:
status code = 200, body = {} */
throw new NotFound()
}
return this.constructor.render({ version: json.version })
}
}