Explorar o código

Code style improvements.

Maciej Bukowski %!s(int64=7) %!d(string=hai) anos
pai
achega
1150c2b0af

+ 40 - 51
packages/ckeditor-cloud-services-core/src/token/token.js

@@ -32,7 +32,7 @@ class Token {
 	 */
 	 */
 	constructor( tokenUrlOrRefreshToken, options = DEFAULT_OPTIONS ) {
 	constructor( tokenUrlOrRefreshToken, options = DEFAULT_OPTIONS ) {
 		if ( !tokenUrlOrRefreshToken ) {
 		if ( !tokenUrlOrRefreshToken ) {
-			throw new Error( 'A `tokenUrl` or a `refreshToken` function must be provided as the first constructor argument.' );
+			throw new Error( 'A `tokenUrl` must be provided as the first constructor argument.' );
 		}
 		}
 
 
 		/**
 		/**
@@ -47,30 +47,23 @@ class Token {
 		 */
 		 */
 		this.set( 'value', options.initValue );
 		this.set( 'value', options.initValue );
 
 
-		/**
-		 * Refresh token function.
-		 *
-		 * @member {Function} #_refreshToken
-		 * @protected
-		 */
+		let refresh = () => defaultRefreshToken( tokenUrlOrRefreshToken );
+
 		if ( typeof tokenUrlOrRefreshToken === 'function' ) {
 		if ( typeof tokenUrlOrRefreshToken === 'function' ) {
-			this._refreshToken = () => {
-				return tokenUrlOrRefreshToken()
-					.then( value => this.set( 'value', value ) )
-					.then( () => this );
-			};
-		} else {
-			this._refreshToken = this._defaultRefreshToken.bind( this );
+			refresh = tokenUrlOrRefreshToken;
 		}
 		}
 
 
 		/**
 		/**
-		 * A token Url, which is requested by the {@link #_defaultRefreshToken} function.
-		 * An empty string when the callback is provided in the constructor.
+		 * Refresh token function.
 		 *
 		 *
-		 * @type {String}
-		 * @private
+		 * @member {Function} #_refreshToken
+		 * @protected
 		 */
 		 */
-		this._tokenUrl = typeof tokenUrlOrRefreshToken === 'string' ? tokenUrlOrRefreshToken : '';
+		this._refreshToken = () => {
+			return refresh()
+				.then( value => this.set( 'value', value ) )
+				.then( () => this );
+		};
 
 
 		/**
 		/**
 		 * @type {Object}
 		 * @type {Object}
@@ -102,38 +95,6 @@ class Token {
 		} );
 		} );
 	}
 	}
 
 
-	/**
-	 * The default function to get the new token.
-	 *
-	 * @protected
-	 * @returns {Promise}
-	 */
-	_defaultRefreshToken() {
-		return new Promise( ( resolve, reject ) => {
-			const xhr = new XMLHttpRequest();
-
-			xhr.open( 'GET', this._tokenUrl );
-
-			xhr.addEventListener( 'load', () => {
-				const statusCode = xhr.status;
-				const xhrResponse = xhr.response;
-
-				if ( statusCode < 200 || statusCode > 299 ) {
-					return reject( 'Cannot download new token!' );
-				}
-
-				this.set( 'value', xhrResponse );
-
-				return resolve( this );
-			} );
-
-			xhr.addEventListener( 'error', () => reject( 'Network Error' ) );
-			xhr.addEventListener( 'abort', () => reject( 'Abort' ) );
-
-			xhr.send();
-		} );
-	}
-
 	/**
 	/**
 	 * Starts value refreshing every `refreshInterval` time.
 	 * Starts value refreshing every `refreshInterval` time.
 	 *
 	 *
@@ -181,4 +142,32 @@ mix( Token, ObservableMixin );
  * @returns {Promise.<String>}
  * @returns {Promise.<String>}
  */
  */
 
 
+/**
+ * @private
+ * @param {String} tokenUrl
+ */
+function defaultRefreshToken( tokenUrl ) {
+	return new Promise( ( resolve, reject ) => {
+		const xhr = new XMLHttpRequest();
+
+		xhr.open( 'GET', tokenUrl );
+
+		xhr.addEventListener( 'load', () => {
+			const statusCode = xhr.status;
+			const xhrResponse = xhr.response;
+
+			if ( statusCode < 200 || statusCode > 299 ) {
+				return reject( 'Cannot download new token!' );
+			}
+
+			return resolve( xhrResponse );
+		} );
+
+		xhr.addEventListener( 'error', () => reject( 'Network Error' ) );
+		xhr.addEventListener( 'abort', () => reject( 'Abort' ) );
+
+		xhr.send();
+	} );
+};
+
 export default Token;
 export default Token;

+ 1 - 1
packages/ckeditor-cloud-services-core/tests/token/token.js

@@ -27,7 +27,7 @@ describe( 'Token', () => {
 	describe( 'constructor()', () => {
 	describe( 'constructor()', () => {
 		it( 'should throw error when no tokenUrl provided', () => {
 		it( 'should throw error when no tokenUrl provided', () => {
 			expect( () => new Token() ).to.throw(
 			expect( () => new Token() ).to.throw(
-				'A `tokenUrl` or a `tokenRefresh` function must be provided as the first constructor argument.'
+				'A `tokenUrl` must be provided as the first constructor argument.'
 			);
 			);
 		} );
 		} );