Skip to content

Commit

Permalink
add [PUB] downloads badge (#10745)
Browse files Browse the repository at this point in the history
* add PUB downloads badge

* test PUB downloads badge

* add PUB monthly downloads badge

* test PUB monthly downloads badge

* revert color override
  • Loading branch information
G1Joshi authored Dec 23, 2024
1 parent ac01fde commit 0cbc131
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
53 changes: 53 additions & 0 deletions services/pub/pub-downloads.service.js
Original file line number Diff line number Diff line change
@@ -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 changes: 28 additions & 0 deletions services/pub/pub-downloads.tester.js
Original file line number Diff line number Diff line change
@@ -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',
})

0 comments on commit 0cbc131

Please sign in to comment.