Files
shields/services/legacy-service.js
Paul Melnikow 47e8cc3de3 Refactor route functions in BaseService (#2860)
The route helper functions are fairly well isolated from the rest of BaseService, with a few convenient entry points. They are easier to test in isolation.

The way the code was written before, `pathToRegexp` was invoked once for every request, which seems inefficient.

`route` was validated when it was used, though it seems more helpful to validate it up front.

This breaks out `_makeFullUrl`, `_regex`, `_regexFromPath` into new helper functions `makeFullUrl`, `assertValidRoute`, `prepareRoute`, and `namedParamsForMatch`.

It adds validation to route, and updates the services without patterns to include one, in order to pass the new validation rules.
2019-01-26 02:38:12 -05:00

35 lines
1.1 KiB
JavaScript

'use strict'
const { BaseService } = require('.')
// This adapter allows running legacy badges in the new file layout and
// service architecture.
//
// There are some tips for rewriting legacy services:
// https://github.com/badges/shields/blob/master/doc/rewriting-services.md
//
// Do not use this for new services. New services should derive from e.g.
// BaseJsonService. Refer to the tutorial:
// https://github.com/badges/shields/blob/master/doc/TUTORIAL.md
class LegacyService extends BaseService {
// Provide a placeholder for services which do not define a route.
static get route() {
return { pattern: '' }
}
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
throw Error(`registerLegacyRouteHandler() not implemented for ${this.name}`)
}
static register({ camp, handleRequest, githubApiProvider }, serviceConfig) {
const { cacheHeaders: cacheHeaderConfig } = serviceConfig
this.registerLegacyRouteHandler({
camp,
cache: (...args) => handleRequest(cacheHeaderConfig, ...args),
githubApiProvider,
})
}
}
module.exports = LegacyService