ソースを参照

Internal: Chnaged public methods to protected (SRP) and renamed options.

Bartosz Czerwonka 8 年 前
コミット
221d8cde46

+ 23 - 19
packages/ckeditor-cloud-services-core/src/token/token.js

@@ -10,7 +10,7 @@
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
 
-const DEFAULT_OPTIONS = { refreshIntervalTime: 3600000, startAutoRefresh: true };
+const DEFAULT_OPTIONS = { refreshInterval: 3600000, autoRefresh: true };
 
 /**
  * Class representing the token used for communication with CKEditor Cloud Services.
@@ -23,12 +23,12 @@ class Token {
 	 * Creates `Token` instance.
 	 *
 	 * @param {String} tokenUrl Endpoint address to download the token.
-	 * @param {String} [initTokenValue] Initial value of the token.
 	 * @param {Object} options
-	 * @param {Number} [options.refreshIntervalTime=3600000] Delay between refreshes. Default 1 hour.
-	 * @param {Boolean} [options.startAutoRefresh=true] Specifies whether to start the refresh automatically.
+	 * @param {String} [options.initTokenValue] Initial value of the token.
+	 * @param {Number} [options.refreshInterval=3600000] Delay between refreshes. Default 1 hour.
+	 * @param {Boolean} [options.autoRefresh=true] Specifies whether to start the refresh automatically.
 	 */
-	constructor( tokenUrl, initTokenValue, options = DEFAULT_OPTIONS ) {
+	constructor( tokenUrl, options = DEFAULT_OPTIONS ) {
 		if ( !tokenUrl ) {
 			throw new Error( '`tokenUrl` must be provided' );
 		}
@@ -42,7 +42,7 @@ class Token {
 		 * @readonly
 		 * @memberOf Token#
 		 */
-		this.set( 'value', initTokenValue );
+		this.set( 'value', options.initTokenValue );
 
 		/**
 		 * @type {String}
@@ -59,18 +59,17 @@ class Token {
 
 	/**
 	 * Initializes the token.
-	 * If `initTokenValue` is not provided then the value of the token is download from url.
 	 *
 	 * @returns {Promise.<Token>}
 	 */
 	init() {
 		return new Promise( ( resolve, reject ) => {
-			if ( this._options.startAutoRefresh ) {
-				this.startRefreshing();
+			if ( this._options.autoRefresh ) {
+				this._startRefreshing();
 			}
 
 			if ( !this.value ) {
-				this.refreshToken()
+				this._refreshToken()
 					.then( resolve )
 					.catch( reject );
 
@@ -84,9 +83,10 @@ class Token {
 	/**
 	 * Gets the new token.
 	 *
+	 * @protected
 	 * @returns {Promise.<Token>}
 	 */
-	refreshToken() {
+	_refreshToken() {
 		return new Promise( ( resolve, reject ) => {
 			const xhr = new XMLHttpRequest();
 
@@ -114,15 +114,19 @@ class Token {
 
 	/**
 	 * Starts value refreshing every `refreshInterval` time.
+	 *
+	 * @protected
 	 */
-	startRefreshing() {
-		this._refreshInterval = setInterval( this.refreshToken.bind( this ), this._options.refreshIntervalTime );
+	_startRefreshing() {
+		this._refreshInterval = setInterval( this._refreshToken.bind( this ), this._options.refreshInterval );
 	}
 
 	/**
 	 * Stops value refreshing.
+	 *
+	 * @protected
 	 */
-	stopRefreshing() {
+	_stopRefreshing() {
 		clearInterval( this._refreshInterval );
 	}
 
@@ -130,14 +134,14 @@ class Token {
 	 * Creates a initialized {@link Token} instance.
 	 *
 	 * @param {String} tokenUrl Endpoint address to download the token.
-	 * @param {String} [initTokenValue] Initial value of the token.
 	 * @param {Object} options
-	 * @param {Number} [options.refreshIntervalTime=3600000] Delay between refreshes. Default 1 hour.
-	 * @param {Boolean} [options.startAutoRefresh=true] Specifies whether to start the refresh automatically.
+	 * @param {String} [options.initTokenValue] Initial value of the token.
+	 * @param {Number} [options.refreshInterval=3600000] Delay between refreshes. Default 1 hour.
+	 * @param {Boolean} [options.autoRefresh=true] Specifies whether to start the refresh automatically.
 	 * @returns {Promise.<Token>}
 	 */
-	static create( tokenUrl, initTokenValue, options = DEFAULT_OPTIONS ) {
-		const token = new Token( tokenUrl, initTokenValue, options );
+	static create( tokenUrl, options = DEFAULT_OPTIONS ) {
+		const token = new Token( tokenUrl, options );
 
 		return token.init();
 	}

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

@@ -30,13 +30,13 @@ describe( 'Token', () => {
 		} );
 
 		it( 'should set a init token value', () => {
-			const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
+			const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
 
 			expect( token.value ).to.equal( 'initValue' );
 		} );
 
 		it( 'should fire `change:value` event if the value of the token has changed', done => {
-			const token = new Token( 'http://token-endpoint', '', { startAutoRefresh: false } );
+			const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
 
 			token.on( 'change:value', ( event, name, newValue ) => {
 				expect( newValue ).to.equal( 'token-value' );
@@ -52,7 +52,7 @@ describe( 'Token', () => {
 
 	describe( 'init()', () => {
 		it( 'should get a token value from endpoint', done => {
-			const token = new Token( 'http://token-endpoint', '', { startAutoRefresh: false } );
+			const token = new Token( 'http://token-endpoint', { autoRefresh: false } );
 
 			token.init()
 				.then( () => {
@@ -67,7 +67,7 @@ describe( 'Token', () => {
 		it( 'should start token refresh every 1 hour', done => {
 			const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
 
-			const token = new Token( 'http://token-endpoint', 'initValues' );
+			const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue' } );
 
 			token.init()
 				.then( () => {
@@ -86,11 +86,11 @@ describe( 'Token', () => {
 		} );
 	} );
 
-	describe( 'refreshToken()', () => {
+	describe( '_refreshToken()', () => {
 		it( 'should get a token from the specified address', done => {
-			const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
+			const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
 
-			token.refreshToken()
+			token._refreshToken()
 				.then( newToken => {
 					expect( newToken.value ).to.equal( 'token-value' );
 
@@ -101,9 +101,9 @@ describe( 'Token', () => {
 		} );
 
 		it( 'should throw error when cannot download new token ', done => {
-			const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
+			const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
 
-			token.refreshToken()
+			token._refreshToken()
 				.catch( error => {
 					expect( error ).to.equal( 'Cannot download new token!' );
 
@@ -114,9 +114,9 @@ describe( 'Token', () => {
 		} );
 
 		it( 'should throw error when response is aborted', done => {
-			const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
+			const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
 
-			token.refreshToken()
+			token._refreshToken()
 				.catch( error => {
 					expect( error ).to.equal( 'Abort' );
 
@@ -127,9 +127,9 @@ describe( 'Token', () => {
 		} );
 
 		it( 'should throw error event when network error occurs', done => {
-			const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
+			const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
 
-			token.refreshToken()
+			token._refreshToken()
 				.catch( error => {
 					expect( error ).to.equal( 'Network Error' );
 
@@ -140,13 +140,13 @@ describe( 'Token', () => {
 		} );
 	} );
 
-	describe( 'startRefreshing()', () => {
+	describe( '_startRefreshing()', () => {
 		it( 'should start refreshing', () => {
 			const clock = sinon.useFakeTimers( { toFake: [ 'setInterval' ] } );
 
-			const token = new Token( 'http://token-endpoint', 'initValue', { startAutoRefresh: false } );
+			const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue', autoRefresh: false } );
 
-			token.startRefreshing();
+			token._startRefreshing();
 
 			clock.tick( 3600000 );
 			clock.tick( 3600000 );
@@ -160,11 +160,11 @@ describe( 'Token', () => {
 		} );
 	} );
 
-	describe( 'stopRefreshing()', () => {
+	describe( '_stopRefreshing()', () => {
 		it( 'should stop refreshing', done => {
 			const clock = sinon.useFakeTimers( { toFake: [ 'setInterval', 'clearInterval' ] } );
 
-			const token = new Token( 'http://token-endpoint', 'initValue' );
+			const token = new Token( 'http://token-endpoint', { initTokenValue: 'initValue' } );
 
 			token.init()
 				.then( () => {
@@ -172,7 +172,7 @@ describe( 'Token', () => {
 					clock.tick( 3600000 );
 					clock.tick( 3600000 );
 
-					token.stopRefreshing();
+					token._stopRefreshing();
 
 					clock.tick( 3600000 );
 					clock.tick( 3600000 );
@@ -188,7 +188,7 @@ describe( 'Token', () => {
 
 	describe( 'static create()', () => {
 		it( 'should return a initialized token', done => {
-			Token.create( 'http://token-endpoint', '', { startAutoRefresh: false } )
+			Token.create( 'http://token-endpoint', { autoRefresh: false } )
 				.then( token => {
 					expect( token.value ).to.equal( 'token-value' );
 

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

@@ -15,7 +15,7 @@ const BASE_64_FILE = 'data:image/gif;base64,R0lGODlhCQAJAPIAAGFhYZXK/1FRUf///' +
 	'9ra2gD/AAAAAAAAACH5BAEAAAUALAAAAAAJAAkAAAMYWFqwru2xERcYJLSNNWNBVimC5wjfaTkJADs=';
 
 describe( 'FileUploader', () => {
-	const token = new Token( 'url', 'token', { startAutoRefresh: false } );
+	const token = new Token( 'url', { initTokenValue: 'token', autoRefresh: false } );
 
 	let fileUploader;
 

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

@@ -10,7 +10,7 @@ import UploadGateway from '../../src/uploadgateway/uploadgateway';
 import Token from '../../src/token/token';
 
 describe( 'UploadGateway', () => {
-	const token = new Token( 'url', 'token', { startAutoRefresh: false } );
+	const token = new Token( 'url', { initTokenValue: 'token', autoRefresh: false } );
 
 	describe( 'constructor()', () => {
 		it( 'should throw error when no token provided', () => {