Refactor [Wordpress] services (#3324)

- Prefer inline transforms to take place in `handle()` rather than `render()`
- Avoid inversion of control by removing `BaseWordpress#handle()`, passing `extensionType` into `fetch()`, and removing one layer of subclassing
- Move “not found” checks into `fetch()`
- Cache wordpress versions instead of fetching on each request
- Start to convert aliases to redirects (there are more of these which could be tackled in a follow-on)
- Replace at least one route `format` with a `pattern` (ref #3329)
- Partially reorder: name, category, route, examples, defaultBadgeData, render, fetch, handle

Some of this is in line with our established patterns or makes it clearly easier to follow; some of it is arguably stylistic.
This commit is contained in:
Paul Melnikow
2019-04-22 10:45:25 -05:00
committed by GitHub
parent 05af1f82b2
commit 5828223356
9 changed files with 206 additions and 169 deletions

View File

@@ -21,20 +21,14 @@ const notFoundSchema = Joi.string().allow(null, false)
const schemas = Joi.alternatives(foundSchema, notFoundSchema)
module.exports = class BaseWordpress extends BaseJsonService {
static get extensionType() {
throw new Error(`extensionType() function not implemented for ${this.name}`)
}
async fetch({ slug }) {
const url = `https://api.wordpress.org/${
this.constructor.extensionType
}s/info/1.1/`
return this._requestJson({
async fetch({ extensionType, slug }) {
const url = `https://api.wordpress.org/${extensionType}s/info/1.1/`
const json = await this._requestJson({
url,
schema: schemas,
options: {
qs: {
action: `${this.constructor.extensionType}_information`,
action: `${extensionType}_information`,
request: {
slug,
fields: {
@@ -51,15 +45,9 @@ module.exports = class BaseWordpress extends BaseJsonService {
},
},
})
}
async handle({ slug }) {
const json = await this.fetch({ slug })
if (!json) {
throw new NotFound()
}
return this.constructor.render({ response: json })
return json
}
}