Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Feature: Expose the refreshToken method
Browse files Browse the repository at this point in the history
Expose the refreshToken method.
  • Loading branch information
Bartosz Czerwonka authored Apr 2, 2020
2 parents 04329e8 + 73e7243 commit 013b89e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src/token/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Token {
}

if ( !this.value ) {
this._refreshToken()
this.refreshToken()
.then( resolve )
.catch( reject );

Expand All @@ -102,10 +102,9 @@ class Token {

/**
* Refresh token method. Useful in a method form as it can be override in tests.
*
* @protected
* @returns {Promise.<String>}
*/
_refreshToken() {
refreshToken() {
return this._refresh()
.then( value => this.set( 'value', value ) )
.then( () => this );
Expand All @@ -124,7 +123,7 @@ class Token {
* @protected
*/
_startRefreshing() {
this._refreshInterval = setInterval( () => this._refreshToken(), this._options.refreshInterval );
this._refreshInterval = setInterval( () => this.refreshToken(), this._options.refreshInterval );
}

/**
Expand Down Expand Up @@ -157,7 +156,7 @@ class Token {
mix( Token, ObservableMixin );

/**
* This function is called in a defined interval by the {@link ~Token} class.
* This function is called in a defined interval by the {@link ~Token} class. It also can be invoked manually.
* 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.
*
Expand Down
8 changes: 4 additions & 4 deletions tests/token/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,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: 'initValue', autoRefresh: false } );

token._refreshToken()
token.refreshToken()
.then( newToken => {
expect( newToken.value ).to.equal( 'token-value' );

Expand All @@ -146,7 +146,7 @@ describe( 'Token', () => {
it( 'should get a token from the specified callback function', () => {
const token = new Token( () => Promise.resolve( 'token-value' ), { initValue: 'initValue', autoRefresh: false } );

return token._refreshToken()
return token.refreshToken()
.then( newToken => {
expect( newToken.value ).to.equal( 'token-value' );
} );
Expand Down Expand Up @@ -195,7 +195,7 @@ describe( 'Token', () => {
it( 'should throw an error when the callback throws error', () => {
const token = new Token( () => Promise.reject( 'Custom error occurred' ), { initValue: 'initValue', autoRefresh: false } );

token._refreshToken()
token.refreshToken()
.catch( error => {
expect( error ).to.equal( 'Custom error occurred' );
} );
Expand Down

0 comments on commit 013b89e

Please sign in to comment.