Add [modrinth] followers (#8601)

* Add Modrinth followers

* Update class names
This commit is contained in:
Pierce Thompson
2022-11-08 11:27:06 -05:00
committed by GitHub
parent c214ad8de8
commit 025a49b715
4 changed files with 63 additions and 2 deletions

View File

@@ -7,7 +7,7 @@ const schema = Joi.object({
downloads: nonNegativeInteger,
}).required()
export default class Modrinth extends BaseJsonService {
export default class ModrinthDownloads extends BaseJsonService {
static category = 'downloads'
static route = {
@@ -17,7 +17,7 @@ export default class Modrinth extends BaseJsonService {
static examples = [
{
title: 'Modrinth',
title: 'Modrinth Downloads',
namedParams: { projectId: 'AANobbMI' },
staticPreview: renderDownloadsBadge({ downloads: 120000 }),
},

View File

@@ -0,0 +1,49 @@
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { BaseJsonService } from '../index.js'
import { nonNegativeInteger } from '../validators.js'
const schema = Joi.object({
followers: nonNegativeInteger,
}).required()
export default class ModrinthFollowers extends BaseJsonService {
static category = 'social'
static route = {
base: 'modrinth/followers',
pattern: ':projectId',
}
static examples = [
{
title: 'Modrinth Followers',
namedParams: { projectId: 'AANobbMI' },
staticPreview: Object.assign(this.render({ followers: 176 }), {
label: 'Followers',
style: 'social',
}),
},
]
static defaultBadgeData = { label: 'followers' }
static render({ followers }) {
return {
message: metric(followers),
color: 'blue',
}
}
async fetch({ projectId }) {
return this._requestJson({
schema,
url: `https://api.modrinth.com/v2/project/${projectId}`,
})
}
async handle({ projectId }) {
const { followers } = await this.fetch({ projectId })
return this.constructor.render({ followers })
}
}

View File

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