Debian Codegraph @

resources/src/mediawiki.ForeignApi/mediawiki.ForeignApi.core.js

raw symbols SCIP
1 module.exports = ( function () {
2
3 /**
4 * @classdesc Interact with the API of another MediaWiki site. mw.Foreign API creates
5 * an object like {@link mw.Api}, but automatically handle everything required to communicate
6 * with another MediaWiki wiki via cross-origin requests (CORS).
7 *
8 * The foreign wiki must be configured to accept requests from the current wiki.
9 * For details, see [$wgCrossSiteAJAXdomains](https://www.mediawiki.org/wiki/Manual:$wgCrossSiteAJAXdomains)
10 * and [$wgRestAllowCrossOriginCookieAuth](https://www.mediawiki.org/wiki/Manual:$wgRestAllowCrossOriginCookieAuth).
11 * ```
12 * const api = new mw.ForeignApi( 'https://commons.wikimedia.org/w/api.php' );
13 * api.get( {
14 * action: 'query',
15 * meta: 'userinfo'
16 * } ).done( function ( data ) {
17 * console.log( data );
18 * } );
19 * ```
20 *
21 * To ensure that the user at the foreign wiki is logged in, pass the `assert: 'user'` parameter
22 * to {@link mw.ForeignApi#get get()}/{@link mw.ForeignApi#post post()} (since MW 1.23), otherwise
23 * the API request will fail. (Note that this doesn't guarantee that it's the same user. To assert
24 * that the user at the foreign wiki has a specific username, pass the `assertuser` parameter with
25 * the desired username.)
26 *
27 * Authentication-related MediaWiki extensions may extend this class to ensure that the user
28 * authenticated on the current wiki will be automatically authenticated on the foreign one. These
29 * extension modules should be registered using the ResourceLoaderForeignApiModules hook. See
30 * CentralAuth for a practical example. The general pattern to extend and override the name is:
31 * ```
32 * function MyForeignApi() {};
33 * OO.inheritClass( MyForeignApi, mw.ForeignApi );
34 * mw.ForeignApi = MyForeignApi;
35 * ```
36 *
37 * @class mw.ForeignApi
38 * @extends mw.Api
39 * @since 1.26
40 *
41 * @constructor
42 * @description Create an instance of `mw.ForeignApi`.
43 * @param {string} url URL pointing to another wiki's `api.php` endpoint.
44 * @param {mw.Api.Options} [options] Also accepts all the options from {@link mw.Api.Options}.
45 * @param {boolean} [options.anonymous=false] Perform all requests anonymously. Use this option if
46 * the target wiki may otherwise not accept cross-origin requests, or if you don't need to
47 * perform write actions or read restricted information and want to avoid the overhead.
48 *
49 * @author Bartosz DziewoƄski
50 * @author Jon Robson
51 */
52 function CoreForeignApi( url, options ) {
53 if ( !url || $.isPlainObject( url ) ) {
54 throw new Error( 'mw.ForeignApi() requires a `url` parameter' );
55 }
56
57 this.apiUrl = String( url );
58 this.anonymous = options && options.anonymous;
59
60 options = $.extend( /* deep= */ true,
61 {
62 ajax: {
63 url: this.apiUrl,
64 xhrFields: {
65 withCredentials: !this.anonymous
66 }
67 },
68 parameters: {
69 origin: this.getOrigin()
70 }
71 },
72 options
73 );
74
75 // Call parent constructor
76 CoreForeignApi.super.call( this, options );
77 }
78
79 OO.inheritClass( CoreForeignApi, mw.Api );
80
81 /**
82 * Return the origin to use for API requests, in the required format (protocol, host and port, if
83 * any).
84 *
85 * @memberof mw.ForeignApi.prototype
86 * @protected
87 * @return {string|undefined}
88 */
89 CoreForeignApi.prototype.getOrigin = function () {
90 if ( this.anonymous ) {
91 return '*';
92 }
93
94 const origin = location.origin;
95 const apiOrigin = new URL( this.apiUrl, location.origin ).origin;
96
97 if ( origin === apiOrigin ) {
98 // requests are not cross-origin, omit parameter
99 return undefined;
100 }
101
102 return origin;
103 };
104
105 /**
106 * @inheritdoc
107 */
108 CoreForeignApi.prototype.ajax = function ( parameters, ajaxOptions ) {
109 let newAjaxOptions;
110
111 // 'origin' query parameter must be part of the request URI, and not just POST request body
112 if ( ajaxOptions.type === 'POST' ) {
113 let url = ( ajaxOptions && ajaxOptions.url ) || this.defaults.ajax.url;
114 const origin = ( parameters && parameters.origin ) || this.defaults.parameters.origin;
115 if ( origin !== undefined ) {
116 url += ( url.indexOf( '?' ) !== -1 ? '&' : '?' ) +
117 'origin=' + encodeURIComponent( origin );
118 }
119 newAjaxOptions = Object.assign( {}, ajaxOptions, { url: url } );
120 } else {
121 newAjaxOptions = ajaxOptions;
122 }
123
124 return CoreForeignApi.super.prototype.ajax.call( this, parameters, newAjaxOptions );
125 };
126
127 return CoreForeignApi;
128 }() );