Files
shields/services/dub/dub-score.service.js
Prashant Rawat 015b5b02d0 Add [dub] score badge service (#9549)
* add dub score badge service

* add examples for isWithinRange test validator

* use Joi to validate range instead of regex

* change labels to lowercase
2023-09-10 11:41:23 +00:00

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 })
}
}