Files
shields/services/wordpress/wordpress-base.js
Joe Izzard a1c50ea4d5 [Wordpress] Plugin & Theme required php (#5723)
* feat: added schema and api options

* feat: added new badge for required php version

* test: fixed existing testing and added new for new badge

Fixed existing testing to conform to new api options and schema. Also added testing for the new badge

* fix: changed to mocked test

Changed the Theme Not Set test to a mock as unable to find a live example (previously used 'generatepress' however this now has the data). Also split out query selector fields to allow updating in one place for themes and plugins

* fix: changed the style of the badge to better show the info

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2020-10-17 18:31:58 +00:00

81 lines
2.0 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { nonNegativeInteger } = require('../validators')
const { BaseJsonService, NotFound } = require('..')
const stringOrFalse = Joi.alternatives(Joi.string(), Joi.bool())
const themeSchema = Joi.object()
.keys({
version: Joi.string().required(),
rating: nonNegativeInteger,
num_ratings: nonNegativeInteger,
downloaded: nonNegativeInteger,
active_installs: nonNegativeInteger,
requires_php: stringOrFalse.required(),
})
.required()
const pluginSchema = Joi.object()
.keys({
version: Joi.string().required(),
rating: nonNegativeInteger,
num_ratings: nonNegativeInteger,
downloaded: nonNegativeInteger,
active_installs: nonNegativeInteger,
requires: stringOrFalse.required(),
tested: Joi.string().required(),
requires_php: stringOrFalse.required(),
})
.required()
const notFoundSchema = Joi.object()
.keys({
error: Joi.string().required(),
})
.required()
const pluginSchemas = Joi.alternatives(pluginSchema, notFoundSchema)
const themeSchemas = Joi.alternatives(themeSchema, notFoundSchema)
module.exports = class BaseWordpress extends BaseJsonService {
async fetch({ extensionType, slug }) {
const url = `https://api.wordpress.org/${extensionType}s/info/1.2/`
let schemas
if (extensionType === 'plugin') {
schemas = pluginSchemas
} else if (extensionType === 'theme') {
schemas = themeSchemas
}
const json = await this._requestJson({
url,
schema: schemas,
options: {
qs: {
action: `${extensionType}_information`,
request: {
slug,
fields: {
active_installs: 1,
sections: 0,
homepage: 0,
tags: 0,
screenshot_url: 0,
downloaded: 1,
requires_php: 1,
},
},
},
qsStringifyOptions: {
encode: false,
},
},
})
if ('error' in json) {
throw new NotFound()
}
return json
}
}