diff --git a/services/modrinth/modrinth.service.js b/services/modrinth/modrinth.service.js new file mode 100644 index 0000000000000..25fdbdb9882ae --- /dev/null +++ b/services/modrinth/modrinth.service.js @@ -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 }) + } +} diff --git a/services/modrinth/modrinth.tester.js b/services/modrinth/modrinth.tester.js new file mode 100644 index 0000000000000..26ac709ab67fd --- /dev/null +++ b/services/modrinth/modrinth.tester.js @@ -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' })