Add [Modrinth] total downloads badge (#7132)

* Add [Modrinth] total downloads badge

* Check that [Modrinth] downloads value is non-negative

* Remove unnecessary test for negative downloads value

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
Dominik Grzelak
2021-10-10 20:43:10 +02:00
committed by GitHub
parent 887ec5b441
commit 5b5ffce5b2
2 changed files with 59 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
import Joi from 'joi'
import { BaseJsonService } from '../index.js'
import { metric } from '../text-formatters.js'
import { downloadCount as downloadCountColor } from '../color-formatters.js'
import { nonNegativeInteger } from '../validators.js'
const schema = Joi.object({
downloads: nonNegativeInteger,
}).required()
export default class Modrinth extends BaseJsonService {
static category = 'downloads'
static route = {
base: 'modrinth/dt',
pattern: ':modId',
}
static examples = [
{
title: 'Modrinth',
namedParams: { modId: 'AANobbMI' },
staticPreview: this.render({ downloads: 120000 }),
},
]
static defaultBadgeData = { label: 'downloads' }
static render({ downloads }) {
return {
message: metric(downloads),
color: downloadCountColor(downloads),
}
}
async fetch({ modId }) {
return this._requestJson({
schema,
url: `https://api.modrinth.com/api/v1/mod/${modId}`,
})
}
async handle({ modId }) {
const { downloads } = await this.fetch({ modId })
return this.constructor.render({ downloads })
}
}

View File

@@ -0,0 +1,12 @@
import { createServiceTester } from '../tester.js'
import { isMetric } from '../test-validators.js'
export const t = await createServiceTester()
t.create('Downloads')
.get('/AANobbMI.json')
.expectBadge({ label: 'downloads', message: isMetric })
t.create('Downloads (not found)')
.get('/not-existing.json')
.expectBadge({ label: 'downloads', message: 'not found', color: 'red' })