Files
shields/services/route-builder.js
Paul Melnikow 5e99aad2de Rewrite [NuGet] badges including [myget chocolatey resharper powershellgallery] (#2257)
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
2018-11-14 17:28:15 -05:00

37 lines
880 B
JavaScript

'use strict'
const { toArray } = require('../lib/badge-data')
/*
* Factory class for building a BaseService `route` object. This class is useful
* in complex collections of service classes, when the URL is built
* conditionally.
*
* Patterns based on path-to-regex may obviate the need for this, though they
* haven't done so yet.
*/
module.exports = class RouteBuilder {
constructor({ base = '' } = {}) {
this.base = base
this._formatComponents = []
this.capture = []
}
get format() {
return this._formatComponents.join('/')
}
push(format, capture) {
this._formatComponents = this._formatComponents.concat(toArray(format))
this.capture = this.capture.concat(toArray(capture))
// Return `this` for chaining.
return this
}
toObject() {
const { base, format, capture } = this
return { base, format, capture }
}
}