Files
shields/services/wikiapiary/wikiapiary-installs.service.js
dependabot[bot] b9d96755ec chore(deps-dev): bump prettier from 2.8.8 to 3.0.0 (#9357)
* chore(deps-dev): bump prettier from 2.8.8 to 3.0.0

Bumps [prettier](https://github.com/prettier/prettier) from 2.8.8 to 3.0.0.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/2.8.8...3.0.0)

---
updated-dependencies:
- dependency-name: prettier
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* reformat all the things (prettier 3)

* update tests to await calls to prettier.format()

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: chris48s <git@chris-shaw.dev>
2023-07-10 09:27:51 +00:00

112 lines
3.0 KiB
JavaScript

import Joi from 'joi'
import { renderDownloadsBadge } from '../downloads.js'
import { BaseJsonService, NotFound } from '../index.js'
const documentation = `
<p>
The name of an extension is case-sensitive excluding the first character.
</p>
<p>
For example, in the case of <code>ParserFunctions</code>, the following are
valid:
<ul>
<li><code>ParserFunctions</code></li>
<li><code>parserFunctions</code></li>
</ul>
However, the following are invalid:
<ul>
<li><code>parserfunctions</code></li>
<li><code>Parserfunctions</code></li>
<li><code>pARSERfUNCTIONS</code></li>
</ul>
</p>
`
const schema = Joi.object({
query: Joi.object({
results: Joi.alternatives([
Joi.object()
.required()
.pattern(/^\w+:.+$/, {
printouts: Joi.object({
'Has website count': Joi.array()
.required()
.items(Joi.number().required()),
}).required(),
}),
Joi.array().required(),
]).required(),
}).required(),
}).required()
/**
* This badge displays the total installations of a MediaWiki extensions, skins,
* etc via Wikiapiary.
*
* {@link https://www.mediawiki.org/wiki/Manual:Extensions MediaWiki Extensions Manual}
*/
export default class WikiapiaryInstalls extends BaseJsonService {
static category = 'downloads'
static route = {
base: 'wikiapiary',
pattern: ':variant(extension|skin|farm|generator|host)/installs/:name',
}
static examples = [
{
title: 'Wikiapiary installs',
namedParams: { variant: 'extension', name: 'ParserFunctions' },
staticPreview: this.render({ usage: 11170 }),
documentation,
keywords: ['mediawiki'],
},
]
static defaultBadgeData = { label: 'installs', color: 'informational' }
static render({ usage: downloads }) {
return renderDownloadsBadge({
downloads,
colorOverride: 'informational',
})
}
async fetch({ variant, name }) {
return this._requestJson({
schema,
url: 'https://wikiapiary.com/w/api.php',
options: {
searchParams: {
action: 'ask',
query: `[[${variant}:${name}]]|?Has_website_count`,
format: 'json',
},
},
})
}
static transform({ results, variant, name }) {
if (Array.isArray(results)) {
throw new NotFound({ prettyMessage: 'not found' })
}
const keyLowerCase = `${variant}:${name.toLowerCase()}`
const resultKey = Object.keys(results).find(
key => keyLowerCase === key.toLowerCase(),
)
if (resultKey === undefined)
throw new NotFound({ prettyMessage: 'not found' })
const [usage] = results[resultKey].printouts['Has website count']
return { usage }
}
async handle({ variant, name }) {
const {
query: { results },
} = await this.fetch({ variant, name })
const { usage } = this.constructor.transform({ results, name, variant })
return this.constructor.render({ usage })
}
}