@ckeditor/ckeditor-cloud-services-core
Advanced tools
Comparing version 1.0.0 to 2.0.0
{ | ||
"name": "@ckeditor/ckeditor-cloud-services-core", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "CKEditor Cloud Services Core API", | ||
@@ -18,3 +18,3 @@ "license": "GPL-2.0-or-later", | ||
"dependencies": { | ||
"@ckeditor/ckeditor5-utils": "^10.0.0" | ||
"@ckeditor/ckeditor5-utils": "^11.0.0" | ||
}, | ||
@@ -42,3 +42,6 @@ "devDependencies": { | ||
"webpack": "^3.6.0" | ||
} | ||
}, | ||
"files": [ | ||
"src" | ||
] | ||
} |
@@ -26,3 +26,4 @@ /** | ||
* | ||
* @param {String} tokenUrl Endpoint address to download the token. | ||
* @param {String|Function} tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the | ||
* value is a function it has to match the {@link ~refreshToken} interface. | ||
* @param {Object} options | ||
@@ -33,5 +34,5 @@ * @param {String} [options.initValue] Initial value of the token. | ||
*/ | ||
constructor( tokenUrl, options = DEFAULT_OPTIONS ) { | ||
if ( !tokenUrl ) { | ||
throw new Error( '`tokenUrl` must be provided' ); | ||
constructor( tokenUrlOrRefreshToken, options = DEFAULT_OPTIONS ) { | ||
if ( !tokenUrlOrRefreshToken ) { | ||
throw new Error( 'A `tokenUrl` must be provided as the first constructor argument.' ); | ||
} | ||
@@ -45,6 +46,5 @@ | ||
* @name value | ||
* @type {String} | ||
* @member {String} #value | ||
* @observable | ||
* @readonly | ||
* @memberOf Token# | ||
*/ | ||
@@ -54,6 +54,12 @@ this.set( 'value', options.initValue ); | ||
/** | ||
* @type {String} | ||
* Base refreshing function. | ||
* | ||
* @private | ||
* @member {String|Function} #_refresh | ||
*/ | ||
this._tokenUrl = tokenUrl; | ||
if ( typeof tokenUrlOrRefreshToken === 'function' ) { | ||
this._refresh = tokenUrlOrRefreshToken; | ||
} else { | ||
this._refresh = () => defaultRefreshToken( tokenUrlOrRefreshToken ); | ||
} | ||
@@ -91,31 +97,17 @@ /** | ||
/** | ||
* Gets the new token. | ||
* Refresh token method. Useful in a method form as it can be override in tests. | ||
* | ||
* @protected | ||
* @returns {Promise.<Token>} | ||
*/ | ||
_refreshToken() { | ||
return new Promise( ( resolve, reject ) => { | ||
const xhr = new XMLHttpRequest(); | ||
return this._refresh() | ||
.then( value => this.set( 'value', value ) ) | ||
.then( () => this ); | ||
} | ||
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(); | ||
} ); | ||
/** | ||
* Destroys token instance. Stops refreshing. | ||
*/ | ||
destroy() { | ||
this._stopRefreshing(); | ||
} | ||
@@ -129,3 +121,3 @@ | ||
_startRefreshing() { | ||
this._refreshInterval = setInterval( this._refreshToken.bind( this ), this._options.refreshInterval ); | ||
this._refreshInterval = setInterval( () => this._refreshToken(), this._options.refreshInterval ); | ||
} | ||
@@ -145,3 +137,4 @@ | ||
* | ||
* @param {String} tokenUrl Endpoint address to download the token. | ||
* @param {String|Function} tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the | ||
* value is a function it has to match the {@link ~refreshToken} interface. | ||
* @param {Object} options | ||
@@ -153,4 +146,4 @@ * @param {String} [options.initValue] Initial value of the token. | ||
*/ | ||
static create( tokenUrl, options = DEFAULT_OPTIONS ) { | ||
const token = new Token( tokenUrl, options ); | ||
static create( tokenUrlOrRefreshToken, options = DEFAULT_OPTIONS ) { | ||
const token = new Token( tokenUrlOrRefreshToken, options ); | ||
@@ -163,2 +156,39 @@ return token.init(); | ||
/** | ||
* This function is called in a defined interval by the {@link ~Token} class. | ||
* It should return a promise, which resolves with the new token value. | ||
* If any error occurs it should return a rejected promise with an error message. | ||
* | ||
* @function refreshToken | ||
* @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( new Error( 'Cannot download new token!' ) ); | ||
} | ||
return resolve( xhrResponse ); | ||
} ); | ||
xhr.addEventListener( 'error', () => reject( new Error( 'Network Error' ) ) ); | ||
xhr.addEventListener( 'abort', () => reject( new Error( 'Abort' ) ) ); | ||
xhr.send(); | ||
} ); | ||
}; | ||
export default Token; |
@@ -179,3 +179,7 @@ /** | ||
if ( statusCode < 200 || statusCode > 299 ) { | ||
return reject( xhrResponse.message || xhrResponse.error ); | ||
if ( xhrResponse.message ) { | ||
return reject( new Error( xhrResponse.message ) ); | ||
} | ||
return reject( xhrResponse.error ); | ||
} | ||
@@ -186,4 +190,4 @@ | ||
xhr.addEventListener( 'error', () => reject( 'Network Error' ) ); | ||
xhr.addEventListener( 'abort', () => reject( 'Abort' ) ); | ||
xhr.addEventListener( 'error', () => reject( new Error( 'Network Error' ) ) ); | ||
xhr.addEventListener( 'abort', () => reject( new Error( 'Abort' ) ) ); | ||
@@ -190,0 +194,0 @@ xhr.send( formData ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15158
6
438
+ Added@ckeditor/ckeditor5-utils@11.1.0(transitive)
+ Addedlodash-es@4.17.21(transitive)
- Removed@ckeditor/ckeditor5-utils@10.2.1(transitive)