convert some service classes to static props, run [drone dub dynamic] (#5543)

* refactor(drone): convert to static props

* refactor(dub): convert to static props

* refactor(dynamic): convert to static props
This commit is contained in:
Caleb Cartwright
2020-09-14 20:42:57 -05:00
committed by GitHub
parent 0b31461af6
commit ac54dd3ced
8 changed files with 117 additions and 196 deletions

View File

@@ -16,72 +16,58 @@ const queryParamSchema = Joi.object({
}).required() }).required()
module.exports = class DroneBuild extends BaseJsonService { module.exports = class DroneBuild extends BaseJsonService {
static get category() { static category = 'build'
return 'build' static route = {
base: 'drone/build',
pattern: ':user/:repo/:branch*',
queryParamSchema,
} }
static get route() { static auth = { passKey: 'drone_token', serviceKey: 'drone' }
return { static examples = [
queryParamSchema, {
base: 'drone/build', title: 'Drone (cloud)',
pattern: ':user/:repo/:branch*', pattern: ':user/:repo',
} namedParams: {
} user: 'drone',
repo: 'drone',
},
staticPreview: renderBuildStatusBadge({ status: 'success' }),
},
{
title: 'Drone (cloud) with branch',
pattern: ':user/:repo/:branch',
namedParams: {
user: 'drone',
repo: 'drone',
branch: 'master',
},
staticPreview: renderBuildStatusBadge({ status: 'success' }),
},
{
title: 'Drone (self-hosted)',
pattern: ':user/:repo',
queryParams: { server: 'https://drone.shields.io' },
namedParams: {
user: 'badges',
repo: 'shields',
},
staticPreview: renderBuildStatusBadge({ status: 'success' }),
},
{
title: 'Drone (self-hosted) with branch',
pattern: ':user/:repo/:branch',
queryParams: { server: 'https://drone.shields.io' },
namedParams: {
user: 'badges',
repo: 'shields',
branch: 'feat/awesome-thing',
},
staticPreview: renderBuildStatusBadge({ status: 'success' }),
},
]
static get auth() { static defaultBadgeData = { label: 'build' }
return { passKey: 'drone_token', serviceKey: 'drone' }
}
static get examples() {
return [
{
title: 'Drone (cloud)',
pattern: ':user/:repo',
namedParams: {
user: 'drone',
repo: 'drone',
},
staticPreview: renderBuildStatusBadge({ status: 'success' }),
},
{
title: 'Drone (cloud) with branch',
pattern: ':user/:repo/:branch',
namedParams: {
user: 'drone',
repo: 'drone',
branch: 'master',
},
staticPreview: renderBuildStatusBadge({ status: 'success' }),
},
{
title: 'Drone (self-hosted)',
pattern: ':user/:repo',
queryParams: { server: 'https://drone.shields.io' },
namedParams: {
user: 'badges',
repo: 'shields',
},
staticPreview: renderBuildStatusBadge({ status: 'success' }),
},
{
title: 'Drone (self-hosted) with branch',
pattern: ':user/:repo/:branch',
queryParams: { server: 'https://drone.shields.io' },
namedParams: {
user: 'badges',
repo: 'shields',
branch: 'feat/awesome-thing',
},
staticPreview: renderBuildStatusBadge({ status: 'success' }),
},
]
}
static get defaultBadgeData() {
return {
label: 'build',
}
}
async handle({ user, repo, branch }, { server = 'https://cloud.drone.io' }) { async handle({ user, repo, branch }, { server = 'https://cloud.drone.io' }) {
const json = await this._requestJson( const json = await this._requestJson(

View File

@@ -35,56 +35,47 @@ const intervalMap = {
} }
module.exports = class DubDownloads extends BaseJsonService { module.exports = class DubDownloads extends BaseJsonService {
static get category() { static category = 'downloads'
return 'downloads' static route = {
base: 'dub',
pattern: ':interval(dd|dw|dm|dt)/:packageName/:version*',
} }
static get route() { static examples = [
return { {
base: 'dub', title: 'DUB',
pattern: ':interval(dd|dw|dm|dt)/:packageName/:version*', namedParams: { interval: 'dm', packageName: 'vibe-d' },
} staticPreview: this.render({ interval: 'dm', downloadCount: 5000 }),
} },
{
title: 'DUB (version)',
namedParams: {
interval: 'dm',
packageName: 'vibe-d',
version: '0.8.4',
},
staticPreview: this.render({
interval: 'dm',
version: '0.8.4',
downloadCount: 100,
}),
},
{
title: 'DUB (latest)',
namedParams: {
interval: 'dm',
packageName: 'vibe-d',
version: 'latest',
},
staticPreview: this.render({
interval: 'dm',
version: 'latest',
downloadCount: 100,
}),
},
]
static get examples() { static defaultBadgeData = { label: 'downloads' }
return [
{
title: 'DUB',
namedParams: { interval: 'dm', packageName: 'vibe-d' },
staticPreview: this.render({ interval: 'dm', downloadCount: 5000 }),
},
{
title: 'DUB (version)',
namedParams: {
interval: 'dm',
packageName: 'vibe-d',
version: '0.8.4',
},
staticPreview: this.render({
interval: 'dm',
version: '0.8.4',
downloadCount: 100,
}),
},
{
title: 'DUB (latest)',
namedParams: {
interval: 'dm',
packageName: 'vibe-d',
version: 'latest',
},
staticPreview: this.render({
interval: 'dm',
version: 'latest',
downloadCount: 100,
}),
},
]
}
static get defaultBadgeData() {
return { label: 'downloads' }
}
static render({ interval, version, downloadCount }) { static render({ interval, version, downloadCount }) {
const { messageSuffix } = intervalMap[interval] const { messageSuffix } = intervalMap[interval]

View File

@@ -9,30 +9,17 @@ const schema = Joi.object({
}) })
module.exports = class DubLicense extends BaseJsonService { module.exports = class DubLicense extends BaseJsonService {
static get category() { static category = 'license'
return 'license' static route = { base: 'dub/l', pattern: ':packageName' }
} static examples = [
{
title: 'DUB',
namedParams: { packageName: 'vibe-d' },
staticPreview: renderLicenseBadge({ licenses: ['MIT'] }),
},
]
static get route() { static defaultBadgeData = { label: 'license' }
return {
base: 'dub/l',
pattern: ':packageName',
}
}
static get examples() {
return [
{
title: 'DUB',
namedParams: { packageName: 'vibe-d' },
staticPreview: renderLicenseBadge({ licenses: ['MIT'] }),
},
]
}
static get defaultBadgeData() {
return { label: 'license' }
}
async fetch({ packageName }) { async fetch({ packageName }) {
return this._requestJson({ return this._requestJson({

View File

@@ -7,30 +7,17 @@ const { BaseJsonService } = require('..')
const schema = Joi.string().required() const schema = Joi.string().required()
module.exports = class DubVersion extends BaseJsonService { module.exports = class DubVersion extends BaseJsonService {
static get category() { static category = 'version'
return 'version' static route = { base: 'dub/v', pattern: ':packageName' }
} static examples = [
{
title: 'DUB',
namedParams: { packageName: 'vibe-d' },
staticPreview: renderVersionBadge({ version: 'v0.8.4' }),
},
]
static get route() { static defaultBadgeData = { label: 'dub' }
return {
base: 'dub/v',
pattern: ':packageName',
}
}
static get examples() {
return [
{
title: 'DUB',
namedParams: { packageName: 'vibe-d' },
staticPreview: renderVersionBadge({ version: 'v0.8.4' }),
},
]
}
static get defaultBadgeData() {
return { label: 'dub' }
}
async fetch({ packageName }) { async fetch({ packageName }) {
return this._requestJson({ return this._requestJson({

View File

@@ -6,13 +6,8 @@ const { createRoute } = require('./dynamic-helpers')
const jsonPath = require('./json-path') const jsonPath = require('./json-path')
module.exports = class DynamicJson extends jsonPath(BaseJsonService) { module.exports = class DynamicJson extends jsonPath(BaseJsonService) {
static get enabledMetrics() { static enabledMetrics = [MetricNames.SERVICE_RESPONSE_SIZE]
return [MetricNames.SERVICE_RESPONSE_SIZE] static route = createRoute('json')
}
static get route() {
return createRoute('json')
}
async fetch({ schema, url, errorMessages }) { async fetch({ schema, url, errorMessages }) {
return this._requestJson({ return this._requestJson({

View File

@@ -14,23 +14,10 @@ const { createRoute } = require('./dynamic-helpers')
// JSON parser and write the queries in jsonpath instead. Then eventually // JSON parser and write the queries in jsonpath instead. Then eventually
// deprecate the old version. // deprecate the old version.
module.exports = class DynamicXml extends BaseService { module.exports = class DynamicXml extends BaseService {
static get category() { static category = 'dynamic'
return 'dynamic' static enabledMetrics = [MetricNames.SERVICE_RESPONSE_SIZE]
} static route = createRoute('xml')
static defaultBadgeData = { label: 'custom badge' }
static get enabledMetrics() {
return [MetricNames.SERVICE_RESPONSE_SIZE]
}
static get route() {
return createRoute('xml')
}
static get defaultBadgeData() {
return {
label: 'custom badge',
}
}
transform({ pathExpression, buffer }) { transform({ pathExpression, buffer }) {
// e.g. //book[2]/@id // e.g. //book[2]/@id

View File

@@ -6,13 +6,8 @@ const { createRoute } = require('./dynamic-helpers')
const jsonPath = require('./json-path') const jsonPath = require('./json-path')
module.exports = class DynamicYaml extends jsonPath(BaseYamlService) { module.exports = class DynamicYaml extends jsonPath(BaseYamlService) {
static get enabledMetrics() { static enabledMetrics = [MetricNames.SERVICE_RESPONSE_SIZE]
return [MetricNames.SERVICE_RESPONSE_SIZE] static route = createRoute('yaml')
}
static get route() {
return createRoute('yaml')
}
async fetch({ schema, url, errorMessages }) { async fetch({ schema, url, errorMessages }) {
return this._requestYaml({ return this._requestYaml({

View File

@@ -17,15 +17,8 @@ const { InvalidParameter, InvalidResponse } = require('..')
*/ */
module.exports = superclass => module.exports = superclass =>
class extends superclass { class extends superclass {
static get category() { static category = 'dynamic'
return 'dynamic' static defaultBadgeData = { label: 'custom badge' }
}
static get defaultBadgeData() {
return {
label: 'custom badge',
}
}
/** /**
* Request data from an upstream API, transform it to JSON and validate against a schema * Request data from an upstream API, transform it to JSON and validate against a schema