mirror of
https://github.com/badges/shields.git
synced 2025-04-18 19:44:04 +03:00
add [PUB] downloads badge (#10745)
* add PUB downloads badge * test PUB downloads badge * add PUB monthly downloads badge * test PUB monthly downloads badge * revert color override
This commit is contained in:
parent
ac01fdefcc
commit
0cbc1319ce
53
services/pub/pub-downloads.service.js
Normal file
53
services/pub/pub-downloads.service.js
Normal file
@ -0,0 +1,53 @@
|
||||
import Joi from 'joi'
|
||||
import { BaseJsonService, pathParams } from '../index.js'
|
||||
import { renderDownloadsBadge } from '../downloads.js'
|
||||
import { nonNegativeInteger } from '../validators.js'
|
||||
import { baseDescription } from './pub-common.js'
|
||||
|
||||
const description = `${baseDescription}
|
||||
<p>This badge shows a measure of how many developers have downloaded a package monthly.</p>`
|
||||
|
||||
const schema = Joi.object({
|
||||
downloadCount30Days: nonNegativeInteger,
|
||||
}).required()
|
||||
|
||||
export default class PubDownloads extends BaseJsonService {
|
||||
static category = 'downloads'
|
||||
|
||||
static route = { base: 'pub/dm', pattern: ':packageName' }
|
||||
|
||||
static openApi = {
|
||||
'/pub/dm/{packageName}': {
|
||||
get: {
|
||||
summary: 'Pub Monthly Downloads',
|
||||
description,
|
||||
parameters: pathParams({
|
||||
name: 'packageName',
|
||||
example: 'analysis_options',
|
||||
}),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
static defaultBadgeData = { label: 'downloads' }
|
||||
|
||||
static render({ downloadCount30Days }) {
|
||||
return renderDownloadsBadge({
|
||||
downloads: downloadCount30Days,
|
||||
interval: 'month',
|
||||
})
|
||||
}
|
||||
|
||||
async fetch({ packageName }) {
|
||||
return this._requestJson({
|
||||
schema,
|
||||
url: `https://pub.dev/api/packages/${packageName}/score`,
|
||||
})
|
||||
}
|
||||
|
||||
async handle({ packageName }) {
|
||||
const score = await this.fetch({ packageName })
|
||||
const downloadCount30Days = score.downloadCount30Days
|
||||
return this.constructor.render({ downloadCount30Days })
|
||||
}
|
||||
}
|
28
services/pub/pub-downloads.tester.js
Normal file
28
services/pub/pub-downloads.tester.js
Normal file
@ -0,0 +1,28 @@
|
||||
import { isMetricOverTimePeriod } from '../test-validators.js'
|
||||
import { createServiceTester } from '../tester.js'
|
||||
|
||||
export const t = await createServiceTester()
|
||||
|
||||
t.create('pub monthly downloads (valid)')
|
||||
.get('/analysis_options.json')
|
||||
.expectBadge({
|
||||
label: 'downloads',
|
||||
message: isMetricOverTimePeriod,
|
||||
color: 'green',
|
||||
})
|
||||
|
||||
t.create('pub monthly downloads (not found)')
|
||||
.get('/analysisoptions.json')
|
||||
.expectBadge({
|
||||
label: 'downloads',
|
||||
message: 'not found',
|
||||
color: 'red',
|
||||
})
|
||||
|
||||
t.create('pub monthly downloads (invalid)')
|
||||
.get('/analysis-options.json')
|
||||
.expectBadge({
|
||||
label: 'downloads',
|
||||
message: 'invalid',
|
||||
color: 'lightgrey',
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user