-
-
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.
[Pulsar] Add Pulsar Badges for Stargazers & Downloads (#8767)
* Added pulsar-edit Downloads & Stargazers * Manage colour similar to how Docker Badges do * Fixed typo in colour * Or it seems no other (not found) tests check colour, likely overrided elsewhere * Remove usage of 'Edit' * errorMessages => httpErrors && downloads => dt --------- Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
- Loading branch information
1 parent
96e9e13
commit d73a5eb
Showing
5 changed files
with
139 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 { nonNegativeInteger } from '../validators.js' | ||
import { pulsarPurple } from './pulsar-helper.js' | ||
|
||
const schema = Joi.object({ | ||
downloads: nonNegativeInteger, | ||
}) | ||
|
||
export default class PulsarDownloads extends BaseJsonService { | ||
static category = 'downloads' | ||
|
||
static route = { base: 'pulsar/dt', pattern: ':packageName' } | ||
|
||
static examples = [ | ||
{ | ||
title: 'Pulsar Downloads', | ||
namedParams: { packageName: 'hey-pane' }, | ||
staticPreview: this.render({ downloadCount: 1000 }), | ||
}, | ||
] | ||
|
||
static defaultBadgeData = { label: 'downloads' } | ||
|
||
static render({ downloadCount }) { | ||
return { | ||
label: 'downloads', | ||
message: metric(downloadCount), | ||
color: pulsarPurple, | ||
} | ||
} | ||
|
||
async fetch({ packageName }) { | ||
return this._requestJson({ | ||
schema, | ||
url: `https://api.pulsar-edit.dev/api/packages/${packageName}`, | ||
httpErrors: { 404: 'package not found' }, | ||
}) | ||
} | ||
|
||
async handle({ packageName }) { | ||
const packageData = await this.fetch({ packageName }) | ||
const downloadCount = packageData.downloads | ||
return this.constructor.render({ downloadCount }) | ||
} | ||
} |
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,18 @@ | ||
import { isMetric } from '../test-validators.js' | ||
import { createServiceTester } from '../tester.js' | ||
import { pulsarPurple } from './pulsar-helper.js' | ||
|
||
export const t = await createServiceTester() | ||
|
||
t.create('pulsar downloads (valid)') | ||
.get('/hey-pane.json') | ||
.expectBadge({ | ||
label: 'downloads', | ||
message: isMetric, | ||
color: `#${pulsarPurple}`, | ||
}) | ||
|
||
t.create('pulsar downloads (not found)').get('/test-package.json').expectBadge({ | ||
label: 'downloads', | ||
message: 'package not found', | ||
}) |
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,7 @@ | ||
// This is based on the format the Docker badges have taken. | ||
// Seems Tests require `#` before colors, whereas the badges do not. | ||
// So a color variable can be exported for all modules to use as needed. | ||
|
||
const pulsarPurple = '662d91' | ||
|
||
export { pulsarPurple } |
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 { nonNegativeInteger } from '../validators.js' | ||
import { pulsarPurple } from './pulsar-helper.js' | ||
|
||
const schema = Joi.object({ | ||
stargazers_count: nonNegativeInteger, | ||
}) | ||
|
||
export default class PulsarStargazers extends BaseJsonService { | ||
static category = 'rating' | ||
|
||
static route = { base: 'pulsar/stargazers', pattern: ':packageName' } | ||
|
||
static examples = [ | ||
{ | ||
title: 'Pulsar Stargazers', | ||
namedParams: { packageName: 'hey-pane' }, | ||
staticPreview: this.render({ stargazerCount: 1000 }), | ||
}, | ||
] | ||
|
||
static defaultBadgeData = { label: 'stargazers' } | ||
|
||
static render({ stargazerCount }) { | ||
return { | ||
label: 'stargazers', | ||
message: metric(stargazerCount), | ||
color: pulsarPurple, | ||
} | ||
} | ||
|
||
async fetch({ packageName }) { | ||
return this._requestJson({ | ||
schema, | ||
url: `https://api.pulsar-edit.dev/api/packages/${packageName}`, | ||
httpErrors: { 404: 'package not found' }, | ||
}) | ||
} | ||
|
||
async handle({ packageName }) { | ||
const packageData = await this.fetch({ packageName }) | ||
const stargazerCount = packageData.stargazers_count | ||
return this.constructor.render({ stargazerCount }) | ||
} | ||
} |
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,20 @@ | ||
import { isMetric } from '../test-validators.js' | ||
import { createServiceTester } from '../tester.js' | ||
import { pulsarPurple } from './pulsar-helper.js' | ||
|
||
export const t = await createServiceTester() | ||
|
||
t.create('pulsar stargazers (valid)') | ||
.get('/hey-pane.json') | ||
.expectBadge({ | ||
label: 'stargazers', | ||
message: isMetric, | ||
color: `#${pulsarPurple}`, | ||
}) | ||
|
||
t.create('pulsar stargazers (not found)') | ||
.get('/test-package.json') | ||
.expectBadge({ | ||
label: 'stargazers', | ||
message: 'package not found', | ||
}) |