-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
887ec5b
commit 5b5ffce
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) |