@dvsa/appdev-api-common
Version:
Utils library for common API functionality
66 lines (65 loc) • 2.23 kB
TypeScript
export interface ClientCredentialsResponse {
token_type: string;
expires_in: number;
ext_expires_in: number;
access_token: string;
}
interface Options {
/**
*
* Whether to log debug messages & tokens during the token retrieval process.
* Useful for troubleshooting and understanding the flow of operations.
*
* Use with Caution, this could leak secrets!
*/
debugMode?: boolean;
/**
* Whether to skip any token caching and force fresh retrievals
*
*/
forceFreshAuth?: boolean;
/**
* Treat token as expired if it has less than this many seconds remaining.
* Helps avoid clock skew / in-flight expiry. (Default 30 secs)
*/
expirySkewSeconds?: number;
}
export declare class ClientCredentials {
private readonly tokenUrl;
private readonly clientId;
private readonly clientSecret;
private readonly scope;
private readonly resource;
private readonly options;
private static accessToken;
private static readonly grantType;
/**
* Create a new instance of the ClientCredentials class
* @param tokenUrl - The URL to fetch the access token from
* @param clientId - The client id
* @param clientSecret - The client secret
* @param scope - The scope of the access token
* @param resource
* @param options
*/
constructor(tokenUrl: string, clientId: string, clientSecret: string, scope: string | undefined, resource: string | undefined, options?: Options);
/**
* Helper method to perform the client credentials flow and return the access token
* This method will check for the existence of the token and if it is expired, it will fetch a new one
* @returns {Promise<string>} - The access token
*/
getAccessToken(): Promise<string>;
/**
* Fetch the client credentials from the token URL
* @returns {Promise<ClientCredentialsResponse>} - The response from the token URL
* @private
*/
private fetchClientCredentials;
/**
* Check if the access token is expired
* @returns {boolean} - Whether the access token is expired
* @private
*/
private static isAccessTokenExpired;
}
export {};