Skip to content

Commit

Permalink
Add [Modrinth] total downloads badge (#7132)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
dominikbrandon and repo-ranger[bot] committed Oct 10, 2021
1 parent 887ec5b commit 5b5ffce
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
47 changes: 47 additions & 0 deletions services/modrinth/modrinth.service.js
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions services/modrinth/modrinth.tester.js
Original file line number Diff line number Diff line change
@@ -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' })

0 comments on commit 5b5ffce

Please sign in to comment.