@sonatel-os/juf
Version:
It's an helpful Javascript Utility Framework to ease application development in SONATEL context.
83 lines (82 loc) • 3.24 kB
TypeScript
export default Authentication;
export type Cache = import("../utils/cache.js").default;
/**
* @class Authentication
* @classdesc Provides methods to handle authentication and token management with the Sonatel API Platform.
* Utilizes caching to store tokens and optimizes API requests by reducing unnecessary network calls.
*
* @example
* // Import the service
* import Authentication from './services/Authentication.js';
*
* // Initialize the service using the factory method
* const authentication = Authentication.init();
*
* // Use the debug method
* (async () => {
* try {
* const { data, status } = await authentication.debug();
* console.log('Authentication successful:', data, 'Status:', status);
* } catch (error) {
* console.error('Authentication failed:', error.status, error.data);
* }
* })();
*
* @typedef {import('../utils/cache.js').default} Cache
*/
declare class Authentication {
/**
* Factory method to initialize Authentication with the provided configuration.
* This method retrieves the necessary configuration from convict and returns an instance.
* @memberof Service\Authentication
* @method init
* @returns {Authentication} An initialized instance of Authentication.
*/
static init(): Authentication;
/**
* Private constructor to prevent direct instantiation.
* Use the {@link Authentication.init} method to create an instance.
*
* @param {Object} config - Configuration object for API endpoints and credentials.
* @param {Object} config.url - URLs for production, sandbox, and pre-production environments.
* @param {string} config.client_id - Apigee client ID.
* @param {string} config.client_secret - Apigee client secret.
* @param {boolean} config.onProd - Flag indicating if production environment is active.
* @param {boolean} config.onPProd - Flag indicating if pre-production environment is active.
*/
constructor(config: {
url: Object;
client_id: string;
client_secret: string;
onProd: boolean;
onPProd: boolean;
});
/**
* @async
* @method debug
* @memberof Service\Authentication
* @description Authenticates against the Sonatel API Platform using the client credentials flow.
* Caches the access token to reduce redundant authentication calls.
*
* @returns {Promise<{ access_token: string, expires_in: number, refresh_expires_in: number, token_type: string, notbefore_policy: number, session_state: string }>}
* A promise resolving with the authentication response data and HTTP status code.
*
* @throws { error: string, error_description: string } Throws an error object containing the HTTP status code and response data if the authentication request fails.
*
* @example
*
* authentication.debug()
* .then(console.log)
* .catch(({ error_description }) => console.log(`Failed with cause ${error_description}`))
*
*/
debug(): Promise<{
access_token: string;
expires_in: number;
refresh_expires_in: number;
token_type: string;
notbefore_policy: number;
session_state: string;
}>;
#private;
}