diff options
author | Ed Sanders <esanders@wikimedia.org> | 2024-09-16 11:42:06 +0100 |
---|---|---|
committer | Ed Sanders <esanders@wikimedia.org> | 2024-09-16 11:45:58 +0100 |
commit | e352a3a878e703d48d6fb1091cf804f594b1b83c (patch) | |
tree | 409531114ca2c03e93e62b402853a27c19b8e341 /resources | |
parent | 9c6a01877deda0b94fbc84b00510d6a62df8f4a8 (diff) | |
download | mediawikicore-e352a3a878e703d48d6fb1091cf804f594b1b83c.tar.gz mediawikicore-e352a3a878e703d48d6fb1091cf804f594b1b83c.zip |
eslint: Prefer const in mediawiki.api
Change-Id: Id1ec60db0489dc9b082ed164e98b0a2a46f56054
Diffstat (limited to 'resources')
-rw-r--r-- | resources/src/mediawiki.api/category.js | 4 | ||||
-rw-r--r-- | resources/src/mediawiki.api/edit.js | 9 | ||||
-rw-r--r-- | resources/src/mediawiki.api/index.js | 23 | ||||
-rw-r--r-- | resources/src/mediawiki.api/login.js | 8 | ||||
-rw-r--r-- | resources/src/mediawiki.api/options.js | 10 | ||||
-rw-r--r-- | resources/src/mediawiki.api/parse.js | 16 | ||||
-rw-r--r-- | resources/src/mediawiki.api/rest.js | 9 | ||||
-rw-r--r-- | resources/src/mediawiki.api/upload.js | 47 |
8 files changed, 56 insertions, 70 deletions
diff --git a/resources/src/mediawiki.api/category.js b/resources/src/mediawiki.api/category.js index 7d2038aae3f6..1b6a0c9c36d5 100644 --- a/resources/src/mediawiki.api/category.js +++ b/resources/src/mediawiki.api/category.js @@ -62,12 +62,10 @@ return apiPromise .then( ( data ) => { - let page; - if ( !data.query || !data.query.pages ) { return false; } - page = data.query.pages[ 0 ]; + const page = data.query.pages[ 0 ]; if ( !page.categories ) { return false; } diff --git a/resources/src/mediawiki.api/edit.js b/resources/src/mediawiki.api/edit.js index 77b5979d5d7e..8e75c7d13578 100644 --- a/resources/src/mediawiki.api/edit.js +++ b/resources/src/mediawiki.api/edit.js @@ -114,11 +114,11 @@ * @return {jQuery.Promise} Edit API response */ edit: function ( title, transform ) { - let basetimestamp, curtimestamp, - api = this; + const api = this; title = String( title ); + let basetimestamp, curtimestamp; return api.get( { action: 'query', prop: 'revisions', @@ -128,18 +128,17 @@ curtimestamp: true } ) .then( ( data ) => { - let page, revision; if ( !data.query || !data.query.pages ) { return $.Deferred().reject( 'unknown' ); } - page = data.query.pages[ 0 ]; + const page = data.query.pages[ 0 ]; if ( !page || page.invalid ) { return $.Deferred().reject( 'invalidtitle' ); } if ( page.missing ) { return $.Deferred().reject( 'nocreate-missing' ); } - revision = page.revisions[ 0 ]; + const revision = page.revisions[ 0 ]; basetimestamp = revision.timestamp; curtimestamp = data.curtimestamp; return transform( { diff --git a/resources/src/mediawiki.api/index.js b/resources/src/mediawiki.api/index.js index 704c64aea405..1d61b4703108 100644 --- a/resources/src/mediawiki.api/index.js +++ b/resources/src/mediawiki.api/index.js @@ -14,7 +14,7 @@ * @private * @type {mw.Api.Options} */ - let defaultOptions; + let defaultOptions = null; /** * @classdesc Interact with the MediaWiki API. `mw.Api` is a client library for @@ -231,14 +231,13 @@ * {@link JSON.parse}. */ ajax: function ( parameters, ajaxOptions ) { - let token, requestIndex, - api = this, - apiDeferred = $.Deferred(), - xhr, key, formData; + const api = this, + apiDeferred = $.Deferred(); parameters = Object.assign( {}, this.defaults.parameters, parameters ); ajaxOptions = Object.assign( {}, this.defaults.ajax, ajaxOptions ); + let token; // Ensure that token parameter is last (per [[mw:API:Edit#Token]]). if ( parameters.token ) { token = parameters.token; @@ -254,9 +253,9 @@ ajaxOptions.contentType === 'multipart/form-data' ) { - formData = new FormData(); + const formData = new FormData(); - for ( key in parameters ) { + for ( const key in parameters ) { formData.append( key, parameters[ key ] ); } // If we extracted a token parameter, add it back in. @@ -286,7 +285,7 @@ } // Make the AJAX request - xhr = $.ajax( ajaxOptions ) + const xhr = $.ajax( ajaxOptions ) // If AJAX fails, reject API call with error code 'http' // and the details in the second argument. .fail( ( jqXHR, textStatus, exception ) => { @@ -318,7 +317,7 @@ } } ); - requestIndex = this.requests.length; + const requestIndex = this.requests.length; this.requests.push( xhr ); xhr.always( () => { api.requests[ requestIndex ] = null; @@ -350,14 +349,14 @@ * @since 1.22 */ postWithToken: function ( tokenType, params, ajaxOptions ) { - let api = this, + const api = this, assertParams = { assert: params.assert, assertuser: params.assertuser }, abortedPromise = $.Deferred().reject( 'http', - { textStatus: 'abort', exception: 'abort' } ).promise(), - abortable, + { textStatus: 'abort', exception: 'abort' } ).promise(); + let abortable, aborted; return api.getToken( tokenType, assertParams ).then( ( token ) => { diff --git a/resources/src/mediawiki.api/login.js b/resources/src/mediawiki.api/login.js index 49c93d5bce1e..bde2f0ca44cf 100644 --- a/resources/src/mediawiki.api/login.js +++ b/resources/src/mediawiki.api/login.js @@ -8,17 +8,17 @@ * @return {jQuery.Promise} See [post()]{@link mw.Api#post} */ login: function ( username, password ) { - let params, apiPromise, innerPromise, - api = this; + const api = this; - params = { + const params = { action: 'login', lgname: username, lgpassword: password }; - apiPromise = api.post( params ); + const apiPromise = api.post( params ); + let innerPromise; return apiPromise .then( ( data ) => { params.lgtoken = data.login.token; diff --git a/resources/src/mediawiki.api/options.js b/resources/src/mediawiki.api/options.js index 776595257bf5..4ef4ecd82b4f 100644 --- a/resources/src/mediawiki.api/options.js +++ b/resources/src/mediawiki.api/options.js @@ -39,15 +39,14 @@ * @return {jQuery.Promise} */ saveOptions: function ( options, params ) { - let name, value, bundleable, - grouped = [], - promise; + const grouped = []; // Logged-out users can't have user options; we can't depend on mw.user, that'd be circular if ( mw.config.get( 'wgUserName' ) === null || mw.config.get( 'wgUserIsTemp' ) ) { return $.Deferred().reject( 'notloggedin' ).promise(); } + let promise; // If another options request to this API is pending, wait for it first if ( saveOptionsRequests[ this.defaults.ajax.url ] && @@ -63,9 +62,10 @@ promise = $.Deferred().resolve(); } - for ( name in options ) { - value = options[ name ] === null ? null : String( options[ name ] ); + for ( const name in options ) { + const value = options[ name ] === null ? null : String( options[ name ] ); + let bundleable; // Can we bundle this option, or does it need a separate request? if ( this.defaults.useUS ) { bundleable = name.indexOf( '=' ) === -1; diff --git a/resources/src/mediawiki.api/parse.js b/resources/src/mediawiki.api/parse.js index 617e499b59a3..2f0a0881a77c 100644 --- a/resources/src/mediawiki.api/parse.js +++ b/resources/src/mediawiki.api/parse.js @@ -11,15 +11,15 @@ * @return {jQuery.Promise<string>} Promise that resolves with the parsed HTML of `wikitext` */ parse: function ( content, additionalParams ) { - let apiPromise, - config = Object.assign( { - formatversion: 2, - action: 'parse', - // Minimize the JSON we get back, there is no way to access anything else anyway - prop: 'text', - contentmodel: 'wikitext' - }, additionalParams ); + const config = Object.assign( { + formatversion: 2, + action: 'parse', + // Minimize the JSON we get back, there is no way to access anything else anyway + prop: 'text', + contentmodel: 'wikitext' + }, additionalParams ); + let apiPromise; if ( mw.Title && content instanceof mw.Title ) { // Parse existing page config.page = content.getPrefixedDb(); diff --git a/resources/src/mediawiki.api/rest.js b/resources/src/mediawiki.api/rest.js index 5335c50d3492..2dce3a5854ba 100644 --- a/resources/src/mediawiki.api/rest.js +++ b/resources/src/mediawiki.api/rest.js @@ -177,18 +177,17 @@ * Fail: Error code */ ajax: function ( path, ajaxOptions ) { - let self = this, - apiDeferred = $.Deferred(), - xhr, requestIndex; + const self = this, + apiDeferred = $.Deferred(); ajaxOptions = Object.assign( {}, this.defaults.ajax, ajaxOptions ); ajaxOptions.url = this.url + path; // Make the AJAX request. - xhr = $.ajax( ajaxOptions ); + const xhr = $.ajax( ajaxOptions ); // Save it to make it possible to abort. - requestIndex = this.requests.length; + const requestIndex = this.requests.length; this.requests.push( xhr ); xhr.always( () => { self.requests[ requestIndex ] = null; diff --git a/resources/src/mediawiki.api/upload.js b/resources/src/mediawiki.api/upload.js index f061fb25a89b..4f7d096b820d 100644 --- a/resources/src/mediawiki.api/upload.js +++ b/resources/src/mediawiki.api/upload.js @@ -62,10 +62,9 @@ * @return {jQuery.Promise} */ uploadWithFormData: function ( file, data ) { - let key, request, - deferred = $.Deferred(); + const deferred = $.Deferred(); - for ( key in data ) { + for ( const key in data ) { if ( !fieldsAllowed[ key ] ) { delete data[ key ]; } @@ -81,7 +80,7 @@ } // Use this.postWithEditToken() or this.post() - request = this[ this.needToken() ? 'postWithEditToken' : 'post' ]( data, { + const request = this[ this.needToken() ? 'postWithEditToken' : 'post' ]( data, { // Use FormData (if we got here, we know that it's available) contentType: 'multipart/form-data', // No timeout (default from mw.Api is 30 seconds) @@ -127,8 +126,7 @@ * @return {jQuery.Promise} */ chunkedUpload: function ( file, data, chunkSize, chunkRetries ) { - let start, end, promise, next, active, - deferred = $.Deferred(); + const deferred = $.Deferred(); chunkSize = chunkSize === undefined ? 5 * 1024 * 1024 : chunkSize; chunkRetries = chunkRetries === undefined ? 1 : chunkRetries; @@ -137,16 +135,17 @@ throw new Error( 'Filename not included in file data.' ); } + let promise; // Submit first chunk to get the filekey - active = promise = this.uploadChunk( file, data, 0, chunkSize, '', chunkRetries ) + let active = promise = this.uploadChunk( file, data, 0, chunkSize, '', chunkRetries ) .done( chunkSize >= file.size ? deferred.resolve : null ) .fail( deferred.reject ) .progress( deferred.notify ); // Now iteratively submit the rest of the chunks - for ( start = chunkSize; start < file.size; start += chunkSize ) { - end = Math.min( start + chunkSize, file.size ); - next = $.Deferred(); + for ( let start = chunkSize; start < file.size; start += chunkSize ) { + const end = Math.min( start + chunkSize, file.size ); + const next = $.Deferred(); // We could simply chain one this.uploadChunk after another with // .then(), but then we'd hit an `Uncaught RangeError: Maximum @@ -183,8 +182,7 @@ * @return {jQuery.Promise} */ uploadChunk: function ( file, data, start, end, filekey, retries ) { - let upload, - api = this, + const api = this, chunk = this.slice( file, start, end ); // When uploading in chunks, we're going to be issuing a lot more @@ -203,12 +201,10 @@ data.filekey = filekey; } - upload = this.uploadWithFormData( file, data ); + const upload = this.uploadWithFormData( file, data ); return upload.then( null, ( code, result ) => { - let retry; - // uploadWithFormData will reject uploads with warnings, but // these warnings could be "harmless" or recovered from // (e.g. exists-normalized, when it'll be renamed later) @@ -233,7 +229,7 @@ } // If the call flat out failed, we may want to try again... - retry = api.uploadChunk.bind( api, file, data, start, end, filekey, retries - 1 ); + const retry = api.uploadChunk.bind( api, file, data, start, end, filekey, retries - 1 ); return api.retry( code, result, retry ); }, // Since we're only uploading small parts of a file, we @@ -253,9 +249,8 @@ * @return {jQuery.Promise} */ retry: function ( code, result, callable ) { - let uploadPromise, - retryTimer, - deferred = $.Deferred(), + let uploadPromise; + const deferred = $.Deferred(), // Wrap around the callable, so that once it completes, it'll // resolve/reject the promise we'll return retry = function () { @@ -269,7 +264,7 @@ return deferred.reject( code, result ); } - retryTimer = setTimeout( retry, 1000 ); + const retryTimer = setTimeout( retry, 1000 ); return deferred.promise( { abort: function () { // Clear the scheduled upload, or abort if already in flight if ( retryTimer ) { @@ -325,8 +320,8 @@ * function that should be called to finish the upload. */ finishUploadToStash: function ( uploadPromise, data ) { - let filekey, - api = this; + const api = this; + let filekey; function finishUpload( moreData ) { return api.uploadFromStash( filekey, Object.assign( {}, data, moreData ) ); @@ -375,13 +370,11 @@ * function that should be called to finish the upload. */ uploadToStash: function ( file, data ) { - let promise; - if ( !data.filename ) { throw new Error( 'Filename not included in file data.' ); } - promise = this.upload( file, { stash: true, filename: data.filename, ignorewarnings: data.ignorewarnings } ); + const promise = this.upload( file, { stash: true, filename: data.filename, ignorewarnings: data.ignorewarnings } ); return this.finishUploadToStash( promise, data ); }, @@ -401,13 +394,11 @@ * function that should be called to finish the upload. */ chunkedUploadToStash: function ( file, data, chunkSize, chunkRetries ) { - let promise; - if ( !data.filename ) { throw new Error( 'Filename not included in file data.' ); } - promise = this.chunkedUpload( + const promise = this.chunkedUpload( file, { stash: true, filename: data.filename, ignorewarnings: data.ignorewarnings }, chunkSize, |