Files
shields/services/wordpress/wordpress-base.js
Joe Izzard ff3acce7bb [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>
2020-09-20 18:31:31 +00:00

78 lines
1.9 KiB
JavaScript

'use strict'
const Joi = require('@hapi/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,
})
.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(),
})
.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,
},
},
},
qsStringifyOptions: {
encode: false,
},
},
})
if ('error' in json) {
throw new NotFound()
}
return json
}
}