Expand all

abstract Api

Base class for API-based data providers.

Constructor

new Api(api, [options])abstract #

Parameters:

Name Type Attributes Description
api mw.Api
options Object optional
Properties:
Name Type Attributes Description
maxage number optional

cache expiration time, in seconds Will be used for both client-side cache (maxage) and reverse proxies (s-maxage)

Source:

Properties

api #

API object for dependency injection.

Properties:

Type Description
mw.Api
Source:
API object for dependency injection.

cacheprotected #

API call cache.

Properties:

Name Type Description
cache Object.<string, jQuery.Promise>
Source:
API call cache.

options #

Options object; the exact format and meaning is unspecified and could be different from subclass to subclass.

Properties:

Type Description
Object
Source:

Options object; the exact format and meaning is unspecified and could be different from subclass to subclass.

Methods

apiGetWithMaxAge(params, [ajaxOptions], [maxage]) → {jQuery.Promise} #

Calls mw.Api.get, with caching parameters.

Parameters:

Name Type Attributes Description
params Object

Parameters to the API query.

ajaxOptions Object optional

ajaxOptions argument for mw.Api.get

maxage number | null optional

Cache the call for this many seconds. Sets both the maxage (client-side) and smaxage (proxy-side) caching parameters. Null means no caching. Undefined means the default caching period is used.

Returns:

the return value from mw.Api.get

Type
jQuery.Promise
Source:
Calls mw.Api.get, with caching parameters.

getCachedPromise(key, getPromise) → {jQuery.Promise} #

Wraps a caching layer around a function returning a promise; if getCachedPromise has been called with the same key already, it will return the previous result.

Since it is the promise and not the API response that gets cached, this method can ensure that there are no race conditions and multiple calls to the same resource: even if the request is still in progress, separate calls (with the same key) to getCachedPromise will share on the same promise object. The promise is cached even if it is rejected, so if the API request fails, all later calls to getCachedPromise will fail immediately without retrying the request.

Parameters:

Name Type Description
key string

cache key

getPromise function

a function to get the promise on cache miss

Returns:

Type
jQuery.Promise
Source:

Wraps a caching layer around a function returning a promise; if getCachedPromise has been called with the same key already, it will return the previous result.

getErrorMessage(data) → {string} #

Pulls an error message out of an API response.

Parameters:

Name Type Description
data Object
Properties:
Name Type Description
error Object
Properties:
Name Type Description
code string
info string

Returns:

From data.error.code + ': ' + data.error.info, or 'unknown error'

Type
string
Source:
Pulls an error message out of an API response.

getQueryPage(data) → {jQuery.Promise} #

Returns a promise with the specified page from the API result. This is intended to be used as a .then() callback for action=query&prop=(...) APIs.

Parameters:

Name Type Description
data Object

Returns:

when successful, the first argument will be the page data, when unsuccessful, it will be an error message. The second argument is always the full API response.

Type
jQuery.Promise
Source:
Returns a promise with the specified page from the API result.