Fandom Developers Wiki
Advertisement

Fetch is a library for getting and caching API requests. It also includes special support for getting and processing system messages.

Usage

Importing

To import and use the Fetch library inside your script, use the following.

mw.hook('dev.fetch').add(function (fetch) {
    // your code here
});
importArticle({
    type: 'script',
    article: 'u:dev:MediaWiki:Fetch.js'
});

Methods

The library exports the window.dev.fetch method, which accepts one parameter. This parameter may be formatted in three ways.

  • If the parameter is a string, the library will get that message and return it as a string. If the string contains multiple messages separated by | (e.g. block|userrights), it will get each of the messages and return them as a function. Invoking the function without a parameter will return an object of all the messages, while supplying a parameter returns the value of that key in the object. The key can be either the message name or its index.
  • If the parameter is an array, the library will get the specified messages and return them as an invokable function (see above).
  • If the parameter is an object, it can have the following properties, all of which are optional.
Name Description Default Type
lang The language to get the system messages in. Use only with messages. wgUserLanguage string
messages The system messages to get. N/A string or array
process Processing that should be applied to the data before it gets returned. N/A function
request The API request and callback to execute. N/A function
time The time, in milliseconds, that the request should be cached for. One day number
noCache Whether the request should be cached. Not recommended except for personal debugging purposes. false boolean

After completing the API request or retrieving the cache, the library returns a promise.

Examples

This gets a single message.
mw.hook('dev.fetch').add(function (fetch) {
    fetch('block').then(function (msg) {
        $('.wds-list').append('<li>' + msg + '</li>');
    });
});
This gets multiple messages.
mw.hook('dev.fetch').add(function (fetch) {
    fetch('block|userrights').then(function (msg) {
        $('.wds-list').append('<li>' + msg()[0] + msg('userrights') + '</li>');
    });
});
This gets multiple messages in German with a two day delay.
mw.hook('dev.fetch').add(function (fetch) {
    fetch({
        lang: 'de',
        messages: ['block', 'userrights'],
        time: 2 * 24 * 60 * 60 * 1000
    }).then(function (msg) {
        $('.wds-list').append('<li>' + msg('block') + msg()[1] + '</li>');
    });
});
This executes and caches an API request. Note that the request function accepts two parameters, which should be used to resolve the library's promise.
mw.hook('dev.fetch').add(function (fetch) {
    fetch({
        request: function (resolve, reject) {
            new mw.Api().get({
                action: 'query',
                titles: 'MediaWiki:ImportJS'
            }).done(function (d) {
                if (d.error) {
                    reject(d.error.code);
                } else {
                    resolve(d.query.pages);
                }
            }).fail(function () {
                reject();
            });
        },
        name: 'PageExists'
    }).then(function (d) {
        if (!d[-1]) {
            console.log('MediaWiki:ImportJS exists!');
        }
    });
});

Cache

Each cache, by default, lasts for one day, after which it is updated. The entire Fetch cache is also regularly cleared approximately every 100 page loads. To bypass this cache, simply add ?debug=1 to the URL, which will delete every Fetch item and re-run every API request. Alternately, use the keyboard commands Ctrl + F5 or Ctrl + ⇧ Shift + R, which will do the same thing and reload the page. To bypass cache for just one script, simply set the noCache option to true.

Dependents

List of dependents using this library

Changelog

Version Date Description Author
1.0 March 25, 2019 Initial revision. TheGoldenPatrik1
1.1 April 2, 2019 Cache messages. TheGoldenPatrik1
1.2 September 17, 2019 Return a promise, cache API requests, add more parameters, support cache clearing. TheGoldenPatrik1
Text above can be found here (edit)
Advertisement