Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add [PUB] downloads badge #10745

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions services/pub/pub-downloads.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Joi from 'joi'
import { BaseJsonService, pathParams } from '../index.js'
import { metric } from '../text-formatters.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. This provides a raw measure of the overall sentiment of a package from peer developers.</p>`
G1Joshi marked this conversation as resolved.
Show resolved Hide resolved

const schema = Joi.object({
downloadCount30Days: nonNegativeInteger,
}).required()

export default class PubDownloads extends BaseJsonService {
static category = 'downloads'

static route = { base: 'pub/downloads', pattern: ':packageName' }

static openApi = {
'/pub/downloads/{packageName}': {
G1Joshi marked this conversation as resolved.
Show resolved Hide resolved
get: {
summary: 'Pub Downloads',
G1Joshi marked this conversation as resolved.
Show resolved Hide resolved
description,
parameters: pathParams({
name: 'packageName',
example: 'analysis_options',
}),
},
},
}

static defaultBadgeData = { label: 'downloads' }

static render({ downloadCount30Days }) {
return {
label: 'downloads',
message: metric(downloadCount30Days),
color: 'blue',
}
G1Joshi marked this conversation as resolved.
Show resolved Hide resolved
}

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 })
}
}
22 changes: 22 additions & 0 deletions services/pub/pub-downloads.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

t.create('pub downloads (valid)').get('/analysis_options.json').expectBadge({
label: 'downloads',
message: isMetric,
color: 'blue',
})

t.create('pub downloads (not found)').get('/analysisoptions.json').expectBadge({
label: 'downloads',
message: 'not found',
color: 'red',
})

t.create('pub downloads (invalid)').get('/analysis-options.json').expectBadge({
label: 'downloads',
message: 'invalid',
color: 'lightgrey',
})
Loading