/** * @typedef {Object} RefreshableTokenOpts * @property {string} clientId - OAuth client ID * @property {string} refreshToken - OAuth refresh token * @property {string} accessToken - Initial OAuth access token (JWT) * @property {OnTokenRefreshHandler} onTokenRefresh - A function that will receive the refreshed token info and perisist it for future use * @property {number} [bufferSeconds=30] - Seconds before expiration to consider token expired */ /** * This is a REQUIRED function. It gets called after the token is refreshed with new tokens. * It should save the new tokens somewhere. When starting back up, re-use the newly perissted tokens. * If you loose track of these new tokens, you will effectively be logged out. * * @typedef {(refreshSuccessEvent: RefreshSuccessEvent) => void | Promise} OnTokenRefreshHandler */ /** * @typedef {Object} RefreshSuccessEvent * @property {string} clientId - The OAuth client ID * @property {string} updatedAccessToken - The new access token * @property {string} updatedRefreshToken - The refresh token (may be updated) * @property {number} updatedExpiresAt - Unix timestamp in seconds when token expires * @property {string} prevAccessToken - The accessToken used in to initiate the refresh flow to be discarded * @property {string} prevRefreshToken - The refreshToken used in to initiate the refresh flow to be discarded * @property {number} prevExpiresAt - The expiresAt for the previous token pair */ /** * Event map for RefreshableToken * @typedef {{ * 'refresh:start': [], * 'refresh:success': [RefreshSuccessEvent], * 'refresh:error': [Error], * 'invalid': [Error] * }} RefreshableTokenEventMap */ /** * A refreshable OAuth token that automatically refreshes when expired. * Handles in-flight refresh deduplication to prevent multiple concurrent refresh requests. * * Events: * - 'refresh:start' - Emitted when token refresh begins * - 'refresh:success' - Emitted when token refresh succeeds, passes { clientId, updatedAccessToken, updatedRefreshToken, updatedExpiresAt, prevAccessToken, prevRefreshToken, prevExpiresAt } * - 'refresh:error' - Emitted when token refresh fails (transient errors), passes error * - 'invalid' - Emitted when refresh token is permanently invalid, passes error * * @extends {EventEmitter} */ export class RefreshableToken extends EventEmitter { /** * @param {RefreshableTokenOpts} opts */ constructor({ clientId, refreshToken, accessToken, bufferSeconds, onTokenRefresh }: RefreshableTokenOpts); /** * Get a valid access token, refreshing if necessary. * @returns {Promise} Valid access token * @throws {Error} If token is invalid or refresh fails */ getAccessToken(): Promise; /** * Stop background token refresh scheduling. */ stopAutoRefresh(): void; /** * Start background token refresh scheduling (enabled by default). */ startAutoRefresh(): void; /** * Check if background token refresh scheduling is enabled. * @returns {boolean} */ isAutoRefreshEnabled(): boolean; /** * Get the current OAuth client ID (synchronous). * @returns {string} */ get clientId(): string; /** * Get the latest access token value (synchronous). * Prefer getAccessToken() if you need a guaranteed-valid token in async contexts. * @returns {string} */ get accessToken(): string; /** * Check if the token is currently valid (not expired and not marked invalid). * @returns {boolean} True if token is valid */ isValid(): boolean; /** * Get the expiration timestamp of the current access token. * @returns {number} Unix timestamp (seconds since epoch) */ getExpiresAt(): number; /** * Get the time remaining until token expiration. * @returns {number} Seconds until expiration (may be negative if expired) */ getTimeRemaining(): number; /** * Manually trigger a token refresh, regardless of expiration status. * Useful for proactive refresh or testing. * @returns {Promise} Token information including clientId, accessToken, refreshToken, and expiresAt * @throws {Error} If token is invalid or refresh fails */ refresh(): Promise; #private; } export type RefreshableTokenOpts = { /** * - OAuth client ID */ clientId: string; /** * - OAuth refresh token */ refreshToken: string; /** * - Initial OAuth access token (JWT) */ accessToken: string; /** * - A function that will receive the refreshed token info and perisist it for future use */ onTokenRefresh: OnTokenRefreshHandler; /** * - Seconds before expiration to consider token expired */ bufferSeconds?: number; }; /** * This is a REQUIRED function. It gets called after the token is refreshed with new tokens. * It should save the new tokens somewhere. When starting back up, re-use the newly perissted tokens. * If you loose track of these new tokens, you will effectively be logged out. */ export type OnTokenRefreshHandler = (refreshSuccessEvent: RefreshSuccessEvent) => void | Promise; export type RefreshSuccessEvent = { /** * - The OAuth client ID */ clientId: string; /** * - The new access token */ updatedAccessToken: string; /** * - The refresh token (may be updated) */ updatedRefreshToken: string; /** * - Unix timestamp in seconds when token expires */ updatedExpiresAt: number; /** * - The accessToken used in to initiate the refresh flow to be discarded */ prevAccessToken: string; /** * - The refreshToken used in to initiate the refresh flow to be discarded */ prevRefreshToken: string; /** * - The expiresAt for the previous token pair */ prevExpiresAt: number; }; /** * Event map for RefreshableToken */ export type RefreshableTokenEventMap = { "refresh:start": []; "refresh:success": [RefreshSuccessEvent]; "refresh:error": [Error]; "invalid": [Error]; }; import { EventEmitter } from 'node:events'; //# sourceMappingURL=token.d.ts.map