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:
47
services/modrinth/modrinth.service.js
Normal file
47
services/modrinth/modrinth.service.js
Normal 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 })
|
||||
}
|
||||
}
|
||||
12
services/modrinth/modrinth.tester.js
Normal file
12
services/modrinth/modrinth.tester.js
Normal 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' })
|
||||
Reference in New Issue
Block a user