8
0
Просмотр исходного кода

Internal: Added static method to creating a initialized token and updated docs for upload gateway.

Bartosz Czerwonka 8 лет назад
Родитель
Сommit
47ea7495ce

+ 16 - 0
packages/ckeditor-cloud-services-core/src/token/token.js

@@ -125,6 +125,22 @@ class Token {
 	stopRefreshing() {
 		clearInterval( this._refreshInterval );
 	}
+
+	/**
+	 * 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.
+	 * @returns {Promise.<Token>}
+	 */
+	static create( tokenUrl, initTokenValue, options = DEFAULT_OPTIONS ) {
+		const token = new Token( tokenUrl, initTokenValue, options );
+
+		return token.init();
+	}
 }
 
 mix( Token, ObservableMixin );

+ 4 - 2
packages/ckeditor-cloud-services-core/src/uploadgateway/uploadgateway.js

@@ -48,7 +48,8 @@ export default class UploadGateway {
 	 * The file is being sent at a time when the method {@link FileUploader#then then} is called
 	 * or when {@link FileUploader#send send} method is called.
 	 *
-	 *     new UploadGateway( 'JSON_WEB_TOKEN_FROM_SERVER', 'https://example.org' )
+	 *     const token = await Token.create( 'https://token-endpoint' );
+	 *     new UploadGateway( token, 'https://example.org' )
 	 *        .upload( 'FILE' )
 	 *        .onProgress( ( data ) => console.log( data ) )
 	 *        .send()
@@ -56,7 +57,8 @@ export default class UploadGateway {
 	 *
 	 *     // OR
 	 *
-	 *     new UploadGateway( 'JSON_WEB_TOKEN_FROM_SERVER', 'https://example.org' )
+	 *     const token = await Token.create( 'https://token-endpoint' );
+	 *     new UploadGateway( token, 'https://example.org' )
 	 *         .upload( 'FILE' )
 	 *         .onProgress( ( data ) => console.log( data ) )
 	 *         .send()

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

@@ -185,4 +185,17 @@ describe( 'Token', () => {
 				} );
 		} );
 	} );
+
+	describe( 'static create()', () => {
+		it( 'should return a initialized token', done => {
+			Token.create( 'http://token-endpoint', '', { startAutoRefresh: false } )
+				.then( token => {
+					expect( token.value ).to.equal( 'token-value' );
+
+					done();
+				} );
+
+			requests[ 0 ].respond( 200, '', 'token-value' );
+		} );
+	} );
 } );