JsonTokenManager is a TypeScript library for managing API tokens. It handles token storage, lifecycle management, and automatic refreshing, supporting multiple retry attempts and customizable configurations.
JsonTokenManager simplifies the management of API tokens by automating the refresh process and handling token expiration seamlessly. The library is designed to be configurable, allowing you to tailor it to your specific API requirements. Key features include automatic token refreshing, support for various configuration options, and the ability to manually refresh tokens when needed. Future improvements will include optimized storage solutions, enhanced security measures, concurrent queue management, and token caching.
Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue on GitHub. Feel free to fork the repository and submit pull requests for any enhancements or bug fixes.
- Automatic token refreshing based on expiration time.
- Configurable retry logic for token refresh attempts.
- Support for customizable request headers and body.
- Buffer time to refresh tokens before they expire.
- Manual token refresh capability.
- Optimized Storage : Improve storage by moving credentials and tokens out of memory to a more secure and efficient storage solution.
- Enhanced Security : Implement additional security measures for storing and handling tokens and credentials.
- Concurrent Queue Management : Introduce a concurrent queue to handle multiple token queries efficiently.
- Token Caching : Implement a caching mechanism to store and retrieve tokens efficiently, reducing unnecessary token refresh requests.
Install the package using npm:
npm install json-token-manager
Import the JsonTokenManager
class in your TypeScript file:
import JsonTokenManager from 'json-token-manager';
Create a configuration object with the necessary details:
const config = {
tokenUrl: 'https://api.example.com/oauth/token',
body: {
grant_type: 'client_credentials',
client_id: 'your-client-id',
client_secret: 'your-client-secret'
},
header_list: {
'Content-Type': 'application/json'
},
tokenField: 'access_token',
expireField: 'expires_in',
retryAttempts: 3, // Optional, default is 3
retryDelay: 1000, // Optional, default is 1000ms
bufferTime: 60000 // Optional, default is 60000ms (1 minute)
};
Create an instance of JsonTokenManager
with the configuration:
const tokenManager = new JsonTokenManager(config);
Use the getToken
method to obtain a valid token. This method will refresh the token if it is expired or about to expire.
const token = await tokenManager.getToken();
console.log(token);
If needed, you can manually refresh the token using the manualRefresh
method:
await tokenManager.manualRefresh();
Here's a complete example of using JsonTokenManager
to make an API request:
import JsonTokenManager from 'json-token-manager';
import axios from 'axios';
const config = {
tokenUrl: 'https://api.example.com/oauth/token',
body: {
grant_type: 'client_credentials',
client_id: 'your-client-id',
client_secret: 'your-client-secret'
},
header_list: {
'Content-Type': 'application/json'
},
tokenField: 'access_token',
expireField: 'expires_in'
};
const tokenManager = new JsonTokenManager(config);
(async () => {
try {
const token = await tokenManager.getToken();
const response = await axios.get('https://api.example.com/resource', {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
console.log(response.data);
} catch (error) {
console.error('Error making API request:', error);
}
})();
tokenUrl
: The URL to request a new token.body
: The body of the token request.header_list
: Headers for the token request.tokenField
: The field in the response containing the token.expireField
: The field in the response containing the expiration time (in seconds).retryAttempts
: (Optional) Number of retry attempts for token refresh (default is 3).retryDelay
: (Optional) Delay between retry attempts in milliseconds (default is 1000ms).bufferTime
: (Optional) Buffer time in milliseconds to refresh token before expiry (default is 60000ms or 1 minute).
If the token refresh fails, the JsonTokenManager
will retry based on the configured retryAttempts
and retryDelay
. If all attempts fail, an error is thrown.
This project is licensed under the MIT License.