Search code examples
javascripttypescriptmediawiki-apijavascript-import

How do I import and use the Javascript MediaWiki API?


This seems way to basic but there just aren't examples available and the documentation is bad in a very typical way. They don't say how to import the library.

I'm trying to use the MediaWiki officially supported library from a Typescript/React project.

There's some examples of basic usage but no import statement in the documentation.

I have tried:

import mw from 'mediawiki'
console.log(mw)

Which gives me an object that contains, very unexpectedly, this:

Object { version: "0.0.11", Bot: Bot(config) }

So I tried adding this, matching a little bit of what the docs look like:

const api = new mw.Bot()

Which produces an object that I just don't recognize as documented anywhere and does not work with the existing docs.

enter image description here

What is going on here? I just want a library that will let me get all the categories from a MediaWiki site and all the pages in a given category. Is it me or are these docs completely wrong and incomplete?

Or maybe I'm missing something really simple all Javascript developers just assume? I am a career software developer but I must admit I program in Javascript a lot less than I used to so there might be assumptions I'm missing here.


Solution

  • TL;DR: You ran npm install mediawiki without checking the package description first.

    The documentation you’ve linked corresponds to the mw object exposed within MediaWiki’s frontend via mediawiki.api/index.js. I’m not personally aware of this file being available for installation via npm.

    The mediawiki package on npm does not correspond with the documentation you’ve linked. Its last release was 10 years ago, made not by Wikimedia (the primary maintainers of MediaWiki), but by GitHub user @oliver-moran. It also exposes that mw.Bot API you’re showing in your screenshot, which confirms my suspicion above.

    If you are committed to using the client library you’re already trying to use, you may wish to take a dependency of mediawiki.api/index.js in your project if you’re also willing to manually maintain it and its dependencies (including jQuery). However, it’s not fully clear to me if this is actually intended to be used this way or outside of a MediaWiki frontend execution context at all.

    That being said, MediaWiki themselves on their developer wiki point to several JavaScript API client libraries available for use in your project, should the one above not meet your requirements.