@ckeditor/ckeditor5-cloud-services 36.0.1 → 37.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module cloud-services/cloudservicescore
7
+ */
8
+ import { ContextPlugin } from 'ckeditor5/src/core';
9
+ import type { TokenUrl } from './cloudservicesconfig';
10
+ import Token, { type InitializedToken, type TokenOptions } from './token/token';
11
+ import UploadGateway from './uploadgateway/uploadgateway';
12
+ /**
13
+ * The `CloudServicesCore` plugin exposes the base API for communication with CKEditor Cloud Services.
14
+ */
15
+ export default class CloudServicesCore extends ContextPlugin {
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ static get pluginName(): 'CloudServicesCore';
20
+ /**
21
+ * Creates the {@link module:cloud-services/token~Token} instance.
22
+ *
23
+ * @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
24
+ * value is a function it has to match the {@link module:cloud-services/token~refreshToken} interface.
25
+ * @param options.initValue Initial value of the token.
26
+ * @param options.autoRefresh Specifies whether to start the refresh automatically.
27
+ */
28
+ createToken(tokenUrlOrRefreshToken: TokenUrl, options?: TokenOptions): Token;
29
+ /**
30
+ * Creates the {@link module:cloud-services/uploadgateway/uploadgateway~UploadGateway} instance.
31
+ *
32
+ * @param token Token used for authentication.
33
+ * @param apiAddress API address.
34
+ */
35
+ createUploadGateway(token: InitializedToken, apiAddress: string): UploadGateway;
36
+ }
37
+ declare module '@ckeditor/ckeditor5-core' {
38
+ interface PluginsMap {
39
+ [CloudServicesCore.pluginName]: CloudServicesCore;
40
+ }
41
+ }
@@ -2,50 +2,40 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module cloud-services/cloudservicescore
8
7
  */
9
-
10
8
  import { ContextPlugin } from 'ckeditor5/src/core';
11
9
  import Token from './token/token';
12
10
  import UploadGateway from './uploadgateway/uploadgateway';
13
-
14
11
  /**
15
12
  * The `CloudServicesCore` plugin exposes the base API for communication with CKEditor Cloud Services.
16
- *
17
- * @extends module:core/contextplugin~ContextPlugin
18
13
  */
19
14
  export default class CloudServicesCore extends ContextPlugin {
20
- /**
21
- * @inheritDoc
22
- */
23
- static get pluginName() {
24
- return 'CloudServicesCore';
25
- }
26
-
27
- /**
28
- * Creates the {@link module:cloud-services/token~Token} instance.
29
- *
30
- * @param {String|Function} tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
31
- * value is a function it has to match the {@link module:cloud-services/token~refreshToken} interface.
32
- * @param {Object} [options]
33
- * @param {String} [options.initValue] Initial value of the token.
34
- * @param {Boolean} [options.autoRefresh=true] Specifies whether to start the refresh automatically.
35
- * @returns {module:cloud-services/token~Token}
36
- */
37
- createToken( tokenUrlOrRefreshToken, options ) {
38
- return new Token( tokenUrlOrRefreshToken, options );
39
- }
40
-
41
- /**
42
- * Creates the {@link module:cloud-services/uploadgateway/uploadgateway~UploadGateway} instance.
43
- *
44
- * @param {module:cloud-services/token~Token} token Token used for authentication.
45
- * @param {String} apiAddress API address.
46
- * @returns {module:cloud-services/uploadgateway/uploadgateway~UploadGateway}
47
- */
48
- createUploadGateway( token, apiAddress ) {
49
- return new UploadGateway( token, apiAddress );
50
- }
15
+ /**
16
+ * @inheritDoc
17
+ */
18
+ static get pluginName() {
19
+ return 'CloudServicesCore';
20
+ }
21
+ /**
22
+ * Creates the {@link module:cloud-services/token~Token} instance.
23
+ *
24
+ * @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
25
+ * value is a function it has to match the {@link module:cloud-services/token~refreshToken} interface.
26
+ * @param options.initValue Initial value of the token.
27
+ * @param options.autoRefresh Specifies whether to start the refresh automatically.
28
+ */
29
+ createToken(tokenUrlOrRefreshToken, options) {
30
+ return new Token(tokenUrlOrRefreshToken, options);
31
+ }
32
+ /**
33
+ * Creates the {@link module:cloud-services/uploadgateway/uploadgateway~UploadGateway} instance.
34
+ *
35
+ * @param token Token used for authentication.
36
+ * @param apiAddress API address.
37
+ */
38
+ createUploadGateway(token, apiAddress) {
39
+ return new UploadGateway(token, apiAddress);
40
+ }
51
41
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module cloud-services
7
+ */
8
+ export { default as CloudServices } from './cloudservices';
9
+ export { default as CloudServicesCore } from './cloudservicescore';
10
+ export { TokenUrl } from './cloudservicesconfig';
11
+ export type { default as Token, InitializedToken } from './token/token';
12
+ export type { default as UploadGateway } from './uploadgateway/uploadgateway';
13
+ export type { default as FileUploader } from './uploadgateway/fileuploader';
package/src/index.js CHANGED
@@ -2,10 +2,8 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module cloud-services
8
7
  */
9
-
10
8
  export { default as CloudServices } from './cloudservices';
11
9
  export { default as CloudServicesCore } from './cloudservicescore';
@@ -0,0 +1,96 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ import type { TokenUrl } from '../cloudservicesconfig';
6
+ declare const Token_base: {
7
+ new (): import("ckeditor5/src/utils").Observable;
8
+ prototype: import("ckeditor5/src/utils").Observable;
9
+ };
10
+ /**
11
+ * Class representing the token used for communication with CKEditor Cloud Services.
12
+ * Value of the token is retrieving from the specified URL and is refreshed every 1 hour by default.
13
+ */
14
+ export default class Token extends Token_base {
15
+ /**
16
+ * Value of the token.
17
+ * The value of the token is undefined if `initValue` is not provided or `init` method was not called.
18
+ * `create` method creates token with initialized value from url.
19
+ *
20
+ * @see module:cloud-services/token~InitializedToken
21
+ * @observable
22
+ * @readonly
23
+ */
24
+ value: string | undefined;
25
+ /**
26
+ * Base refreshing function.
27
+ */
28
+ private _refresh;
29
+ private _options;
30
+ private _tokenRefreshTimeout?;
31
+ /**
32
+ * Creates `Token` instance.
33
+ * Method `init` should be called after using the constructor or use `create` method instead.
34
+ *
35
+ * @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
36
+ * value is a function it has to match the {@link module:cloud-services/token~refreshToken} interface.
37
+ */
38
+ constructor(tokenUrlOrRefreshToken: TokenUrl, options?: TokenOptions);
39
+ /**
40
+ * Initializes the token.
41
+ */
42
+ init(): Promise<InitializedToken>;
43
+ /**
44
+ * Refresh token method. Useful in a method form as it can be override in tests.
45
+ */
46
+ refreshToken(): Promise<InitializedToken>;
47
+ /**
48
+ * Destroys token instance. Stops refreshing.
49
+ */
50
+ destroy(): void;
51
+ /**
52
+ * Checks whether the provided token follows the JSON Web Tokens (JWT) format.
53
+ *
54
+ * @param tokenValue The token to validate.
55
+ */
56
+ private _validateTokenValue;
57
+ /**
58
+ * Registers a refresh token timeout for the time taken from token.
59
+ */
60
+ private _registerRefreshTokenTimeout;
61
+ /**
62
+ * Returns token refresh timeout time calculated from expire time in the token payload.
63
+ *
64
+ * If the token parse fails or the token payload doesn't contain, the default DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME is returned.
65
+ */
66
+ private _getTokenRefreshTimeoutTime;
67
+ /**
68
+ * Creates a initialized {@link module:cloud-services/token~Token} instance.
69
+ *
70
+ * @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
71
+ * value is a function it has to match the {@link module:cloud-services/token~refreshToken} interface.
72
+ */
73
+ static create(tokenUrlOrRefreshToken: TokenUrl, options?: TokenOptions): Promise<InitializedToken>;
74
+ }
75
+ /**
76
+ * A {@link ~Token} instance that has been initialized.
77
+ */
78
+ export type InitializedToken = Token & {
79
+ value: string;
80
+ };
81
+ /**
82
+ * Options for creating tokens.
83
+ */
84
+ export interface TokenOptions {
85
+ /**
86
+ * Initial value of the token.
87
+ */
88
+ initValue?: string;
89
+ /**
90
+ * Specifies whether to start the refresh automatically.
91
+ *
92
+ * @default true
93
+ */
94
+ autoRefresh?: boolean;
95
+ }
96
+ export {};
@@ -2,258 +2,170 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module cloud-services/token
8
7
  */
9
-
10
8
  /* globals XMLHttpRequest, setTimeout, clearTimeout, atob */
11
-
12
- import { mix, ObservableMixin, CKEditorError } from 'ckeditor5/src/utils';
13
-
9
+ import { ObservableMixin, CKEditorError } from 'ckeditor5/src/utils';
14
10
  const DEFAULT_OPTIONS = { autoRefresh: true };
15
11
  const DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME = 3600000;
16
-
17
12
  /**
18
13
  * Class representing the token used for communication with CKEditor Cloud Services.
19
14
  * Value of the token is retrieving from the specified URL and is refreshed every 1 hour by default.
20
- *
21
- * @mixes ObservableMixin
22
15
  */
23
- class Token {
24
- /**
25
- * Creates `Token` instance.
26
- * Method `init` should be called after using the constructor or use `create` method instead.
27
- *
28
- * @param {String|Function} tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
29
- * value is a function it has to match the {@link module:cloud-services/token~refreshToken} interface.
30
- * @param {Object} options
31
- * @param {String} [options.initValue] Initial value of the token.
32
- * @param {Boolean} [options.autoRefresh=true] Specifies whether to start the refresh automatically.
33
- */
34
- constructor( tokenUrlOrRefreshToken, options = DEFAULT_OPTIONS ) {
35
- if ( !tokenUrlOrRefreshToken ) {
36
- /**
37
- * A `tokenUrl` must be provided as the first constructor argument.
38
- *
39
- * @error token-missing-token-url
40
- */
41
- throw new CKEditorError(
42
- 'token-missing-token-url',
43
- this
44
- );
45
- }
46
-
47
- if ( options.initValue ) {
48
- this._validateTokenValue( options.initValue );
49
- }
50
-
51
- /**
52
- * Value of the token.
53
- * The value of the token is null if `initValue` is not provided or `init` method was not called.
54
- * `create` method creates token with initialized value from url.
55
- *
56
- * @name value
57
- * @member {String} #value
58
- * @observable
59
- * @readonly
60
- */
61
- this.set( 'value', options.initValue );
62
-
63
- /**
64
- * Base refreshing function.
65
- *
66
- * @private
67
- * @member {String|Function} #_refresh
68
- */
69
- if ( typeof tokenUrlOrRefreshToken === 'function' ) {
70
- this._refresh = tokenUrlOrRefreshToken;
71
- } else {
72
- this._refresh = () => defaultRefreshToken( tokenUrlOrRefreshToken );
73
- }
74
-
75
- /**
76
- * @type {Object}
77
- * @private
78
- */
79
- this._options = Object.assign( {}, DEFAULT_OPTIONS, options );
80
- }
81
-
82
- /**
83
- * Initializes the token.
84
- *
85
- * @returns {Promise.<module:cloud-services/token~Token>}
86
- */
87
- init() {
88
- return new Promise( ( resolve, reject ) => {
89
- if ( !this.value ) {
90
- this.refreshToken()
91
- .then( resolve )
92
- .catch( reject );
93
-
94
- return;
95
- }
96
-
97
- if ( this._options.autoRefresh ) {
98
- this._registerRefreshTokenTimeout();
99
- }
100
-
101
- resolve( this );
102
- } );
103
- }
104
-
105
- /**
106
- * Refresh token method. Useful in a method form as it can be override in tests.
107
- * @returns {Promise.<String>}
108
- */
109
- refreshToken() {
110
- return this._refresh()
111
- .then( value => {
112
- this._validateTokenValue( value );
113
- this.set( 'value', value );
114
-
115
- if ( this._options.autoRefresh ) {
116
- this._registerRefreshTokenTimeout();
117
- }
118
- } )
119
- .then( () => this );
120
- }
121
-
122
- /**
123
- * Destroys token instance. Stops refreshing.
124
- */
125
- destroy() {
126
- clearTimeout( this._tokenRefreshTimeout );
127
- }
128
-
129
- /**
130
- * Checks whether the provided token follows the JSON Web Tokens (JWT) format.
131
- *
132
- * @protected
133
- * @param {String} tokenValue The token to validate.
134
- */
135
- _validateTokenValue( tokenValue ) {
136
- // The token must be a string.
137
- const isString = typeof tokenValue === 'string';
138
-
139
- // The token must be a plain string without quotes ("").
140
- const isPlainString = !/^".*"$/.test( tokenValue );
141
-
142
- // JWT token contains 3 parts: header, payload, and signature.
143
- // Each part is separated by a dot.
144
- const isJWTFormat = isString && tokenValue.split( '.' ).length === 3;
145
-
146
- if ( !( isPlainString && isJWTFormat ) ) {
147
- /**
148
- * The provided token must follow the [JSON Web Tokens](https://jwt.io/introduction/) format.
149
- *
150
- * @error token-not-in-jwt-format
151
- */
152
- throw new CKEditorError( 'token-not-in-jwt-format', this );
153
- }
154
- }
155
-
156
- /**
157
- * Registers a refresh token timeout for the time taken from token.
158
- *
159
- * @protected
160
- */
161
- _registerRefreshTokenTimeout() {
162
- const tokenRefreshTimeoutTime = this._getTokenRefreshTimeoutTime();
163
-
164
- clearTimeout( this._tokenRefreshTimeout );
165
-
166
- this._tokenRefreshTimeout = setTimeout( () => {
167
- this.refreshToken();
168
- }, tokenRefreshTimeoutTime );
169
- }
170
-
171
- /**
172
- * Returns token refresh timeout time calculated from expire time in the token payload.
173
- *
174
- * If the token parse fails or the token payload doesn't contain, the default DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME is returned.
175
- *
176
- * @protected
177
- * @returns {Number}
178
- */
179
- _getTokenRefreshTimeoutTime() {
180
- try {
181
- const [ , binaryTokenPayload ] = this.value.split( '.' );
182
- const { exp: tokenExpireTime } = JSON.parse( atob( binaryTokenPayload ) );
183
-
184
- if ( !tokenExpireTime ) {
185
- return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
186
- }
187
-
188
- const tokenRefreshTimeoutTime = Math.floor( ( ( tokenExpireTime * 1000 ) - Date.now() ) / 2 );
189
-
190
- return tokenRefreshTimeoutTime;
191
- } catch ( err ) {
192
- return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
193
- }
194
- }
195
-
196
- /**
197
- * Creates a initialized {@link module:cloud-services/token~Token} instance.
198
- *
199
- * @param {String|Function} tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
200
- * value is a function it has to match the {@link module:cloud-services/token~refreshToken} interface.
201
- * @param {Object} options
202
- * @param {String} [options.initValue] Initial value of the token.
203
- * @param {Boolean} [options.autoRefresh=true] Specifies whether to start the refresh automatically.
204
- * @returns {Promise.<module:cloud-services/token~Token>}
205
- */
206
- static create( tokenUrlOrRefreshToken, options = DEFAULT_OPTIONS ) {
207
- const token = new Token( tokenUrlOrRefreshToken, options );
208
-
209
- return token.init();
210
- }
16
+ export default class Token extends ObservableMixin() {
17
+ /**
18
+ * Creates `Token` instance.
19
+ * Method `init` should be called after using the constructor or use `create` method instead.
20
+ *
21
+ * @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
22
+ * value is a function it has to match the {@link module:cloud-services/token~refreshToken} interface.
23
+ */
24
+ constructor(tokenUrlOrRefreshToken, options = {}) {
25
+ super();
26
+ if (!tokenUrlOrRefreshToken) {
27
+ /**
28
+ * A `tokenUrl` must be provided as the first constructor argument.
29
+ *
30
+ * @error token-missing-token-url
31
+ */
32
+ throw new CKEditorError('token-missing-token-url', this);
33
+ }
34
+ if (options.initValue) {
35
+ this._validateTokenValue(options.initValue);
36
+ }
37
+ this.set('value', options.initValue);
38
+ if (typeof tokenUrlOrRefreshToken === 'function') {
39
+ this._refresh = tokenUrlOrRefreshToken;
40
+ }
41
+ else {
42
+ this._refresh = () => defaultRefreshToken(tokenUrlOrRefreshToken);
43
+ }
44
+ this._options = { ...DEFAULT_OPTIONS, ...options };
45
+ }
46
+ /**
47
+ * Initializes the token.
48
+ */
49
+ init() {
50
+ return new Promise((resolve, reject) => {
51
+ if (!this.value) {
52
+ this.refreshToken()
53
+ .then(resolve)
54
+ .catch(reject);
55
+ return;
56
+ }
57
+ if (this._options.autoRefresh) {
58
+ this._registerRefreshTokenTimeout();
59
+ }
60
+ resolve(this);
61
+ });
62
+ }
63
+ /**
64
+ * Refresh token method. Useful in a method form as it can be override in tests.
65
+ */
66
+ refreshToken() {
67
+ return this._refresh()
68
+ .then(value => {
69
+ this._validateTokenValue(value);
70
+ this.set('value', value);
71
+ if (this._options.autoRefresh) {
72
+ this._registerRefreshTokenTimeout();
73
+ }
74
+ return this;
75
+ });
76
+ }
77
+ /**
78
+ * Destroys token instance. Stops refreshing.
79
+ */
80
+ destroy() {
81
+ clearTimeout(this._tokenRefreshTimeout);
82
+ }
83
+ /**
84
+ * Checks whether the provided token follows the JSON Web Tokens (JWT) format.
85
+ *
86
+ * @param tokenValue The token to validate.
87
+ */
88
+ _validateTokenValue(tokenValue) {
89
+ // The token must be a string.
90
+ const isString = typeof tokenValue === 'string';
91
+ // The token must be a plain string without quotes ("").
92
+ const isPlainString = !/^".*"$/.test(tokenValue);
93
+ // JWT token contains 3 parts: header, payload, and signature.
94
+ // Each part is separated by a dot.
95
+ const isJWTFormat = isString && tokenValue.split('.').length === 3;
96
+ if (!(isPlainString && isJWTFormat)) {
97
+ /**
98
+ * The provided token must follow the [JSON Web Tokens](https://jwt.io/introduction/) format.
99
+ *
100
+ * @error token-not-in-jwt-format
101
+ */
102
+ throw new CKEditorError('token-not-in-jwt-format', this);
103
+ }
104
+ }
105
+ /**
106
+ * Registers a refresh token timeout for the time taken from token.
107
+ */
108
+ _registerRefreshTokenTimeout() {
109
+ const tokenRefreshTimeoutTime = this._getTokenRefreshTimeoutTime();
110
+ clearTimeout(this._tokenRefreshTimeout);
111
+ this._tokenRefreshTimeout = setTimeout(() => {
112
+ this.refreshToken();
113
+ }, tokenRefreshTimeoutTime);
114
+ }
115
+ /**
116
+ * Returns token refresh timeout time calculated from expire time in the token payload.
117
+ *
118
+ * If the token parse fails or the token payload doesn't contain, the default DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME is returned.
119
+ */
120
+ _getTokenRefreshTimeoutTime() {
121
+ try {
122
+ const [, binaryTokenPayload] = this.value.split('.');
123
+ const { exp: tokenExpireTime } = JSON.parse(atob(binaryTokenPayload));
124
+ if (!tokenExpireTime) {
125
+ return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
126
+ }
127
+ const tokenRefreshTimeoutTime = Math.floor(((tokenExpireTime * 1000) - Date.now()) / 2);
128
+ return tokenRefreshTimeoutTime;
129
+ }
130
+ catch (err) {
131
+ return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
132
+ }
133
+ }
134
+ /**
135
+ * Creates a initialized {@link module:cloud-services/token~Token} instance.
136
+ *
137
+ * @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
138
+ * value is a function it has to match the {@link module:cloud-services/token~refreshToken} interface.
139
+ */
140
+ static create(tokenUrlOrRefreshToken, options = {}) {
141
+ const token = new Token(tokenUrlOrRefreshToken, options);
142
+ return token.init();
143
+ }
211
144
  }
212
-
213
- mix( Token, ObservableMixin );
214
-
215
145
  /**
216
146
  * This function is called in a defined interval by the {@link ~Token} class. It also can be invoked manually.
217
147
  * It should return a promise, which resolves with the new token value.
218
148
  * If any error occurs it should return a rejected promise with an error message.
219
- *
220
- * @function refreshToken
221
- * @returns {Promise.<String>}
222
149
  */
223
-
224
- /**
225
- * @private
226
- * @param {String} tokenUrl
227
- */
228
- function defaultRefreshToken( tokenUrl ) {
229
- return new Promise( ( resolve, reject ) => {
230
- const xhr = new XMLHttpRequest();
231
-
232
- xhr.open( 'GET', tokenUrl );
233
-
234
- xhr.addEventListener( 'load', () => {
235
- const statusCode = xhr.status;
236
- const xhrResponse = xhr.response;
237
-
238
- if ( statusCode < 200 || statusCode > 299 ) {
239
- /**
240
- * Cannot download new token from the provided url.
241
- *
242
- * @error token-cannot-download-new-token
243
- */
244
- return reject(
245
- new CKEditorError( 'token-cannot-download-new-token', null )
246
- );
247
- }
248
-
249
- return resolve( xhrResponse );
250
- } );
251
-
252
- xhr.addEventListener( 'error', () => reject( new Error( 'Network Error' ) ) );
253
- xhr.addEventListener( 'abort', () => reject( new Error( 'Abort' ) ) );
254
-
255
- xhr.send();
256
- } );
150
+ function defaultRefreshToken(tokenUrl) {
151
+ return new Promise((resolve, reject) => {
152
+ const xhr = new XMLHttpRequest();
153
+ xhr.open('GET', tokenUrl);
154
+ xhr.addEventListener('load', () => {
155
+ const statusCode = xhr.status;
156
+ const xhrResponse = xhr.response;
157
+ if (statusCode < 200 || statusCode > 299) {
158
+ /**
159
+ * Cannot download new token from the provided url.
160
+ *
161
+ * @error token-cannot-download-new-token
162
+ */
163
+ return reject(new CKEditorError('token-cannot-download-new-token', null));
164
+ }
165
+ return resolve(xhrResponse);
166
+ });
167
+ xhr.addEventListener('error', () => reject(new Error('Network Error')));
168
+ xhr.addEventListener('abort', () => reject(new Error('Abort')));
169
+ xhr.send();
170
+ });
257
171
  }
258
-
259
- export default Token;