FANDOM


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 Control+F5 or Control+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.


Changelog

v1.0 — March 25, 2019
TheGoldenPatrik1: Initial revision
v1.1 — April 2, 2019
TheGoldenPatrik1: Cache messages
v1.2 — September 17, 2019
TheGoldenPatrik1: Return a promise, cache API requests, add more parameters, support cache clearing
Text above can be found here (edit)
Community content is available under CC-BY-SA unless otherwise noted.

Fandom may earn an affiliate commission on sales made from links on this page.

Stream the best stories.

Fandom may earn an affiliate commission on sales made from links on this page.

Get Disney+