Debian Codegraph @

resources/src/mediawiki.api/rest.js

raw symbols SCIP
1 ( function () {
2
3 /**
4 * @typedef {Object} mw.Rest.Options
5 * @property {Object} [ajax={ url: mw.util.wikiScript( 'rest' ), timeout: 30 * 1000 }] Default
6 * options for [ajax()]{@link mw.Rest#ajax} calls. Can be overridden by passing `options` to
7 * the {@link mw.Rest} constructor.
8 */
9
10 /**
11 * @type {mw.Rest.Options}
12 * @private
13 */
14 const defaultOptions = {
15 ajax: {
16 url: mw.util.wikiScript( 'rest' ),
17 timeout: 30 * 1000 // 30 seconds
18 }
19 };
20
21 /**
22 * Lower cases the key names in the provided object.
23 *
24 * @param {Object} headers
25 * @return {Object}
26 * @private
27 */
28 function objectKeysToLowerCase( headers ) {
29 return Object.keys( headers || {} ).reduce( ( updatedHeaders, key ) => {
30 updatedHeaders[ key.toLowerCase() ] = headers[ key ];
31 return updatedHeaders;
32 }, {} );
33 }
34
35 /**
36 * @classdesc Interact with the REST API. mw.Rest is a client library
37 * for the [REST API](https://www.mediawiki.org/wiki/Special:MyLanguage/API:REST_API).
38 * An mw.Rest object represents the REST API of a MediaWiki site.
39 * For the action API, see {@link mw.Api}.
40 *
41 * @example
42 * var api = new mw.Rest();
43 * api.get( '/v1/page/Main_Page/html' )
44 * .then( function ( data ) {
45 * console.log( data );
46 * } );
47 *
48 * api.post( '/v1/page/Main_Page', {
49 * token: 'anon_token',
50 * source: 'Lörem Ipsüm',
51 * comment: 'tästing',
52 * title: 'My_Page'
53 * }, {
54 * 'authorization': 'token'
55 * } )
56 * .then( function ( data ) {
57 * console.log( data );
58 * } );
59 *
60 * @constructor
61 * @description Create an instance of `mw.Rest`.
62 * @param {mw.Rest.Options} [options] See {@link mw.Rest.Options}
63 */
64 mw.Rest = function ( options ) {
65 const defaults = Object.assign( {}, options );
66 defaults.ajax = Object.assign( {}, defaultOptions.ajax, defaults.ajax );
67
68 this.url = defaults.ajax.url;
69 delete defaults.ajax.url;
70
71 this.defaults = defaults;
72 this.requests = [];
73 };
74
75 mw.Rest.prototype = {
76 /**
77 * Abort all unfinished requests issued by this Api object.
78 *
79 * @method
80 */
81 abort: function () {
82 this.requests.forEach( ( request ) => {
83 if ( request ) {
84 request.abort();
85 }
86 } );
87 },
88
89 /**
90 * Perform REST API get request.
91 *
92 * @method
93 * @param {string} path
94 * @param {Object} query
95 * @param {Object} [headers]
96 * @return {jQuery.Promise}
97 */
98 get: function ( path, query, headers ) {
99 return this.ajax( path, {
100 type: 'GET',
101 data: query,
102 headers: headers || {}
103 } );
104 },
105
106 /**
107 * Perform REST API post request.
108 *
109 * Note: only sending application/json is currently supported.
110 *
111 * @method
112 * @param {string} path
113 * @param {Object} [body]
114 * @param {Object} [headers]
115 * @return {jQuery.Promise}
116 */
117 post: function ( path, body, headers ) {
118 if ( body === undefined ) {
119 body = {};
120 }
121
122 headers = objectKeysToLowerCase( headers );
123 return this.ajax( path, {
124 type: 'POST',
125 headers: Object.assign( headers, { 'content-type': 'application/json' } ),
126 data: JSON.stringify( body )
127 } );
128 },
129
130 /**
131 * Perform REST API PUT request.
132 *
133 * Note: only sending `application/json` is currently supported.
134 *
135 * @method
136 * @param {string} path
137 * @param {Object} body
138 * @param {Object} [headers]
139 * @return {jQuery.Promise}
140 */
141 put: function ( path, body, headers ) {
142 headers = objectKeysToLowerCase( headers );
143 return this.ajax( path, {
144 type: 'PUT',
145 headers: Object.assign( headers, { 'content-type': 'application/json' } ),
146 data: JSON.stringify( body )
147 } );
148 },
149
150 /**
151 * Perform REST API DELETE request.
152 *
153 * Note: only sending `application/json` is currently supported.
154 *
155 * @method
156 * @param {string} path
157 * @param {Object} body
158 * @param {Object} [headers]
159 * @return {jQuery.Promise}
160 */
161 delete: function ( path, body, headers ) {
162 headers = objectKeysToLowerCase( headers );
163 return this.ajax( path, {
164 type: 'DELETE',
165 headers: Object.assign( headers, { 'content-type': 'application/json' } ),
166 data: JSON.stringify( body )
167 } );
168 },
169
170 /**
171 * Perform the API call.
172 *
173 * @method
174 * @param {string} path
175 * @param {Object} [ajaxOptions]
176 * @return {jQuery.Promise} Done: API response data and the jqXHR object.
177 * Fail: Error code
178 */
179 ajax: function ( path, ajaxOptions ) {
180 const self = this,
181 apiDeferred = $.Deferred();
182
183 ajaxOptions = Object.assign( {}, this.defaults.ajax, ajaxOptions );
184 ajaxOptions.url = this.url + path;
185
186 // Make the AJAX request.
187 const xhr = $.ajax( ajaxOptions );
188
189 // Save it to make it possible to abort.
190 const requestIndex = this.requests.length;
191 this.requests.push( xhr );
192 xhr.always( () => {
193 self.requests[ requestIndex ] = null;
194 } );
195
196 xhr.then(
197 // AJAX success just means "200 OK" response.
198 ( result, textStatus, jqXHR ) => {
199 apiDeferred.resolve( result, jqXHR );
200 },
201 // If AJAX fails, reject API call with error code 'http'
202 // and details in second argument.
203 ( jqXHR, textStatus, exception ) => {
204 apiDeferred.reject( 'http', {
205 xhr: jqXHR,
206 textStatus: textStatus,
207 exception: exception
208 } );
209 }
210 );
211
212 // Return the Promise
213 return apiDeferred.promise( { abort: xhr.abort } );
214 }
215 };
216 }() );