The NuGet badge examples are straggling in all-badge-examples. Rather than move them as is, I thought it made more sense to refactor the services and see if they could be generated. I didn't take that on here; this is a straight rewrite of the badges. The old implementations were fairly difficult to follow. The new implementations are complicated too, though I hope much more readable. Though the NuGet behaviors could be consolidated into a single flag, I split `withTenant` and `withFeed` into separate flags, thinking naming the behaviors makes the implementations easier to understand. I defaulted these to true, thinking that really this is really a MyGet implementation which is generalized to NuGet. Though maybe it makes more sense to have the MyGet style as the default. Probably it doesn't matter much either way. I added a helper class ServiceUrlBuilder to construct the Shields service URL. It's useful in this complex case where the URL must be built up conditionally. This might be useful in a couple other places. I also wrote a new service to handle the Powershell badges. They've diverged a little bit from the Nuget v2. There's a bit of shared code which I factored out. If the XML Nuget APIs are more reliable, we could consider switching everything else over to them, though for now I would like to get this merged and get #2078 fixed. Fix #2078
120 lines
2.6 KiB
JavaScript
120 lines
2.6 KiB
JavaScript
'use strict'
|
|
|
|
const Joi = require('joi')
|
|
|
|
const BaseXmlService = require('../base-xml')
|
|
const { NotFound } = require('../errors')
|
|
const { nonNegativeInteger } = require('../validators')
|
|
const { createFilter } = require('../nuget/nuget-v2-service-family')
|
|
const {
|
|
renderVersionBadge,
|
|
renderDownloadBadge,
|
|
} = require('../nuget/nuget-helpers')
|
|
|
|
const schema = Joi.object({
|
|
feed: Joi.object({
|
|
entry: Joi.object({
|
|
'm:properties': Joi.object({
|
|
'd:Version': Joi.string(),
|
|
'd:NormalizedVersion': Joi.string(),
|
|
'd:DownloadCount': nonNegativeInteger,
|
|
}),
|
|
}),
|
|
}).required(),
|
|
}).required()
|
|
|
|
async function fetch(
|
|
serviceInstance,
|
|
{ packageName, includePrereleases = false }
|
|
) {
|
|
const data = await serviceInstance._requestXml({
|
|
schema,
|
|
url: `https://www.powershellgallery.com/api/v2/Search()`,
|
|
options: {
|
|
qs: { $filter: createFilter({ packageName, includePrereleases }) },
|
|
},
|
|
})
|
|
|
|
const packageData =
|
|
'entry' in data.feed ? data.feed.entry['m:properties'] : undefined
|
|
|
|
if (packageData) {
|
|
return packageData
|
|
} else if (!includePrereleases) {
|
|
return fetch(serviceInstance, {
|
|
packageName,
|
|
includePrereleases: true,
|
|
})
|
|
} else {
|
|
throw new NotFound()
|
|
}
|
|
}
|
|
|
|
class PowershellGalleryVersion extends BaseXmlService {
|
|
static get category() {
|
|
return 'version'
|
|
}
|
|
|
|
static get route() {
|
|
return {
|
|
base: 'powershellgallery',
|
|
pattern: ':which(v|vpre)/:packageName',
|
|
}
|
|
}
|
|
|
|
static get examples() {
|
|
return []
|
|
}
|
|
|
|
static get defaultBadgeData() {
|
|
return {
|
|
label: 'powershell gallery',
|
|
}
|
|
}
|
|
|
|
static render(props) {
|
|
return renderVersionBadge(props)
|
|
}
|
|
|
|
async handle({ which, packageName }) {
|
|
const packageData = await fetch(this, {
|
|
packageName,
|
|
includePrereleases: which === 'vpre',
|
|
})
|
|
const version =
|
|
packageData['d:NormalizedVersion'] || packageData['d:Version']
|
|
return this.constructor.render({ version })
|
|
}
|
|
}
|
|
|
|
class PowershellGalleryDownloads extends BaseXmlService {
|
|
static get category() {
|
|
return 'downloads'
|
|
}
|
|
|
|
static get route() {
|
|
return {
|
|
base: 'powershellgallery',
|
|
pattern: 'dt/:packageName',
|
|
}
|
|
}
|
|
|
|
static get examples() {
|
|
return []
|
|
}
|
|
|
|
static render(props) {
|
|
return renderDownloadBadge(props)
|
|
}
|
|
|
|
async handle({ packageName }) {
|
|
const packageData = await fetch(this, {
|
|
packageName,
|
|
})
|
|
const { 'd:DownloadCount': downloads } = packageData
|
|
return this.constructor.render({ downloads })
|
|
}
|
|
}
|
|
|
|
module.exports = { PowershellGalleryVersion, PowershellGalleryDownloads }
|