Typed MediaWiki API client for node.js using TypeScript.
Perform different actions over the MediaWiki API, both logged out and logged in.
# npm
npm i mw.js
# yarn
yarn add mw.js
For a wiki network like Fandom, refer to § Wiki Network.
Create a generic Wiki
instance for any wiki using the Wiki
class. The only mandatory parameter is the URL to its /api.php
:
import { Wiki } from 'mw.js'
const wikipedia = new Wiki( {
api: 'https://en.wikipedia.org/w/api.php'
} )
To log in into a bot account using your BotPasswords credentials, create a Bot
instance as follows:
import { Bot, Wiki } from 'mw.js'
const wikipedia = new Wiki( {
api: 'https://en.wikipedia.org/w/api.php'
} )
const bot = new Bot( {
password: 'YOUR_PASSWORD',
username: 'Username@Botname',
wiki: wikipedia
} )
await bot.login()
The pervious code will log in into your bot account on English Wikipedia. After that, you can perform any action your bot's passwords allows.
You only need to login if you wish to perform an action that requires an user's token, like a CSRF token (e.g. deleting, editing or protecting a page). For querying pages, Wiki
is more than enough.
If you only need to work on a single wiki, the previous example should work for you. However, mw.js
exports a Fandom
class that facilitates some useful utilities for Fandom's wikis.
Other Wiki Networks may be added in the future, but feel free to create a PR and add your own.
Fandom's class allows to get wikis using only their interwiki. In case you are not familiar with interwikis:
Interwiki | URL |
---|---|
community | https://community.fandom.com |
es.genshin-impact | https://genshin-impact.fandom.com/es |
pl.gothic | https://gothic.fandom.com/pl |
To instantiate a FandomWiki
, it is advisable to instantiate Fandom
first:
import { Fandom } from 'mw.js'
const fandom = new Fandom()
const wiki = fandom.getWiki( 'es.genshin-impact' )
You can also login into an account through a Fandom
's method:
import { Fandom } from 'mw.js'
const fandom = new Fandom()
const wiki = fandom.getWiki( 'es.genshin-impact' )
const bot = await fandom.login( {
password: 'YOUR_PASSWORD',
username: 'Username@Botname',
wiki
} )
Unlike Bot#login
, Fandom#login
returns a FandomBot
instance after logging in.