Skip to content

Commit

Permalink
refactor [pub] service (#2942)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s authored and paulmelnikow committed Feb 6, 2019
1 parent 44c891a commit 4a8a1f2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 55 deletions.
87 changes: 36 additions & 51 deletions services/pub/pub.service.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
'use strict'

const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const { addv: versionText } = require('../../lib/text-formatters')
const { version: versionColor } = require('../../lib/color-formatters')
const Joi = require('joi')
const { BaseJsonService } = require('..')
const { renderVersionBadge } = require('../../lib/version')
const { latest: latestVersion } = require('../../lib/version')

// For Dart's pub.
//
// This legacy service should be rewritten to use e.g. BaseJsonService.
//
// Tips for rewriting:
// https://github.com/badges/shields/blob/master/doc/rewriting-services.md
//
// Do not base new services on this code.
module.exports = class Pub extends LegacyService {
const schema = Joi.object({
versions: Joi.array()
.items(Joi.string())
.required(),
}).required()

module.exports = class PubVersion extends BaseJsonService {
static get category() {
return 'version'
}
Expand All @@ -26,53 +23,41 @@ module.exports = class Pub extends LegacyService {
}
}

static get defaultBadgeData() {
return { label: 'pub' }
}

static get examples() {
return [
{
title: 'Pub',
pattern: 'v/:packageName',
namedParams: {
packageName: 'box2d',
},
staticPreview: {
label: 'pub',
message: 'v0.4.0',
color: 'orange',
},
namedParams: { packageName: 'box2d' },
staticPreview: renderVersionBadge({ version: 'v0.4.0' }),
keywords: ['dart', 'dartlang'],
},
{
title: 'Pub',
pattern: 'vpre/:packageName',
namedParams: { packageName: 'box2d' },
staticPreview: renderVersionBadge({ version: 'v0.4.0' }),
keywords: ['dart', 'dartlang'],
},
]
}

static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/pub\/v(pre)?\/(.*)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const includePre = Boolean(match[1])
const userRepo = match[2] // eg, "box2d"
const format = match[3]
const apiUrl = `https://pub.dartlang.org/packages/${userRepo}.json`
const badgeData = getBadgeData('pub', data)
request(apiUrl, (err, res, buffer) => {
if (err != null) {
badgeData.text[1] = 'inaccessible'
sendBadge(format, badgeData)
return
}
try {
const data = JSON.parse(buffer)
// Grab the latest stable version, or an unstable
const versions = data.versions
const version = latestVersion(versions, { pre: includePre })
badgeData.text[1] = versionText(version)
badgeData.colorscheme = versionColor(version)
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
async fetch({ packageName }) {
return this._requestJson({
schema,
url: `https://pub.dartlang.org/packages/${packageName}.json`,
})
}

async handle({ which, packageName }) {
const data = await this.fetch({ packageName })
const includePre = which === 'vpre'
const versions = data.versions
const version = latestVersion(versions, { pre: includePre })
return renderVersionBadge({ version })
}
}
15 changes: 11 additions & 4 deletions services/pub/pub.tester.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'

const Joi = require('joi')
const { ServiceTester } = require('../tester')
const { isVPlusTripleDottedVersion } = require('../test-validators')

const t = (module.exports = new ServiceTester({ id: 'pub', title: 'Pub' }))
const t = (module.exports = require('../tester').createServiceTester())

t.create('package version')
.get('/v/box2d.json')
Expand All @@ -15,9 +13,18 @@ t.create('package version')
})
)

t.create('package pre-release version')
.get('/vpre/box2d.json')
.expectJSONTypes(
Joi.object().keys({
name: 'pub',
value: isVPlusTripleDottedVersion,
})
)

t.create('package not found')
.get('/v/does-not-exist.json')
.expectJSON({
name: 'pub',
value: 'invalid',
value: 'not found',
})

0 comments on commit 4a8a1f2

Please sign in to comment.