* add dub score badge service * add examples for isWithinRange test validator * use Joi to validate range instead of regex * change labels to lowercase
43 lines
1022 B
JavaScript
43 lines
1022 B
JavaScript
import Joi from 'joi'
|
|
import { BaseJsonService } from '../index.js'
|
|
import { colorScale } from '../color-formatters.js'
|
|
|
|
const schema = Joi.object({
|
|
score: Joi.number().required(),
|
|
})
|
|
|
|
export default class DubScore extends BaseJsonService {
|
|
static category = 'rating'
|
|
|
|
static route = { base: 'dub/score', pattern: ':packageName' }
|
|
|
|
static examples = [
|
|
{
|
|
title: 'DUB Score',
|
|
namedParams: { packageName: 'vibe-d' },
|
|
staticPreview: this.render({ score: 4.5 }),
|
|
},
|
|
]
|
|
|
|
static defaultBadgeData = { label: 'score' }
|
|
|
|
static render({ score }) {
|
|
return {
|
|
label: 'score',
|
|
color: colorScale([1, 2, 3, 4, 5])(score),
|
|
message: score,
|
|
}
|
|
}
|
|
|
|
async fetch({ packageName }) {
|
|
const url = `https://code.dlang.org/api/packages/${packageName}/stats`
|
|
return this._requestJson({ schema, url })
|
|
}
|
|
|
|
async handle({ packageName }) {
|
|
let { score } = await this.fetch({ packageName })
|
|
score = score.toFixed(1)
|
|
return this.constructor.render({ score })
|
|
}
|
|
}
|