[WordPress] API Update (#5579)
* feat: updated to new WordPress API Updated to the new WordPress API, and separated plugin and theme schemas for future expansion * test: updated testing for new API Updated some testing to work with the new API request * feat: updated schema to require keys Updated schema to require all the keys in the schema * fix: remove duplicate requirement The helper already includes required so we don't need to require it again Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com> * fix: remove duplicate requirement The helper already includes required so we don't need to require it again Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com> Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com> Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
@@ -4,25 +4,48 @@ const Joi = require('@hapi/joi')
|
||||
const { nonNegativeInteger } = require('../validators')
|
||||
const { BaseJsonService, NotFound } = require('..')
|
||||
|
||||
const foundSchema = Joi.object()
|
||||
const stringOrFalse = Joi.alternatives(Joi.string(), Joi.bool())
|
||||
|
||||
const themeSchema = Joi.object()
|
||||
.keys({
|
||||
version: Joi.string(),
|
||||
version: Joi.string().required(),
|
||||
rating: nonNegativeInteger,
|
||||
num_ratings: nonNegativeInteger,
|
||||
downloaded: nonNegativeInteger,
|
||||
active_installs: nonNegativeInteger,
|
||||
requires: Joi.string(), // Plugin Only
|
||||
tested: Joi.string(), // Plugin Only
|
||||
})
|
||||
.required()
|
||||
|
||||
const notFoundSchema = Joi.string().allow(null, false)
|
||||
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(),
|
||||
})
|
||||
.required()
|
||||
|
||||
const schemas = Joi.alternatives(foundSchema, notFoundSchema)
|
||||
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.1/`
|
||||
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,
|
||||
@@ -37,6 +60,7 @@ module.exports = class BaseWordpress extends BaseJsonService {
|
||||
homepage: 0,
|
||||
tags: 0,
|
||||
screenshot_url: 0,
|
||||
downloaded: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -45,7 +69,7 @@ module.exports = class BaseWordpress extends BaseJsonService {
|
||||
},
|
||||
},
|
||||
})
|
||||
if (!json) {
|
||||
if ('error' in json) {
|
||||
throw new NotFound()
|
||||
}
|
||||
return json
|
||||
|
||||
@@ -34,6 +34,7 @@ const mockedQuerySelector = {
|
||||
homepage: '0',
|
||||
tags: '0',
|
||||
screenshot_url: '0',
|
||||
downloaded: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -46,7 +47,7 @@ t.create('Plugin Tested WP Version - current')
|
||||
.get('/plugin/tested/akismet.json')
|
||||
.intercept(nock =>
|
||||
nock('https://api.wordpress.org')
|
||||
.get('/plugins/info/1.1/')
|
||||
.get('/plugins/info/1.2/')
|
||||
.query(mockedQuerySelector)
|
||||
.reply(200, {
|
||||
version: '1.3',
|
||||
@@ -70,7 +71,7 @@ t.create('Plugin Tested WP Version - old')
|
||||
.get('/plugin/tested/akismet.json')
|
||||
.intercept(nock =>
|
||||
nock('https://api.wordpress.org')
|
||||
.get('/plugins/info/1.1/')
|
||||
.get('/plugins/info/1.2/')
|
||||
.query(mockedQuerySelector)
|
||||
.reply(200, {
|
||||
version: '1.2',
|
||||
@@ -94,7 +95,7 @@ t.create('Plugin Tested WP Version - non-exsistant or unsupported')
|
||||
.get('/plugin/tested/akismet.json')
|
||||
.intercept(nock =>
|
||||
nock('https://api.wordpress.org')
|
||||
.get('/plugins/info/1.1/')
|
||||
.get('/plugins/info/1.2/')
|
||||
.query(mockedQuerySelector)
|
||||
.reply(200, {
|
||||
version: '1.2',
|
||||
|
||||
Reference in New Issue
Block a user