Files
shields/services/cdnjs/cdnjs.service.js
chris48s b2f47a3303 migrate examples to openApi part 2; affects [archlinux bitcomponents bountysource cdnjs chrome clearlydefined clojars cocoapods coincap] (#9428)
* convert an example that doesn't matter

* migrate some services from examples to openApi

* improve and de-dupe service titles

* revert changes to codefactor
2023-08-08 23:57:47 +00:00

52 lines
1.2 KiB
JavaScript

import Joi from 'joi'
import { renderVersionBadge } from '../version.js'
import { BaseJsonService, NotFound, pathParams } from '../index.js'
const cdnjsSchema = Joi.object({
// optional due to non-standard 'not found' condition
version: Joi.string(),
}).required()
export default class Cdnjs extends BaseJsonService {
static category = 'version'
static route = { base: 'cdnjs/v', pattern: ':library' }
static openApi = {
'/cdnjs/v/{library}': {
get: {
summary: 'Cdnjs',
parameters: pathParams({
name: 'library',
example: 'jquery',
}),
},
},
}
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 })
}
}