mwjs v1.1.0
mwjs
A promise-based MediaWiki client for Node.js with types. Mainly for FANDOM wikis
Installation
With npm:
npm i mwjs
Usage
Client
Create a Client
instance specifying the API endpoint and both an username and password generated through Special:BotPasswords.
import { Client } from 'mwjs'
(async () => {
const client = new Client({
api: 'https://wikiname.fandom.com/api.php',
username: 'Bitomic@mwjs',
password: 'YOURBOTPASSWORD'
})
await client.login()
})()
Recommendation: Do not insert directly your username nor password in the file if you intend to make the file public. If that occurs, reset your password on Special:BotPasswords.
import dotenv from 'dotenv'
import { Client } from 'mwjs'
(async () => {
const client = new Client({
api: 'https://wikiname.fandom.com/api.php',
username: process.env.FANDOM_BOT_USERNAME,
password: process.env.FANDOM_BOT_PASSWORD
})
await client.login()
})()
get
If you need to perform your own GET
request directly to the API, you can use client.get
for that purpose. It takes as its unique argument an object with the search parameters.
It will force the parameters to include { format: 'json' }
, so it is not necessary to include it.
await client.get({
action: 'query',
list: 'allpages'
})
post
If you need to perform your own POST
request directly to the API, you can use client.post
for that purpose. It takes two arguments: a parameters object, like client.get
, and a FormData
object.
import FormData from 'form-data'
const params = { action: 'delete' }
const form = new FormData()
form.append('title', 'Page to delete')
form.append('reason', 'mwjs: delete')
form.append('token', await client.getCSRFToken())
client.post({
params, form
})
TODO
- Support generators.