-
-
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 PUB downloads badge * test PUB downloads badge * add PUB monthly downloads badge * test PUB monthly downloads badge * revert color override
- Loading branch information
Showing
2 changed files
with
81 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,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 }) | ||
} | ||
} |
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,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', | ||
}) |